1. You can now buy finished microcontroller project from us, Check out the Store for the complete list of projects.
  2. Need a custom project, Send us some details about your project. So that we can quote the price for it.

Interfacing DS18b20 with 8051 microcontroller 1.0

Interfacing 1 wire temperature sensor DS18B20 with 8051 microcontroller and lcd display.

  1. sajiv jess
    Interfacing 1 wire temperature sensor DS18B20 with 8051 microcontroller and lcd display.

    Circuit Diagram
    [​IMG]
    C Code:
    Code (Text):
    1. //////////ds18b20/////////
    2. #include <reg51.h>
    3. #include <INTRINS.h>
    4. unsigned char readdata[2];
    5. sbit DQ=P3^3;
    6. ////////////end of ds18b20 variable///////
    7. /////////////lcd variable/////////////////
    8. #include <reg51.h>
    9. #include <absacc.h>
    10.  
    11. #define REG0  XBYTE[0x0000]
    12. #define REG1  XBYTE[0x0001]
    13. #define REG2  XBYTE[0x0002]
    14. #define REG3  XBYTE[0x0003]
    15.  
    16. unsigned char bdata busyflag;
    17. unsigned char dat,datn,count;
    18.  
    19. unsigned char word1[16]={" T= "};
    20. code unsigned char word2[16]={" by Samsung"};
    21. code unsigned char word3[16]={"8051projects.info!"};
    22. code unsigned char word4[16]={"8051projects.info!"};
    23. code unsigned char  word5[16]={"  Wellcome To "};
    24. code unsigned char  word6[16]={" Proteus Tools!"};
    25. code unsigned char  word7[16]={"This Programme "};
    26. code unsigned char  word8[16]={"  by Sonu "};
    27.  
    28. sbit busyflag_7=busyflag^7;
    29. sbit p10=P1^0;
    30. sbit p11=P1^1;
    31. sbit p12=P1^2;
    32. /////////////////end of lcd variable////////////
    33. ///////////////ds18b20/////////////////////
    34. //Delay function
    35.  
    36. void delay(unsigned int i)
    37. {
    38.     while(i--);
    39. }
    40.  
    41. //Initialization function
    42. void Init_DS18B20(void)
    43. {
    44.     unsigned char x=0;
    45.     DQ = 1;    //DQ reset
    46.     delay(8);  //Slight delay
    47.     DQ = 0;    //SCM will be pulled down DQ
    48.     delay(80); //Accurate than 480us delay
    49.     DQ = 1;    //Pulled the bus
    50.     delay(14);
    51.     x=DQ;      //After slight delay is initialized if x = 0 x = 1 is initialized successfully defeat
    52.     delay(20);
    53. }
    54.  
    55. //Reading a byte
    56. unsigned char ReadOneChar(void)
    57. {
    58.     unsigned char i=0;
    59.     unsigned char dat = 0;
    60.     for (i=8;i>0;i--)
    61.     {
    62.       DQ = 0; // To the pulse signal
    63.       dat>>=1;
    64.       DQ = 1; // To the pulse signal
    65.       if(DQ)
    66.       dat|=0x80;
    67.       delay(4);
    68.     }
    69.     return(dat);
    70. }
    71.  
    72. //Write a byte
    73. void WriteOneChar(unsigned char dat)
    74. {
    75.     unsigned char i=0;
    76.     for (i=8; i>0; i--)
    77.     {
    78.       DQ = 0;
    79.       DQ = dat&0x01;
    80.       delay(5);
    81.       DQ = 1;
    82.       dat>>=1;
    83.     }
    84.     delay(4);
    85. }
    86.  
    87. //Read temperature
    88. void  ReadTemperature(void)
    89. {
    90.     Init_DS18B20();
    91.     WriteOneChar(0xCC); // Skip read serial number column number of operations
    92.     WriteOneChar(0x44); // Start temperature conversion
    93.     Init_DS18B20();
    94.     WriteOneChar(0xCC); //Skip read serial number column number of operations
    95.     WriteOneChar(0xBE); //Read the temperature register, etc. (a total of 9 registers readable) is the temperature of the first two
    96.     readdata[0]=ReadOneChar();
    97.     readdata[1]=ReadOneChar();
    98.    
    99. }
    100. void Tempprocess() //Temperature Conversion
    101. {
    102.     unsigned int t;
    103.     float tt;
    104.     unsigned char temp;
    105.     if((readdata[1]&0x80)!=0)
    106.     {
    107.         word1[3]='-';
    108.         t=readdata[1];
    109.         t<<=8;
    110.         t=t|readdata[0];
    111.         t=t-1;
    112.         t=~t;
    113.         t>>=4;
    114.         word1[4]=t/100+48;
    115.         word1[5]=((t/10)%10)+48;
    116.         word1[6]=t%10+48;
    117.         temp=readdata[0];
    118.         temp=temp-1;
    119.         temp=~temp;
    120.         temp=temp&0x0f;
    121.         tt=temp*0.0625;
    122.         word1[7]='.';
    123.         word1[8]=(unsigned char )(tt*10);
    124.         word1[9]=(unsigned char )(tt*100-word1[8]*10);
    125.         word1[10]=(unsigned char )(tt*1000-word1[8]*100-word1[9]*10);
    126.         word1[11]=(unsigned char )(tt*10000-word1[8]*1000-word1[9]*100-word1[10]*10);
    127.         word1[8]+=48;
    128.         word1[9]+=48;
    129.         word1[10]+=48;
    130.         word1[11]+=48;
    131.         word1[12]='C';
    132.  
    133.     }
    134.     else
    135.     {
    136.         word1[3]='+';
    137.         t=readdata[1];
    138.         t<<=8;
    139.         t=t|readdata[0];
    140.         t>>=4;
    141.         word1[4]=t/100+48;
    142.         word1[5]=((t/10)%10)+48;
    143.         word1[6]=t%10+48;
    144.         temp=readdata[0];
    145.         temp=temp&0x0f;
    146.         tt=temp*0.0625;
    147.         word1[7]='.';
    148.         word1[8]=(unsigned char )(tt*10);
    149.         word1[9]=(unsigned char )(tt*100-word1[8]*10);
    150.         word1[10]=(unsigned char )(tt*1000-word1[8]*100-word1[9]*10);
    151.         word1[11]=(unsigned char )(tt*10000-word1[8]*1000-word1[9]*100-word1[10]*10);
    152.         word1[8]+=48;
    153.         word1[9]+=48;
    154.         word1[10]+=48;
    155.         word1[11]+=48;
    156.         word1[12]='C';
    157.     }
    158. }
    159. /////////////////////end  of ds18b20//////////////////
    160. ///////////////start  of  lcd 1602/////////////////
    161. void busy()
    162. {
    163.     do
    164.     {
    165.         busyflag=REG1;
    166.     }while(busyflag_7);
    167. }
    168.  
    169. void wrc(unsigned char wcon)
    170. {
    171.     busy();
    172.     REG0=wcon;
    173. }
    174.  
    175. void wrd(unsigned char wdat)
    176. {
    177.     busy();
    178.     REG2=wdat;
    179. }
    180.  
    181. void rdd()
    182. {
    183.     busy();
    184.     dat=REG3;
    185. }
    186.  
    187. void lcdint()
    188. {
    189.     wrc(0x38);
    190.     wrc(0x01);
    191.     wrc(0x06);
    192.     wrc(0x0c);
    193. }
    194. void wrn(unsigned char word[])
    195. {
    196.     unsigned char i;
    197.     for(i=0;i<16;i++)
    198.     {
    199.         wrd(word[i]);
    200.     }
    201. }
    202. //////////////end of lcd 1602///////////////////////
    203. void main()
    204. {
    205.         lcdint();//Initialize LCD
    206.         wrc(0x80);
    207.         wrn(word5);
    208.         wrc(0xc0);
    209.         wrn(word6);
    210.  
    211.         while(1)
    212.         {
    213.             //if(p10==0) //Determine whether the press P1.0
    214.             {              
    215.                 ReadTemperature();
    216.                 Tempprocess();
    217.                 wrc(0x80);
    218.                 wrn(word1);
    219.                 wrc(0xc0);
    220.                 wrn(word2);
    221.             }
    222.             if(p11==0)//Determine whether the press P1.1
    223.             {
    224.                 wrc(0x80);
    225.                 wrn(word3);
    226.                 wrc(0xc0);
    227.                 wrn(word4);
    228.             }
    229.             if(p12==0)//Determine whether the press P1.2
    230.             {
    231.                 wrc(0x80);
    232.                 wrn(word7);
    233.                 wrc(0xc0);
    234.                 wrn(word8);
    235.             }
    236.         }
    237. }
Loading...