Advertisement
Guest User

confirm the code

a guest
Feb 19th, 2012
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.72 KB | None | 0 0
  1. /*  --start of comment
  2. * Project name:
  3.      LPG Detector
  4.  
  5. Description:
  6.     A project that detects LPG gas leaks.
  7.  
  8.   Created By:
  9.    Salim
  10.  
  11.   Version 1.2
  12.  01/04/2012
  13.  
  14.   Version 1
  15.  12/17/2011
  16.  
  17.   Test configuration:
  18.      MCU:       PIC16F877
  19.      Oscillator:      8000000 Hz
  20.      SW:        mikroC PRO for PIC
  21.  
  22.   -- end of comment */
  23. // Lcd pinout settings
  24. sbit LCD_RS at RB0_bit;
  25. sbit LCD_EN at RB1_bit;
  26. sbit LCD_D4 at RB2_bit;
  27. sbit LCD_D5 at RB3_bit;
  28. sbit LCD_D6 at RB4_bit;
  29. sbit LCD_D7 at RB5_bit;
  30.  
  31. // Pin direction
  32. sbit LCD_RS_Direction at TRISB0_bit;
  33. sbit LCD_EN_Direction at TRISB1_bit;
  34. sbit LCD_D4_Direction at TRISB2_bit;
  35. sbit LCD_D5_Direction at TRISB3_bit;
  36. sbit LCD_D6_Direction at TRISB4_bit;
  37. sbit LCD_D7_Direction at TRISB5_bit;
  38.  
  39.  
  40. //status LEDs are mapped to sbit variables
  41. sbit GREEN_LED at RE1_bit;
  42. sbit RED_LED at RE2_bit;
  43.  
  44. void MCU_Init();
  45. void Lcd_Init();
  46. void SendMessage();
  47. void CheckGasSensor();
  48. void UpdateLCD();
  49. void UpdateLEDs();
  50. void SolenoidDisableGas();
  51.  
  52.  
  53. //Global sensor status code value
  54. int SENSOR_STATUS_CODE = 0;
  55.  
  56. //Global gas concentration level
  57. double GAS_CONCENTRATION_LEVEL = 0.0;
  58.  
  59.  
  60. void main()
  61. {
  62.   //Set up the MCU
  63.    MCU_Init();
  64.  
  65.   //Set up the ADC
  66.   ADC_Init();
  67.  
  68.   //Set up LCD screen
  69.   Lcd_Init();
  70.   Lcd_Out(1, 1, "LPG Detector");
  71.   Lcd_Cmd(_LCD_CURSOR_OFF);
  72.  
  73.  
  74.   //Program Run loop
  75.   while(1){
  76.     //Check for a sensor reading
  77.     CheckGasSensor();
  78.  
  79.     //Update the text on the LCD Screen
  80.     UpdateLCD();
  81.  
  82.     //Update the Red and Green status LEDs
  83.     UpdateLEDs();
  84.  
  85.     //Shut off the gas if the gas level is dangerous
  86.     if(SENSOR_STATUS_CODE == 2){
  87.       SolenoidDisableGas();
  88.     }
  89.  
  90.     //Send a GSM message if there is an error
  91.     if(SENSOR_STATUS_CODE != 0){
  92.       SendMessage();
  93.     }
  94.  
  95.     //Wait a 1/2 second between updates
  96.     Delay_ms(500);
  97.   }
  98.  
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. void MCU_Init()
  107. {
  108.  ANSEL = 0x08; // Configure AN5 pin as analog input
  109.  C1ON_bit = 0; // Disable comparators
  110.  C2ON_bit = 0;
  111.  
  112.  //GIE = 1;      // Global interrupt is enabled
  113.  
  114.   TRISA = 0;    // PORTA is output
  115.   TRISE = 0xFF; // PORTE is input
  116. }
  117.  
  118.  
  119.  
  120. //Check for a sensor reading
  121. void CheckGasSensor(){
  122.         long int tlong;
  123.         long ch;
  124.         double GAS_CONCENTRATION_LEVEL;
  125.         unsigned int temp;  //This creates a 10 bit number
  126.         temp = ADC_Read(5); // Read the ADC value from ADC Channel 5
  127.  
  128.         Lcd_Out(2,1,"gas Detector");    // Write result in the second line
  129.         tlong = ADC_Read(5)* 5000;         // Convert the result in millivolts
  130.         tlong = tlong / 1023;        // 0..1023 -> 0-5000mV
  131.  
  132.         ch = tlong / 1000;           // Extract volts (thousands of millivolts)
  133.                                      // from result
  134.         Lcd_Chr(2,9,48+ch);          // Write result in ASCII format
  135.         Lcd_Chr_CP('.');
  136.         ch = (tlong / 1000) % 10;     // Extract thousands of millivolts
  137.         Lcd_Chr_CP(48+ch);
  138.         ch = (tlong / 100) % 10;     // Extract hundreds of millivolts
  139.         Lcd_Chr_CP(48+ch);           // Write result in ASCII format
  140.         ch = (tlong / 10) % 10;      // Extract tens of millivolts
  141.         Lcd_Chr_CP(48+ch);           // Write result in ASCII format
  142.         ch = tlong % 10;             // Extract digits for millivolts
  143.         Lcd_Chr_CP(48+ch);           // Write result in ASCII format
  144.         Lcd_Chr_CP('V');
  145.         Delay_ms(1);
  146.         GAS_CONCENTRATION_LEVEL = temp*0.02;   // multiply max ADC value by 0.02 to scale it to 20.48
  147.         if(   (GAS_CONCENTRATION_LEVEL > 100)   &&  (GAS_CONCENTRATION_LEVEL < 300) ){
  148.         //Sets the sensor status code to 1 (warning level)
  149.        //if the gas is between 100 and 300 ppm
  150.     SENSOR_STATUS_CODE = 1;
  151.   }
  152.   else if(GAS_CONCENTRATION_LEVEL > 300){
  153.     //Sets the sensor status code to 2 (danger level)
  154.     //if the gas is above 300 ppm
  155.     SENSOR_STATUS_CODE = 2;
  156.   }
  157.   else{
  158.     //Sets the sensor status code to 0 (okay)
  159.     SENSOR_STATUS_CODE = 0;
  160.   }
  161.  
  162. }
  163.  
  164.  
  165.  
  166.  
  167. //Update the text on the LCD Screen
  168. void UpdateLCD(){
  169.   //Check if there are no errors
  170.   if(SENSOR_STATUS_CODE == 0){
  171.     Lcd_Out(2, 1, "Status: Normal   ");
  172.   }
  173.   else if (SENSOR_STATUS_CODE == 1){
  174.     Lcd_Out(2, 1, "Status:Warning!  ");
  175.   }
  176.   else if (SENSOR_STATUS_CODE == 2){
  177.     Lcd_Out(2, 1, "Status:Gas Off  ");
  178.   }
  179. }
  180.  
  181.  
  182.  
  183. //Update the Red and Green status LEDs
  184. void UpdateLEDs(){
  185.  
  186.   //Check if there are no errors
  187.   if(SENSOR_STATUS_CODE == 0){
  188.     //Status: Normal has the Green LED on
  189.     GREEN_LED = 1;
  190.     RED_LED = 0;
  191.   }
  192.   else if (SENSOR_STATUS_CODE == 1){
  193.     //Status:Warning has the Red LED blinking
  194.     GREEN_LED = 0;
  195.  
  196.     //Toggle Red LED on / off for a blinking effect
  197.     if(RED_LED){
  198.       RED_LED = 0;
  199.     }
  200.     else{
  201.       RED_LED = 1;
  202.     }
  203.  
  204.   }
  205.   else if (SENSOR_STATUS_CODE == 2){
  206.     //Status:Gas Off has the Red LED on solid
  207.     GREEN_LED = 0;
  208.     RED_LED = 1;
  209.   }
  210.  
  211. }
  212.  
  213.  
  214.  
  215. //Shut off the Gas
  216. void SolenoidDisableGas(){
  217.     //Disable the gas by turning on a solenoid
  218.     //choose PORTA and pin RA0 to controls the solenoid
  219.     PORTA.F0 = 1;
  220. }
  221.  
  222.  
  223.  
  224. //Send a GSM message if there is an error
  225. void SendMessage(){
  226.   UART1_Init(9600);
  227.  
  228.   UART1_Write_Text("AT+CMGS=");       //AT+CMGS=
  229.   Delay_ms(1000);
  230.  
  231.   UART1_Write(0x22);            //"
  232.   Delay_ms(2000);
  233.  
  234.   UART1_Write_Text("0060197437650");        // my no
  235.   Delay_ms(2000);
  236.  
  237.   UART1_Write(0x22);
  238.   UART1_Write(0x0D);            //(CR) means Enter
  239.   Delay_ms(2000);
  240.  
  241.   UART1_Write_Text("LPG Leak Concentration > 300ppm");   // status text
  242.   UART1_Write(0x0D);            //(CR) means Enter
  243.   Delay_ms(2000);
  244.  
  245.   UART1_Write(26);             // Ctrl z
  246.   Delay_ms(2000);
  247.  
  248.   UART1_Write(0x0D);           //(CR) means Enter
  249.   Delay_ms(2000);
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement