Shamks412

Temperature sensor code

May 22nd, 2022 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. /*this program is for displaying the temperature on 16*2 lcd display 8051 microcontroller , LM35 sensor and ADC0804*/
  2.  
  3. #include<reg51.h>
  4. sbit rs=P3^7; //Register Select(RS) pin of 16*2 lcd
  5. sbit rw=P3^6; //Read/Write(RW) pin of 16*2 lcd
  6. sbit en=P3^5; //Enable(E) pin of 16*2 lcd
  7.  
  8.  
  9. sbit rd_adc=P3^0; //Read(RD) pin of ADC0804
  10. sbit wr_adc=P3^1; //Write(WR) pin of ADC0804
  11. sbit intr_adc=P3^2; //Interrupt(INTR) pin of ADC0804
  12.  
  13. void delay(unsigned int)  ; //function for creating delay
  14. void cmdwrt(unsigned char); //function for sending commands to 16*2 lcd display
  15. void datawrt(unsigned char); //function for sending to 16*2 lcd display
  16. void convert_display(unsigned char); //function for converting ADC value to temperature and display it on 16*2 lcd display
  17.  
  18. void main(void) //main function
  19. {
  20.    unsigned char i;
  21.    unsigned char cmd[]={0x38,0x01,0x06,0x0c,0x82};//16*2 lcd initialization commands
  22.    unsigned char data1[]="Temperature:";
  23.    unsigned char value;
  24.  
  25.    P1=0xFF; //make Port 1 as input port i.e ADC
  26.    P2=0x00; //make Port 2 as output port i.e LCD
  27.  
  28.    for(i=0;i<5;i++) //send commands to 16*2 lcd display one command at a time
  29.    {
  30.       cmdwrt(cmd[i]); //function call to send commands to 16*2 lcd display
  31.   delay(1);
  32.    }
  33.  
  34.    for(i=0;i<12;i++) //send to 16*2 lcd display one character at a time
  35.    {
  36.       datawrt(data1[i]);  //function call to send to 16*2 lcd display
  37.   delay(1);
  38.    }
  39.  
  40.    intr_adc=1; //make INTR pin as input
  41.    rd_adc=1;    //set RD pin HIGH
  42.    wr_adc=1; //set WR pin LOW
  43.  
  44.    while(1)    //repeat forever
  45.    {
  46.   wr_adc=0; //send LOW to HIGH pulse on WR pin
  47.   delay(1);
  48.   wr_adc=1;
  49.   while(intr_adc==1); //wait for End of Conversion
  50.   rd_adc=0; //make RD = 0 to read the from ADC0804
  51.   value=P1; //copy ADC
  52.   convert_display(value); //function call to convert ADC into temperature and display it on     16*2 lcd display
  53.   delay(1000);  //interval between every cycles
  54.   rd_adc=1;   //make RD = 1 for the next cycle
  55.      
  56.    }
  57.  
  58. }
  59.  
  60. void cmdwrt (unsigned char x)
  61. {
  62.    P2=x;  //send the command to Port 0 on which 16*2 lcd is connected
  63.    rs=0;  //make RS = 0 for command
  64.    rw=0;  //make RW = 0 for write operation
  65.    en=1;  //send a HIGH to LOW pulse on Enable(E) pin to start commandwrite operation
  66.    delay(1);
  67.    en=0;
  68. }
  69. void datawrt (unsigned char y)
  70. {
  71.    P2=y; //send the to Port 2 on which 16*2 lcd is connected
  72.    rs=1; //make RS = 1 for command
  73.    rw=0; //make RW = 0 for write operation
  74.    en=1; //send a HIGH to LOW pulse on Enable(E) pin to start datawrite operation
  75.    delay(1);
  76.    en=0;
  77. }
  78. void convert_display(unsigned char value)
  79. {
  80.   unsigned char x1,x2,x3;
  81.  
  82.   cmdwrt(0xc6);  //command to set the cursor to 6th position of 2nd line on 16*2 lcd
  83.  
  84.   x1=(value/10); //divide the value by 10 and store quotient in variable x1
  85.   x1=x1+(0x30); //convert variable x1 to ascii by adding 0x30
  86.   x2=value%10; //divide the value by 10 and store remainder in variable x2
  87.   x2=x2+(0x30); //convert variable x2 to ascii by adding 0x30
  88.   x3=0xDF; //ascii value of degree(°) symbol
  89.  
  90.   datawrt(x1);  //display temperature on 16*2 lcd display
  91.   datawrt(x2);
  92.   datawrt(x3);
  93.   datawrt('C');
  94. }
  95.  
  96. void delay(unsigned int z)
  97. {
  98.   unsigned int p,q;
  99.   for(p=0;p<z;p++)    //repeat for 'z' times
  100.   {
  101.     for(q=0;q<1375;q++);   //repeat for 1375 times
  102.   }
  103. }
  104.  
Add Comment
Please, Sign In to add comment