Light and Gas Sensor Interfacing with PIC18F4550

Updated on 2017/11/20 12:57

Syllabus

  • Light and Gas Sensor Interfacing with PIC18F4550

Light and Gas Sensor Interfacing with PIC18F4550

Video Proteus Simulation


Algorithm for Interfacing

Interfacing Diagram

light-and-gas-sensor-interfacing-with-pic18f.png

Embedded C code for the interfacing

Question:-Draw and explain interfacing of Light and Gas Sensor  with PIC18FXXX microcontroller and also write an Embedded C program to .

i] Turn Off LED if Digital voltage value is between 300 to 800

ii] Buzzer should sound if Gas sensor pin status is High

#include<P18F4550.h>
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void msdelay(unsigned int itime);

#define ldata PORTD
#define rs PORTEbits.RE0
#define rw PORTEbits.RE1
#define en PORTEbits.RE2
#define buzzer PORTCbits.RC1
#define led PORTCbits.RC0
#define GAS PORTBbits.RB6
                                                                  
void main(void)
{
unsigned int i, d;
unsigned char temp[3];
unsigned char l;
unsigned int h,t;
TRISD=0;
PORTD=0;
TRISE=0x00;
PORTE=0;
TRISCbits.RC0 = 0; // led
TRISCbits.RC1 = 0;  // buzzer
TRISBbits.RB6 = 1;  // mq-2
ADCON0 = 0X01;     //CHANNEL AN0 as input of ADC and ADC ON
ADCON1 = 0X0E;     // ONLY AN0 pin as analog remaining pins as Digital Input Output
ADCON2=0b10001010;  //Result Right justified, A/D Acquisition time 2TAD,Clock frequency=fosc/32
msdelay(15);
lcdcmd(0x38);
msdelay(15);
lcdcmd(0x0E);
msdelay(15);
lcdcmd(0x01);
msdelay(15);
lcdcmd(0x06);
msdelay(15);
while(1)
{
if((GAS == 1))
{
buzzer = 1;
}
else
{
buzzer = 0;
}
lcdcmd(0x81);
msdelay(20);
ADCON0bits.GO = 1;
while(!ADCON0bits.DONE ==1);
l = ADRESL;
h = ADRESH;
h = h << 8;
t = h | l;
if (t > 300 && t <800)
{
led=0;
}
else
{
led=1;
}
temp[0]= (ADRESH & 0x0f);
temp[1]= (ADRESL & 0xf0)>>4;
temp[2]= (ADRESL & 0x0f);
for(d=0; d<3; d++)
{
if (temp[d] < 10)
temp[d] = temp[d]+0x30;
else
temp[d] = temp[d]+0x37;
lcddata(temp[d]);
msdelay(15);
}
msdelay(10);
}
}

void lcdcmd (unsigned char value)
{
ldata=value;
rs=0;
rw=0;
en=1;
msdelay(1);
en=0;
}
void lcddata (unsigned char value)
{
ldata=value;
rs=1;
rw=0;
en=1;
msdelay(1);
en=0;
}
void msdelay (unsigned int itime)
{
int i,j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}

References

  • Created and developed by Prof. S.M.Wagh, SKNCOE  Pune
  • Interfacing diagram by Jayesh Gopal, Student, SKNCOE,Pune
  • WikiNote Foundation
Tags:
Created by Sujit Wagh on 2017/09/16 20:50