#include "reg52.h"http://頭文件#include "intrins.h"#define uchar unsigned char//宏定義#define uint unsigned int//宏定義sbit led_7=P1^7;//;;;LED秒閃爍sbit DQ=P2^2;////定義I/O的硬件接口--溫度uchar code temp[4][8]= {{40,92,34,152,2,0,0,132}, //張{40,42,193,186,2,0,0,77}, //江{40,153,209,186,2,0,0,111}, //斌{40,14,151,7,3,0,0,137}}; //云sbit beep=P3^6;sbit rs=P3^5; sbit lcden=P3^4;sbit key1=P2^4;sbit key2=P2^5;uchar table1[8]={0};uchar fuhao;uchar count=1;void delay1(uchar x2)
//經(jīng)精準計算,該段程序1次延時時間為 19.53us;多次調(diào)用需要重新計算。{while(x2>0){x2--;}}void delay3(uint t){while(t--);}void DB18B20_init()//DB18B20復位{DQ=1;delay1(1);//程序1次延時時間為 19.53usDQ=0;delay1(110);//程序110次延時時間為 965.71usDQ=1;delay1(1);//程序1次延時時間為 19.53uswhile(DQ==1);if(DQ==0)led_7=~led_7;//led檢測復位正常否,如果DB18B20存在,且正常;led閃爍。while(DQ==0);delay1(20);//程序20次延時時間為 197.32us}void write_DB18B20(uchar m1)//寫DB18B20{uchar i,m2;for(i=0;i8;i++){m2=m1;m2=m20x01;DQ=0; _nop_();DQ=1;if(m2==0x01) DQ=1;elseDQ=0;delay1(5);//程序5次延時時間為 54.25usm1=m1>>1;DQ=1;}}//從總線上取1 個字節(jié)uchar read_byte(void){uchar i;uchar value=0;for(i=8;i>0;i--){DQ=1;value>>=1;DQ=0;nop_();_nop_(); //2 usDQ=1;_nop_();_nop_();_nop_();_nop_(); //4 usif(DQ)value|=0x80;delay3(6); //66 us}DQ=1;return(value);}void delay2(uint z) //延時函數(shù){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void write_com(uchar com)//液晶寫命令{rs=0; //指令選通lcden=0;P0=com; //送指令delay2(5);lcden=1;delay2(5);lcden=0; }void write_date(uchar date)//液晶寫數(shù)據(jù){rs=1; //數(shù)據(jù)選通lcden=0; //液晶使能信號P0=date; //送數(shù)據(jù)delay2(5);lcden=1; delay2(5);lcden=0; }void init() //初始化函數(shù){lcden=0;write_com(0x38); //設(shè)置顯示模式write_com(0x0c); // 不顯示光標write_com(0x06);// 地址指針自動加1write_com(0x01);// 顯示開始清屏write_com(0x80); // 設(shè)置數(shù)據(jù)指針write_date(0x35);}void write_sfm(uchar date) //寫數(shù)據(jù){uchar bai,shi,ge;bai=date/100;shi=date%100/10;ge=date%10;if(bai==0){write_date(0x30+shi);write_date(0x30+ge);}else{ write_date(0x30+bai);write_date(0x30+shi);write_date(0x30+ge);}}void display(uchar dis){ write_com(0x80+0x40);write_date(0x30+dis+1);if(fuhao==1){write_com(0x80+0x40+5);write_date(0x2d);}else write_com(0x80+0x40+5);write_sfm(table1[2*dis]);write_date(0x2e);write_sfm(table1[2*dis+1]);}void read_tmper(uchar tt){ uint twork;uchar i_s;uint zhenshu,xiaoshu;DB18B20_init();delay1(10);write_DB18B20(0xcc);write_DB18B20(0x44);delay1(20);DB18B20_init();delay1(10);write_DB18B20(0x55);for(i_s=0;i_s8;i_s++){write_DB18B20(temp[tt][i_s]);}write_DB18B20(0xbe);table1[2*tt]=read_byte();table1[2*tt+1]=read_byte();twork=table1[2*tt+1];twork=8;twork=twork|table1[2*tt];if(twork0x0800){fuhao=1;twork=~twork;zhenshu=twork>>4;zhenshu=zhenshu0x007f;table1[2*tt]=zhenshu;xiaoshu=twork0x000f;xiaoshu=xiaoshu*625/100;table1[2*tt+1]=xiaoshu;}else{ fuhao=0;zhenshu=twork>>4;zhenshu=zhenshu0x007f;table1[2*tt]=zhenshu;xiaoshu=twork0x000f;xiaoshu=xiaoshu*625/100;table1[2*tt+1]=xiaoshu;}}void keyscan(){if(key1==0){ while(!key1);count++;if(count==5)count=1;}if(key2==0){ while(!key2);count--;if(count==0)count=4;}}void alarm(){if((table1[3]>50count==0)||(table1[3]>10fuhao==1count==0)){beep=~beep;}if((table1[3]>50count==1)||(table1[3]>10fuhao==1count==1)){beep=0;delay1(100);beep=1;}if((table1[3]>50count==2)||(table1[3]>10fuhao==1count==2)){beep=0;delay1(50);beep=1;delay1(50);beep=0;delay1(50);beep=1;}if((table1[3]>50count==3)||(table1[3]>10fuhao==1count==3)){beep=0;delay1(70);beep=1;delay1(70);beep=0;}}void main(){ init();while(1){ read_tmper(0);read_tmper(1);read_tmper(2);read_tmper(3);display(count-1); keyscan();alarm();}}
評論