#include <reg52.h>
//宏定义
sbit RS = P3^5;
sbit RW = P3^6;
sbit EN = P3^4;
sbit DQ = P2^2;
unsigned int shuju;
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char code digital[] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
//延时函数
void yanchi_1602(unsigned char q)
{
unsigned char p;
while(q--)
{
for(p=0;p<=113;p++);
}
}
//
void Delay(int n)
{
while(n--);
}
//发送命令
void Wcmd(unsigned char cmd)
{
RS = 0;
RW = 0;
EN = 1;
P0 = cmd;
yanchi_1602(5);
EN = 0;
}
//发送数据
void Wdat(unsigned char dat)
{
RS = 1;
RW = 0;
EN = 1;
P0 = dat;
yanchi_1602(5);
EN = 0;
}
//初始化1602屏幕
void start_1602()
{
Wcmd(0x01); //清屏
Wcmd(0x02); //光标归位
Wcmd(0x06); //设置输入模式
Wcmd(0x0C); //显示设置
Wcmd(0x38); //功能设置
}
//
void plan()
{
a = shuju/100;
b = shuju%100/10;
c = shuju%10;
}
//B站
//初始化DS18B20
void Init_18b20()
{
DQ = 1; //DQ复位
Delay(4); //延时
DQ = 0; //DQ拉低
Delay(100); //精确延时大于48us
DQ = 1; //DQ拉高
Delay(40); //延时
}
//读数据函数
unsigned char Readdat()
{
unsigned char i = 0;
unsigned char dat = 0;
for(i=8;i>0;i--)
{
DQ = 1;
Delay(1);
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
dat |= 0x80;
Delay(10);
}
return(dat);
}
//写数据函数
void Writedat(unsigned char dat)
{
unsigned char i = 0;
for(i=8;i>0;i--)
{
DQ = 0;
DQ = dat & 0x01;
Delay(10);
DQ = 1;
dat >>= 1;
}
}
//读取温度
unsigned int ReadDigital(void)
{
unsigned char m;
unsigned char n;
Init_18b20();
Writedat(0xcc); //跳过读序列号
Writedat(0x44); //启动温度转换
Delay(5);
Init_18b20();
Writedat(0xcc); //跳过读序列号
Writedat(0xbe); //开始读取温度
m = Readdat();
n = Readdat();
shuju = n;
shuju <<= 8;
shuju = shuju|m;
shuju = (shuju*0.0625)*10; //温度值扩大十倍,精确到一位小数
return(shuju);
}
void main()
{
start_1602();
Wcmd(0x82);
Wdat('t');
Wcmd(0x83);
Wdat('e');
Wcmd(0x84);
Wdat('m');
Wcmd(0x85);
Wdat('p');
Wcmd(0x86);
Wdat('e');
Wcmd(0x87);
Wdat('r');
Wcmd(0x88);
Wdat('a');
Wcmd(0x89);
Wdat('t');
Wcmd(0x8a);
Wdat('u');
Wcmd(0x8b);
Wdat('r');
Wcmd(0x8c);
Wdat('e');
Wcmd(0x8d);
Wdat(':');
while(1)
{
shuju = ReadDigital();
plan();
Wcmd(0xc4);
Wdat('+');
Wcmd(0xc5);
Wdat(digital[a]);
Wcmd(0xc6);
Wdat(digital[b]);
Wcmd(0xc7);
Wdat('.');
Wcmd(0xc8);
Wdat(digital[c]);
}
}