Advertisement
Shamks412

Gas+temperature sensor code

May 28th, 2022
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 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. sbit gas = P3^3;
  8. sbit buzzer = P3^4;
  9.  
  10. sbit rd_adc=P3^0; //Read(RD) pin of ADC0804
  11. sbit wr_adc=P3^1; //Write(WR) pin of ADC0804
  12. sbit intr_adc=P3^2; //Interrupt(INTR) pin of ADC0804
  13. unsigned char y1;
  14.  
  15. void delay(unsigned int)  ; //function for creating delay
  16. void cmdwrt(unsigned char); //function for sending commands to 16*2 lcd display[
  17. void datawrt(unsigned char); //function for sending to 16*2 lcd display
  18. void convert_display(unsigned char); //function for converting ADC value to temperature and display it on 16*2 lcd display
  19. void delaygas(unsigned int);
  20.  
  21. void main(void) //main function
  22. {
  23.    unsigned char i;
  24.    unsigned char cmd[]={0x38,0x01,0x06,0x0c,0x82};//16*2 lcd initialization commands
  25.    unsigned char data1[]="Temparature:";
  26.    unsigned char value;
  27.  
  28.    P1=0xFF; //make Port 1 as input port i.e ADC
  29.    P2=0x00; //make Port 2 as output port i.e LCD
  30.  
  31.    for(i=0;i<5;i++) //send commands to 16*2 lcd display one command at a time
  32.    {
  33.       cmdwrt(cmd[i]); //function call to send commands to 16*2 lcd display
  34.   delay(1);
  35.    }
  36.  
  37.    for(i=0;i<12;i++) //send to 16*2 lcd display one character at a time
  38.    {
  39.       datawrt(data1[i]);  //function call to send to 16*2 lcd display
  40.   delay(1);
  41.    }
  42.  
  43.    intr_adc=1; //make INTR pin as input
  44.    rd_adc=1;    //set RD pin HIGH
  45.    wr_adc=1; //set WR pin LOW
  46.      
  47.    
  48.  
  49.    while(1)    //repeat forever
  50.    {
  51.     if(gas==1 && y1<4)
  52.   {
  53.     buzzer=0;
  54.     delaygas(100);
  55.        
  56.     wr_adc=0; //send LOW to HIGH pulse on WR pin
  57.   delay(1);
  58.   wr_adc=1;
  59.   while(intr_adc==1); //wait for End of Conversion
  60.   rd_adc=0; //make RD = 0 to read the from ADC0804
  61.   value=P1; //copy ADC
  62.   convert_display(value); //function call to convert ADC into temperature and display it on     16*2 lcd display
  63.   delay(100);  //interval between every cycles
  64.   rd_adc=1;   //make RD = 1 for the next cycle
  65.            
  66.   }
  67.     else
  68.     {
  69.         buzzer=1;
  70.          
  71.   wr_adc=0; //send LOW to HIGH pulse on WR pin
  72.   delay(1);
  73.   wr_adc=1;
  74.   while(intr_adc==1); //wait for End of Conversion
  75.   rd_adc=0; //make RD = 0 to read the from ADC0804
  76.   value=P1; //copy ADC
  77.   convert_display(value); //function call to convert ADC into temperature and display it on     16*2 lcd display
  78.   delay(100);  //interval between every cycles
  79.   rd_adc=1;   //make RD = 1 for the next cycle
  80.      
  81.    }
  82.  
  83. }
  84.      }
  85.  
  86. void cmdwrt (unsigned char x)
  87. {
  88.    P2=x;  //send the command to Port 0 on which 16*2 lcd is connected
  89.    rs=0;  //make RS = 0 for command
  90.    rw=0;  //make RW = 0 for write operation
  91.    en=1;  //send a HIGH to LOW pulse on Enable(E) pin to start commandwrite operation
  92.    delay(1);
  93.    en=0;
  94. }
  95. void datawrt (unsigned char y)
  96. {
  97.    P2=y; //send the to Port 0 on which 16*2 lcd is connected
  98.    rs=1; //make RS = 1 for command
  99.    rw=0; //make RW = 0 for write operation
  100.    en=1; //send a HIGH to LOW pulse on Enable(E) pin to start datawrite operation
  101.    delay(1);
  102.    en=0;
  103. }
  104. void convert_display(unsigned char value)
  105. {
  106.   unsigned char x1,x2,x3;
  107.  
  108.   cmdwrt(0xc6);  //command to set the cursor to 6th position of 2nd line on 16*2 lcd
  109.  
  110.   x1=(value/10); //divide the value by 10 and store quotient in variable x1
  111.     y1=x1;
  112.   x1=x1+(0x30); //convert variable x1 to ascii by adding 0x30
  113.   x2=value%10; //divide the value by 10 and store remainder in variable x2
  114.   x2=x2+(0x30); //convert variable x2 to ascii by adding 0x30
  115.   x3=0xDF; //ascii value of degree(°) symbol
  116.  
  117.    
  118.   datawrt(x1);  //display temperature on 16*2 lcd display
  119.   datawrt(x2);
  120.   datawrt(x3);
  121.   datawrt('C');
  122. }
  123.  
  124. void delay(unsigned int z)
  125. {
  126.   unsigned int p,q;
  127.   for(p=0;p<z;p++)    //repeat for 'z' times
  128.   {
  129.     for(q=0;q<1375;q++);   //repeat for 1375 times
  130.   }
  131. }
  132.  
  133. void delaygas(unsigned int nn)
  134. {
  135.  unsigned char ii;
  136.  for(nn;nn>0;nn--)
  137.  {
  138.   for(ii=247;ii>0;ii--);
  139.   for(ii=247;ii>0;ii--);
  140.  }
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement