#include#defineucharunsignedchar#defineuintunsignedint#definers_h(PORTC|=0x01)#definers_l(PORTC&=0xfe)#definers_o(TRISC&=0xfe)#definerw_h(PORTC|=0x02)#definerw_l(PORTC&=0xfd)#definerw_o(TRISC&=0xfd)#defineen_h(PORTC|=0x04)#defineen_l(PORTC&=0xfb)#defineen_o(TRISC&=0xfb)#definetemp_h(PORTC|=0x08)#definetemp_l(PORTC&=0xf7)#definetemp_o(TRISC&=0xf7)#definetemp_i(TRISC|=0x08)#defineled_o(TRISC&=0xef)#defineled_l(PORTC&=0xef)#defineled_h(PORTC|=0x10)#defineLCDPORTBuchardat1,dat2;//保存讀出的溫度zunsignedlongintdat;voiddelayms(uintx)//4M晶振下,延時(shí)1ms{uinty,z;for(y=x;y>0;y--)for(z=110;z>0;z--);}voidDs18b20_reset(void)//DS18B20初始化{uintcount;uchari,flag=1;temp_o;temp_l;for(count=60;count>0;count--);//延時(shí)480ustemp_i;while(flag){if(RC3)flag=1;elseflag=0;}led_o;led_l;//開指示燈for(count=60;count>0;count--);//延時(shí)480us}voidDs18b20_write(uchardatt)//向DS18B20寫一個(gè)字節(jié){ucharcount;uchari;temp_o;for(i=8;i>0;i--){temp_o;temp_l;for(count=1;count>0;count--);if(datt&0x01==0x01)temp_i;else{temp_o;temp_l;}for(count=23;count>0;count--);//延時(shí)60ustemp_i;for(count=1;count>0;count--);datt>>=1;}}ucharDs18b20_read(void)//從DS18B20讀一個(gè)字節(jié){uchari,datt;ucharcount;for(i=8;i>0;i--){datt>>=1;temp_o;temp_l;for(count=1;count>0;count--);temp_i;//改為輸入方向時(shí),上拉電阻把數(shù)據(jù)線拉高,釋放總線,此語句必須有,參考datasheet的P15for(count=1;count>0;count--);if(RC3)datt|=0x80;for(count=23;count>0;count--);//延時(shí)60us}returndatt;}voidlcd_com(ucharcom)//向LCD1602寫命令{rs_o;rw_o;en_o;TRISB=0x00;//配置RB為輸出方向rs_l;rw_l;LCD=com;delayms(1);en_h;delayms(1);en_l;delayms(1);}voidlcd_dat(uchardat)//向LCD1602寫數(shù)據(jù){rs_o;rw_o;TRISB=0x00;//配置RB為輸出方向en_o;rs_h;rw_l;LCD=dat;delayms(1);en_h;delayms(1);en_l;delayms(1);}voidlcd_write(ucharc,ucharr,uchardat)//向LCD1602指定行、指定列、寫數(shù)據(jù){lcd_com(0x80+0x40*c+r);lcd_dat(dat);delayms(1);}voidlcd_init(void)//LCD1602初始化,初始化后第一行顯示temperature:,第二行顯示.C{lcd_com(0x38);lcd_com(0x0c);lcd_com(0x06);lcd_write(0,2,0x54);lcd_write(0,3,0x65);lcd_write(0,4,0x6d);lcd_write(0,5,0x70);lcd_write(0,6,0x65);lcd_write(0,7,0x72);lcd_write(0,8,0x61);lcd_write(0,9,0x74);lcd_write(0,10,0x75);lcd_write(0,11,0x72);lcd_write(0,12,0x65);lcd_write(0,13,0x3a);lcd_write(1,11,0xdf);lcd_write(1,12,0x43);}voidshow(void)//把溫度值送LCD1602顯示{ucharflag;uchart[4];uinttemp;if(dat2>=240){dat=(~(dat2*256+dat1)+1)*(0.0625*10);//取反加一,保留一位小數(shù)flag=1;}else{dat=(dat2*256+dat1)*(0.0625*10);flag=0;};temp=dat%10;t[0]=(0x30+temp);temp=dat%100;temp=temp/10;t[1]=(0x30+temp);temp=dat%1000;temp=temp/100;t[2]=(0x30+temp);temp=dat/1000;t[3]=(0x30+temp);if(flag==1)//負(fù)溫度顯示{lcd_write(1,10,t[0]);lcd_write(1,9,0xa5);lcd_write(1,8,t[1]);lcd_write(1,7,t[2]);lcd_write(1,6,t[3]);lcd_write(1,5,0x2d);}if(flag==0)//正溫度顯示{lcd_write(1,10,t[0]);lcd_write(1,9,0xa5);lcd_write(1,8,t[1]);lcd_write(1,7,t[2]);lcd_write(1,6,t[3]);lcd_write(1,5,0x20);//顯示空格,刷掉負(fù)號(hào)}}voidmain(void){lcd_init();while(1){Ds18b20_reset();Ds18b20_write(0xcc);Ds18b20_write(0x44);//發(fā)送溫度轉(zhuǎn)換命令delayms(1000);//延時(shí)1s,等待溫度轉(zhuǎn)換完成Ds18b20_reset();Ds18b20_write(0xcc);Ds18b20_write(0xbe);//發(fā)送讀溫度寄存器命令dat1=Ds18b20_read();dat2=Ds18b20_read();show();led_h;//關(guān)指示燈delayms(2000);}}//調(diào)試總結(jié)://某IO口8位未全使用時(shí),對(duì)整個(gè)IO口讀取進(jìn)行位運(yùn)算無效