//*****************************************************************
/*程序功用:在1602液晶屏上显现当时温度触及芯片:1602液晶、SD18b20温度传感器*///*****************************************************************#include#define uint unsigned int #define uchar unsigned charsbit DQ=P0^3;sbit rs=P2^0;sbit rw=P2^1;sbit en=P2^2;uchar low,high;//====================================函数声明//*********sd18b20*******************void delayus(uint time);void init_18b20();void write_18b20(uchar date);uchar read_18b20();void wndu();//**********1602液晶**************void init_1602();void write_com1602(uchar com);void write_date1602(uchar date);//====================================void delayms(uint time)//毫秒级延时函数{uint i,j;for(i=time;i>0;i--)for(j=110;j>0;j--);}void delayus(uint time)//微秒级延时函数{while(time--);}void init_18b20()//对sd18b20的初始化{uchar n;DQ=1;delayus(8);DQ=0;delayus(80);DQ=1;delayus(8);n=DQ;delayus(8);}/*将数据线拉为低电平发生写时刻片,在数据线口拉为低电平在15微秒到60微秒间对数据线进行数据采样.假如为高电平则为写“1”,假如为低电平则为写零。关于主机发生的写“1”时刻片的状况:数据线有必要必先拉为低电平,然后被开释,是数据线在发生时刻片15微秒之内拉至高电平;关于主机发生的写“0”时刻片的状况:数据线有必要被拉至逻辑电平而且坚持低电平60个微秒*/void write_18b20(uchar com){uint i;for(i=0;i<8;i++){DQ=0;//delayus(4);DQ=com&0x01;delayus(10);DQ=1;com>>=1;delayus(4);}delayus(4);}/*从ds18b20读数据时,主机发生读时刻片。当主机把数据线从逻辑高电平拉至低电平时,发生读时刻片。数据线有必要坚持在逻辑低电平至少1个微秒。来至ds18b20的数据在读时刻下降沿之后15微秒有用。因而,为了读出从时刻片开端15微秒的状况,主机有必要中止坚持数据线为逻辑低电平。*/uchar read_18b20(){uint i;uchar date=0x00;for(i=0;i<8;i++){DQ=0;date>>=1;DQ=1;if(DQ)date|=0x80;delayus(4);DQ=1;delayus(4);}return date;}void wendu(){init_18b20();write_18b20(0xcc);//越过ROMwrite_18b20(0x44);//发动温度丈量delayus(300);init_18b20();write_18b20(0xcc);//越过ROMwrite_18b20(0xbe);//读取温度low=read_18b20(); //先读低八位high=read_18b20();//后读高八位}void xianshi(){uchar a,b,c,d;float m,n=0.0625;high<<=4;high+=(low&0xf0)>>4;m=(low%10)*n;delayus(4);write_com1602(0x80+0x03);a=high/10; //显现十位上的数write_date1602(0x30+a);delayms(5);write_com1602(0x80+0x04);b=high%10; //显现个位上的数write_date1602(0x30+b);delayms(5);write_com1602(0x80+0x06);c=(int)(m*10); //小数点后一位write_date1602(0x30+c);delayms(5);d=(int)(m*100)%10; //小数点后第二位write_com1602(0x80+0x07);write_date1602(0x30+d);delayms(5);write_com1602(0x80+0x08);write_date1602(0x27);delayms(5);write_com1602(0x80+0x09);write_date1602(C);delayms(5);write_com1602(0x80+0x05);write_date1602(0x2e);delayms(5); }void main(){init_1602();while(1){wendu();xianshi();}}void init_1602(){en=0;rw=0;write_com1602(0x28);delayms(5);write_com1602(0x01);delayms(5);write_com1602(0x0c);delayms(5);write_com1602(0x06);delayms(5);}void write_com1602(uchar com){uchar gao,di;gao=com&0xf0;di=(com<<4)&0xf0;delayms(5);P2=gao;rs=0;delayms(5);en=1;delayms(5);en=0;delayms(5);P2=di;rs=0;delayms(5);en=1;delayms(5);en=0;delayms(5);}void write_date1602(uchar date){uchar gao,di;gao=date&0xf0;di=(date<<4)&0xf0;delayms(5);P2=gao;rs=1;delayms(5);en=1;delayms(5);en=0;delayms(5);P2=di;rs=1;delayms(5);en=1;delayms(5);en=0;delayms(5);}