/************************************************************************ 采用四位共陰極數(shù)碼管琒R42056K,P1口接數(shù)碼管段碼。P0.0接第一位* P0.1接第二位,p0.2接第三位,p0.3接第四位。* 單片機(jī)AR89S52,晶振7.3728M* DS18B20接P2.0。 ************************************************************************/ #include#include#define uchar unsigned char#define uint unsigned int/*********************************************************************** * DS18B20 數(shù)據(jù)口 DQ* 共陰極數(shù)碼管*共陰極數(shù)碼管帶小數(shù)點(diǎn)***********************************************************************/ //#define DQ RC1//#define DQ_HIGH() TRISC1=1//#define DQ_LOW() TRISC1=0;DQ=0//__CONFIG(0x3B31);sbit DQ=P2^0;//const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,// 0x07,0x7f,0x6f};unsigned char code table[]={//共陰極0~f數(shù)碼管編碼0x3f,0x06,0x5b,0x4f, //0~30x66,0x6d,0x7d,0x07, //4~70x7f,0x6f,0x77,0x7c, //8~b0x39,0x5e,0x79,0x71 //c~f};//const uchar table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,// 0x87,0xff,0xef};const uchar table1[]={0xbf,0x86,0xdb,0xcf,//0-30xe6,0xed,0xfd,0x87,//4-70xff,0xef//8-9};uint temper;//溫度值緩存uchar a1,a2,a3,a4;//數(shù)碼管每個(gè)位上顯示的數(shù)字uchar time=0;//延時(shí)緩存uchar TH; //判斷負(fù)值void delay(uint x);void delayus(uint,uchar);void init();//初始化void disp(uchar num1,uchar num2,uchar num3,uchar num4);//正值顯示void disp1(uchar num1,uchar num2,uchar num3);//負(fù)值顯示void write_byte(uchar date);void delayms(uchar i); //誤差 -0.651041666667usuchar read_byte();void get_tem();//獲取溫度void delay20us();void delay30us();void delay45us();void delay70us();void delay750us();void delay500us();void InitTimer0(void);void delay20us(void) //誤差 -0.46875us{unsigned char a,b;for(b=1;b>0;b--)for(a=3;a>0;a--);}void delay30us(void) //誤差 -0.703125us{unsigned char a,b;for(b=3;b>0;b--)for(a=1;a>0;a--);}void delay45us(void) //誤差 -1.0546875us{unsigned char a;for(a=12;a>0;a--);}void delay70us(void) //誤差 -0.013020833333us{unsigned char a;for(a=20;a>0;a--);}void delay750us(void) //誤差 -1.302083333333us{unsigned char a,b;for(b=1;b>0;b--)for(a=227;a>0;a--);}void delay500us(void) //誤差 -0.325520833333us{unsigned char a;for(a=152;a>0;a--);}void InitTimer0(){TMOD = 0x01;TH0 = 0x88;TL0 = 0x00;EA = 1;ET0 = 1;TR0 = 1;}void main(){init();InitTimer0();while(1){uchar num;for(num=100;num>0;num--){if(TH<0X7F)disp(a1,a2,a3,a4);elsedisp1(a1,a2,a3);}} }/************************************************************************ 復(fù)位18B20***********************************************************************/void reset(){uchar st=1;// DQ_HIGH();DQ=1;// NOP();NOP();_nop_();while(st){// DQ_LOW();DQ=0;// delayus(70,30);delay750us();// DQ_HIGH();DQ=1;// delayus(4,4);delay70us();if(DQ==1)st=1;elsest=0;//delayus(50,10);delay500us();}}/************************************************************************ 寫(xiě)字節(jié)***********************************************************************/void write_byte(uchar date){uchar i,temp;// DQ_HIGH();DQ=1;// NOP();NOP();_nop_();for(i=8;i>0;i--){temp=date&0x01;//01010101// DQ_LOW();DQ=0;// delayus(0,0);delay20us();if(temp==1)// DQ_HIGH();DQ=1;// delayus(2,2);delay45us();// DQ_HIGH();DQ=1;date=date>>1;//00101010}}/************************************************************************ 讀字節(jié)***********************************************************************/uchar read_byte(){uchar i,date;static bit j;for(i=8;i>0;i--){date=date>>1;// DQ_HIGH();DQ=1;// NOP();NOP();_nop_();// DQ_LOW();DQ=0;// NOP();NOP();NOP();NOP();NOP();NOP();_nop_();_nop_();// DQ_HIGH();DQ=1;// NOP();NOP();NOP();NOP();_nop_();j=DQ;if(j==1)date=date|0x80;//1000 0000// delayus(1,1);delay30us();}return (date);}
評(píng)論