1、t tempreadbit(void);uchar tempread(void);void tempwritebyte(uchar dat);void tempchange(void);uint get_temp();uchar keyscan(void);/*定时器0和中断系统初始化*/void init() HEAT=1; TMOD=0x11; TH0=(65536-1000)/256; /1ms时基 TL0=(65536-1000)%256; TH1=(65536-1000)/256; TL1=(65536-1000)%256; ET0=1; ET1=1; EA=1; TR0=1;voi
2、d main()init();while(1) switch(keyscan_num)case 0:P20=1;P21=1;P22=1;P23=1;P0=0xbf; break;case 1:if(wendu_value(set_temp_H*10)COOLING=0;H_warn=0;elseCOOLING=1;H_warn=1;break; void timer0(void) interrupt 1static uchar timecount=0,timecount1=0,timecount3=0;TR0=0;TH0=(65536-1000)/256;TL0=(65536-1000)%25
3、6;timecount+;timecount1+;timecount3+;if(timecount=5)timecount=0; if(keyscan_num=1)display();if(timecount1=200)timecount1=0;tempchange();wendu_value=get_temp();if(timecount3=50)timecount3=0;keyscan_num=keyscan();TR0=1;void timer1(void) interrupt 3 static uchar timecount2=0;TR1=0;TH1=(65536-1000)/256;
4、 TL1=(65536-1000)%256;timecount2+;if(timecount2=200)timecount2=0;time20ms=!time20ms; TR1=1;/*数码管显示子程序*/void display()temp_shi=wendu_value/100;temp_ge=wendu_value%100/10;temp_xiaoshu=wendu_value%100%10;P20=0;P21=0;P22=0;P23=0;if(temp_wela=0)P0=tabletemp_shi;P20=1; if(temp_wela=1)P0=tabletemp_ge+10;P2
5、1=1;if(temp_wela=2)P0=tabletemp_xiaoshu;P22=1;temp_wela+;if(temp_wela=3)temp_wela=0;/*温度读取子程序*/void delay(uint delay_x)uint x,y; for(x=delay_x;x0;x-)for(y=110;y0;y-);void dsreset(void) /DS18B20复位,初始化函数uint i;ds=0;i=103;while(i0)i-;ds=1;i=4;while(i0)i-;bit tempreadbit(void) /读一位数据函数uint i;bit dat;ds=
6、0;i+;ds=1;i+;i+;dat=ds;i=8;while(i0)i-;return(dat);uchar tempread(void) /读1个字节数据函数uchar i,j,dat;dat=0;for(i=1;i=8;i+)j=tempreadbit();dat=(j1); /读出的数据最低位在最前面,这样刚好一个字节在dat里return(dat);void tempwritebyte(uchar dat) /向DS18B20写一个字节数据函数uint i;uchar j;bit testb;for(j=1;j1;if(testb) /写1ds=0;i+;i+;ds=1;i=8;w
7、hile(i0)i-;elseds=0; /写0i=8;while(i0)i-;ds=1;i+;i+;void tempchange(void) /DS18B20开始获取温度并转换dsreset();delay(1);tempwritebyte(0xcc); /写跳过读ROM指令tempwritebyte(0x44); /写温度转换指令uint get_temp() /读取寄存器中存储的温度数据uchar a,b;dsreset();delay(1);tempwritebyte(0xcc); tempwritebyte(0xbe);a=tempread(); /读低8位b=tempread()
8、; /读高8位temp=b;temp=8;temp=temp|a;f_temp=temp*0.0625;temp=f_temp*10+0.5;f_temp=f_temp+0.05;return temp; /temp是整型/*键盘子程序*/uchar keyscan(void)static uchar k=0;uchar Trg,Cont,ReadData;P3=0xff;ReadData=P30xff;Trg=ReadData&(ReadDataCont);Cont=ReadData;switch(Trg)case 0x01:k=1;break;case 0x02:k=2;break;case 0x04:k=3;break;case 0x08:k=4;break;return(k);4 总结.