Interfacing 1 wire temperature sensor DS18B20 with 8051 microcontroller and lcd display.
Circuit Diagram
C Code:
Code (Text):
//////////ds18b20///////// #include <reg51.h> #include <INTRINS.h> unsigned char readdata[2]; sbit DQ=P3^3; ////////////end of ds18b20 variable/////// /////////////lcd variable///////////////// #include <reg51.h> #include <absacc.h> #define REG0 XBYTE[0x0000] #define REG1 XBYTE[0x0001] #define REG2 XBYTE[0x0002] #define REG3 XBYTE[0x0003] unsigned char bdata busyflag; unsigned char dat,datn,count; unsigned char word1[16]={" T= "}; code unsigned char word2[16]={" by Samsung"}; code unsigned char word3[16]={"8051projects.info!"}; code unsigned char word4[16]={"8051projects.info!"}; code unsigned char word5[16]={" Wellcome To "}; code unsigned char word6[16]={" Proteus Tools!"}; code unsigned char word7[16]={"This Programme "}; code unsigned char word8[16]={" by Sonu "}; sbit busyflag_7=busyflag^7; sbit p10=P1^0; sbit p11=P1^1; sbit p12=P1^2; /////////////////end of lcd variable//////////// ///////////////ds18b20///////////////////// //Delay function void delay(unsigned int i) { while(i--); } //Initialization function void Init_DS18B20(void) { unsigned char x=0; DQ = 1; //DQ reset delay(8); //Slight delay DQ = 0; //SCM will be pulled down DQ delay(80); //Accurate than 480us delay DQ = 1; //Pulled the bus delay(14); x=DQ; //After slight delay is initialized if x = 0 x = 1 is initialized successfully defeat delay(20); } //Reading a byte unsigned char ReadOneChar(void) { unsigned char i=0; unsigned char dat = 0; for (i=8;i>0;i--) { DQ = 0; // To the pulse signal dat>>=1; DQ = 1; // To the pulse signal if(DQ) dat|=0x80; delay(4); } return(dat); } //Write a byte void WriteOneChar(unsigned char dat) { unsigned char i=0; for (i=8; i>0; i--) { DQ = 0; DQ = dat&0x01; delay(5); DQ = 1; dat>>=1; } delay(4); } //Read temperature void ReadTemperature(void) { Init_DS18B20(); WriteOneChar(0xCC); // Skip read serial number column number of operations WriteOneChar(0x44); // Start temperature conversion Init_DS18B20(); WriteOneChar(0xCC); //Skip read serial number column number of operations WriteOneChar(0xBE); //Read the temperature register, etc. (a total of 9 registers readable) is the temperature of the first two readdata[0]=ReadOneChar(); readdata[1]=ReadOneChar(); } void Tempprocess() //Temperature Conversion { unsigned int t; float tt; unsigned char temp; if((readdata[1]&0x80)!=0) { word1[3]='-'; t=readdata[1]; t<<=8; t=t|readdata[0]; t=t-1; t=~t; t>>=4; word1[4]=t/100+48; word1[5]=((t/10)%10)+48; word1[6]=t%10+48; temp=readdata[0]; temp=temp-1; temp=~temp; temp=temp&0x0f; tt=temp*0.0625; word1[7]='.'; word1[8]=(unsigned char )(tt*10); word1[9]=(unsigned char )(tt*100-word1[8]*10); word1[10]=(unsigned char )(tt*1000-word1[8]*100-word1[9]*10); word1[11]=(unsigned char )(tt*10000-word1[8]*1000-word1[9]*100-word1[10]*10); word1[8]+=48; word1[9]+=48; word1[10]+=48; word1[11]+=48; word1[12]='C'; } else { word1[3]='+'; t=readdata[1]; t<<=8; t=t|readdata[0]; t>>=4; word1[4]=t/100+48; word1[5]=((t/10)%10)+48; word1[6]=t%10+48; temp=readdata[0]; temp=temp&0x0f; tt=temp*0.0625; word1[7]='.'; word1[8]=(unsigned char )(tt*10); word1[9]=(unsigned char )(tt*100-word1[8]*10); word1[10]=(unsigned char )(tt*1000-word1[8]*100-word1[9]*10); word1[11]=(unsigned char )(tt*10000-word1[8]*1000-word1[9]*100-word1[10]*10); word1[8]+=48; word1[9]+=48; word1[10]+=48; word1[11]+=48; word1[12]='C'; } } /////////////////////end of ds18b20////////////////// ///////////////start of lcd 1602///////////////// void busy() { do { busyflag=REG1; }while(busyflag_7); } void wrc(unsigned char wcon) { busy(); REG0=wcon; } void wrd(unsigned char wdat) { busy(); REG2=wdat; } void rdd() { busy(); dat=REG3; } void lcdint() { wrc(0x38); wrc(0x01); wrc(0x06); wrc(0x0c); } void wrn(unsigned char word[]) { unsigned char i; for(i=0;i<16;i++) { wrd(word[i]); } } //////////////end of lcd 1602/////////////////////// void main() { lcdint();//Initialize LCD wrc(0x80); wrn(word5); wrc(0xc0); wrn(word6); while(1) { //if(p10==0) //Determine whether the press P1.0 { ReadTemperature(); Tempprocess(); wrc(0x80); wrn(word1); wrc(0xc0); wrn(word2); } if(p11==0)//Determine whether the press P1.1 { wrc(0x80); wrn(word3); wrc(0xc0); wrn(word4); } if(p12==0)//Determine whether the press P1.2 { wrc(0x80); wrn(word7); wrc(0xc0); wrn(word8); } } }
Interfacing DS18b20 with 8051 microcontroller 1.0
Interfacing 1 wire temperature sensor DS18B20 with 8051 microcontroller and lcd display.