资源描述:
《单片机数字温度计c语言程序设计》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、电路设计图源程序#include#includeunsignedcharcodedisplaybit[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsignedcharcodedisplaycode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x40};unsignedcharcodedotco
2、de[32]={0,3,6,9,12,16,19,22,25,28,31,34,38,41,44,48,50,53,56,59,63,66,69,72,75,78,81,84,88,91,94,97};unsignedchardisplaycount;unsignedchardisplaybuf[8]={16,16,16,16,16,16,16,16};unsignedchartimecount;unsignedcharreaddata[8];sbitDQ=P3^7;bitsflag;bitresetpulse(v
3、oid){unsignedchari;DQ=0;for(i=255;i>0;i--);DQ=1;for(i=60;i>0;i--);return(DQ);for(i=200;i>0;i--);}voidwritecommandtods18b20(unsignedcharcommand){unsignedchari;unsignedcharj;for(i=0;i<8;i++){if((command&0x01)==0){DQ=0;for(j=35;j>0;j--);DQ=1;}else{DQ=0;for(j=2;j>
4、0;j--);DQ=1;for(j=33;j>0;j--);}command=_cror_(command,1);}}unsignedcharreaddatafromds18b20(void){unsignedchari;unsignedcharj;unsignedchartemp;temp=0;for(i=0;i<8;i++){temp=_cror_(temp,1);DQ=0;_nop_();_nop_();DQ=1;for(j=10;j>0;j--);if(DQ==1){temp=temp
5、0x80;}else
6、{temp=temp
7、0x00;}for(j=200;j>0;j--);}return(temp);}voidmain(void){TMOD=0x01;TH0=(65536-4000)/256;TL0=(65536-4000)%6;ET0=1;EA=1;while(resetpulse());writecommandtods18b20(0xcc);writecommandtods18b20(0x44);TR0=1;while(1){;}}voidt0(void)interrupt1using0{unsignedch
8、arx;unsignedintresult;TH0=(65536-4000)/256;TL0=(65536-4000)%6;if(displaycount==2){P0=displaycode[displaybuf[displaycount]]
9、0x80;}else{P0=displaycode[displaybuf[displaycount]];}P2=displaybit[displaycount];displaycount++;if(displaycount==8){displaycount=0;}timec
10、ount++;if(timecount==150){timecount=0;while(resetpulse());writecommandtods18b20(0xcc);writecommandtods18b20(0xbe);readdata[0]=readdatafromds18b20();readdata[1]=readdatafromds18b20();for(x=0;x<8;x++){displaybuf[x]=16;}sflag=0;if((readdata[1]&0xf8)!=0x00){sflag=
11、1;readdata[1]=~readdata[1];readdata[0]=~readdata[0];result=readdata[0]+1;readdata[0]=result;if(result>255){readdata[1]++;}}readdata[1]=readdata[1]<<4;readdata[1]=readdata[1]&0x70;x