Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Fix error for programming code

Status
Not open for further replies.

salim alfarsi

New Member
Hi,i have project and i am having difficulties in the programming code.i explain briefly to u my circuit. gas sensor send analog voltage to PIC16F887 microcontroller and MC Display the reading on LCD. in same time the MC will close or shut off solenoid valve to isolate the gas leak. after that MC also will send signal to GSM modem to send SMS to the concerned person informing him by the gas leak.View attachment 60737


and this is my code:
Code:
/*  --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
sqqqbit 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 = 0xFF; // PORTA is input
  TRISE = 0; // PORTE is output
}



//Check for a sensor reading
void CheckGasSensor(){  
  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,text);           // Write result in the second line
        tlong = (long)adc_rd * 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 = (double)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("0060179291471");        // 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);
}
 
Last edited by a moderator:
You could tell us what the problem is.
However having had a quick glance,
1. In the main loop..
//Send a GSM message if there is an error
if(SENSOR_STATUS_CODE != 0){
SendMessage();

This will constantly try and send a text message via the GSM modem, probably overloading the input buffers. You need to send once and do something to prevent it being constantly resent once an error has occurred.

2. In void CheckGasSensor() what is "adc_rd"? It's undefined. Do you mean "temp" as this is declared and not used in the function.

3. In void CheckGasSensor() you do not check for the value of 300. Over and under yes but not exactly 300.

Just my opinion after a few mins.

WTP
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top