#include "reg51.h" #include "intrins.h" #define dm P0 //以P0口作為LED段碼輸出口 #define uchar unsigned char #define uint unsigned int sbit DQ=P2^7; //以P2.7口作為溫度傳感器輸入口 sbit w0=P2^0; //數(shù)碼管4 sbit w1=P2^1; //數(shù)碼管3 sbit w2=P2^2; //數(shù)碼管2 sbit w3=P2^3; //數(shù)碼管1
int temp1=0; //顯示當前溫度和設(shè)置溫度的標志位為0 時顯示當前溫度 uint h; uint temp; uchar r; uchar high=35,low=20; uchar sign; uchar q=0; uchar tt=0; uchar scale; uchar code ditab[16]= {0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09}; uchar code table_dm[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40}; uchartable_dm1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; uchar data temp_data[2]={0x00,0x00}; uchar datadisplay[5]={0x00,0x00,0x00,0x00,0x00}; //延時電路程序// void delay(uint t) { for (;t>0;t--); } void scan() { int j; for(j=0;j<4;j++) { switch (j) { case 0:dm=table_dm[display[0]];w0=0;delay(50);w0=1;//xiaoshu case 1: dm=table_dm1[display[1]];w1=0;delay(50);w1=1;//gewei case 2:dm=table_dm[display[2]];w2=0;delay(50);w2=1;//shiwei case 3:dm=table_dm[display[3]];w3=0;delay(50);w3=1;//baiwei //else{dm=table_dm[b3];w3=0;delay(50);w3=1;} } } } //復位電路程序/ ow_reset(void) { char presence=1; while(presence) { while(presence) { DQ=1;_nop_();_nop_(); DQ=0;---== delay(50); DQ=1; delay(6); presence=DQ; //presence=0 } delay(45); presence=~DQ; } DQ=1; } //DS18B20 寫命令函數(shù)// void write_byte(uchar val) { uchar i; for(i=8;i>0;i--) { DQ=1;_nop_();_nop_(); DQ=0;_nop_();_nop_();_nop_();_nop_(); //5us DQ=val&0x01; delay(6); val=val/2; } DQ=1; delay(1); } //DS18B20 讀1 字節(jié)函數(shù)// uchar read_byte(void) { uchar i; uchar value=0; for(i=8;i>0;i--) { DQ=1;_nop_();_nop_(); value>>=1; DQ=0;_nop_();_nop_();_nop_();_nop_(); //4us DQ=1;_nop_();_nop_();_nop_();_nop_(); //4us if(DQ)value|=0x80; delay(6); //66 us } DQ=1; return(value); } //讀出溫度函數(shù)// read_temp() { ow_reset(); delay(200); write_byte(0xcc); write_byte(0x44); ow_reset(); delay(1); write_byte(0xcc); write_byte(0xbe); temp_data[0]=read_byte(); temp_data[1]=read_byte(); temp=temp_data[1]; temp<<=8; temp=temp|temp_data[0]; return temp; } //溫度數(shù)據(jù)處理函數(shù)// work_temp(uint tem) { uchar n=0; if(tem>6348) // 溫度值正負判斷 {tem=65536-tem;n=1;} // 負溫度求補碼,標志位置1 display[4]=tem&0x0f; // 取小數(shù)部分的值 display[0]=ditab[display[4]]; // 存入小數(shù)部分顯示值 display[4]=tem>>4; // 取中間八位,即整數(shù)部分的值 display[3]=display[4]/100; // 取百位數(shù)據(jù)暫存 display[1]=display[4]%100; // 取后兩位數(shù)據(jù)暫存 display[2]=display[1]/10; // 取十位數(shù)據(jù)暫存 display[1]=display[1]%10; //個位數(shù)據(jù) r=display[1]+display[2]*10+display[3]*100; /////符號位顯示判斷///// if(!display[3]) { display[3]=0x0a; //最高位為0 時不顯示 if(!display[2]) { display[2]=0x0a; //次高位為0 時不顯示 } } if(n){display[3]=0x0b;} //負溫度時最高位顯示"-" }
//*********設(shè)置溫度顯示轉(zhuǎn)換************// void xianshi(int horl) { int n=0; if(horl>128) { horl=256-horl;n=1; } display[3]=horl/100; display[3]=display[3]&0x0f; display[2]=horl%100/10; display[1]=horl%10; display[0]=0; if(!display[3]) { display[3]=0x0a; //最高位為0 時不顯示 if(!display[2]) { display[2]=0x0a; //次高位為0 時不顯示 } } if(n) { display[3]=0x0b; //負溫度時最高位顯示"-" } } /****************主函數(shù)************************/ void main() { dm=0x00; //初始化端口 w0=0; w1=0; w2=0; w3=0; for(h=0;h<4;h++) //開機顯示"0000" { display[h]=0; } ow_reset(); //開機先轉(zhuǎn)換一次 write_byte(0xcc); //Skip ROM write_byte(0x44); //發(fā)轉(zhuǎn)換命令 for(h=0;h<100;h++) //開機顯示"0000" { scan(); } while(1) { if (temp1==0) { work_temp(read_temp()); //處理溫度數(shù)據(jù) scan(); //顯示溫度值 } else scan(); } }
|