Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* --start of comment
- * Project name:
- LPG Detector
- Description:
- A project that detects LPG gas leaks.
- Created By:
- Salim
- Version 1.2
- 01/04/2012
- Version 1
- 12/17/2011
- Test configuration:
- MCU: PIC16F877
- Oscillator: 8000000 Hz
- SW: mikroC PRO for PIC
- -- end of comment */
- // Lcd pinout settings
- sbit LCD_RS at RB0_bit;
- sbit LCD_EN at RB1_bit;
- sbit LCD_D4 at RB2_bit;
- sbit LCD_D5 at RB3_bit;
- sbit LCD_D6 at RB4_bit;
- sbit LCD_D7 at RB5_bit;
- // Pin direction
- sbit LCD_RS_Direction at TRISB0_bit;
- sbit LCD_EN_Direction at TRISB1_bit;
- sbit LCD_D4_Direction at TRISB2_bit;
- sbit LCD_D5_Direction at TRISB3_bit;
- sbit LCD_D6_Direction at TRISB4_bit;
- sbit LCD_D7_Direction at TRISB5_bit;
- //status LEDs are mapped to sbit variables
- sbit GREEN_LED at RE1_bit;
- sbit RED_LED at RE2_bit;
- void MCU_Init();
- void Lcd_Init();
- void SendMessage();
- void CheckGasSensor();
- void UpdateLCD();
- void UpdateLEDs();
- void SolenoidDisableGas();
- //Global sensor status code value
- int SENSOR_STATUS_CODE = 0;
- //Global gas concentration level
- double GAS_CONCENTRATION_LEVEL = 0.0;
- void main()
- {
- //Set up the MCU
- MCU_Init();
- //Set up the ADC
- ADC_Init();
- //Set up LCD screen
- Lcd_Init();
- Lcd_Out(1, 1, "LPG Detector");
- Lcd_Cmd(_LCD_CURSOR_OFF);
- //Program Run loop
- while(1){
- //Check for a sensor reading
- CheckGasSensor();
- //Update the text on the LCD Screen
- UpdateLCD();
- //Update the Red and Green status LEDs
- UpdateLEDs();
- //Shut off the gas if the gas level is dangerous
- if(SENSOR_STATUS_CODE == 2){
- SolenoidDisableGas();
- }
- //Send a GSM message if there is an error
- if(SENSOR_STATUS_CODE != 0){
- SendMessage();
- }
- //Wait a 1/2 second between updates
- Delay_ms(500);
- }
- }
- void MCU_Init()
- {
- ANSEL = 0x08; // Configure AN5 pin as analog input
- C1ON_bit = 0; // Disable comparators
- C2ON_bit = 0;
- //GIE = 1; // Global interrupt is enabled
- TRISA = 0; // PORTA is output
- TRISE = 0xFF; // PORTE is input
- }
- //Check for a sensor reading
- void CheckGasSensor(){
- long int tlong;
- long ch;
- double GAS_CONCENTRATION_LEVEL;
- unsigned int temp; //This creates a 10 bit number
- temp = ADC_Read(5); // Read the ADC value from ADC Channel 5
- Lcd_Out(2,1,"gas Detector"); // Write result in the second line
- tlong = ADC_Read(5)* 5000; // Convert the result in millivolts
- tlong = tlong / 1023; // 0..1023 -> 0-5000mV
- ch = tlong / 1000; // Extract volts (thousands of millivolts)
- // from result
- Lcd_Chr(2,9,48+ch); // Write result in ASCII format
- Lcd_Chr_CP('.');
- ch = (tlong / 1000) % 10; // Extract thousands of millivolts
- Lcd_Chr_CP(48+ch);
- ch = (tlong / 100) % 10; // Extract hundreds of millivolts
- Lcd_Chr_CP(48+ch); // Write result in ASCII format
- ch = (tlong / 10) % 10; // Extract tens of millivolts
- Lcd_Chr_CP(48+ch); // Write result in ASCII format
- ch = tlong % 10; // Extract digits for millivolts
- Lcd_Chr_CP(48+ch); // Write result in ASCII format
- Lcd_Chr_CP('V');
- Delay_ms(1);
- GAS_CONCENTRATION_LEVEL = temp*0.02; // multiply max ADC value by 0.02 to scale it to 20.48
- if( (GAS_CONCENTRATION_LEVEL > 100) && (GAS_CONCENTRATION_LEVEL < 300) ){
- //Sets the sensor status code to 1 (warning level)
- //if the gas is between 100 and 300 ppm
- SENSOR_STATUS_CODE = 1;
- }
- else if(GAS_CONCENTRATION_LEVEL > 300){
- //Sets the sensor status code to 2 (danger level)
- //if the gas is above 300 ppm
- SENSOR_STATUS_CODE = 2;
- }
- else{
- //Sets the sensor status code to 0 (okay)
- SENSOR_STATUS_CODE = 0;
- }
- }
- //Update the text on the LCD Screen
- void UpdateLCD(){
- //Check if there are no errors
- if(SENSOR_STATUS_CODE == 0){
- Lcd_Out(2, 1, "Status: Normal ");
- }
- else if (SENSOR_STATUS_CODE == 1){
- Lcd_Out(2, 1, "Status:Warning! ");
- }
- else if (SENSOR_STATUS_CODE == 2){
- Lcd_Out(2, 1, "Status:Gas Off ");
- }
- }
- //Update the Red and Green status LEDs
- void UpdateLEDs(){
- //Check if there are no errors
- if(SENSOR_STATUS_CODE == 0){
- //Status: Normal has the Green LED on
- GREEN_LED = 1;
- RED_LED = 0;
- }
- else if (SENSOR_STATUS_CODE == 1){
- //Status:Warning has the Red LED blinking
- GREEN_LED = 0;
- //Toggle Red LED on / off for a blinking effect
- if(RED_LED){
- RED_LED = 0;
- }
- else{
- RED_LED = 1;
- }
- }
- else if (SENSOR_STATUS_CODE == 2){
- //Status:Gas Off has the Red LED on solid
- GREEN_LED = 0;
- RED_LED = 1;
- }
- }
- //Shut off the Gas
- void SolenoidDisableGas(){
- //Disable the gas by turning on a solenoid
- //choose PORTA and pin RA0 to controls the solenoid
- PORTA.F0 = 1;
- }
- //Send a GSM message if there is an error
- void SendMessage(){
- UART1_Init(9600);
- UART1_Write_Text("AT+CMGS="); //AT+CMGS=
- Delay_ms(1000);
- UART1_Write(0x22); //"
- Delay_ms(2000);
- UART1_Write_Text("0060197437650"); // my no
- Delay_ms(2000);
- UART1_Write(0x22);
- UART1_Write(0x0D); //(CR) means Enter
- Delay_ms(2000);
- UART1_Write_Text("LPG Leak Concentration > 300ppm"); // status text
- UART1_Write(0x0D); //(CR) means Enter
- Delay_ms(2000);
- UART1_Write(26); // Ctrl z
- Delay_ms(2000);
- UART1_Write(0x0D); //(CR) means Enter
- Delay_ms(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement