Smoke detector sensor interface with pic16f877a
// 'C' source line config statements
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 20000000
int analogRead(int a);
void adcconfig();
int main()
{
int result;//digital conversion values
TRISD=0X00;//
adcconfig();
result=analogRead(0);
if(result<120)
{
PORTD=0X00;
}
else
{
PORTD=0Xff;
}
}
void adcconfig()
{
ADCON0=0X00;
ADCON1=0X80;
}
int analogRead(int a)
{
ADCON0=((1<<0)|(a<<3));
__delay_ms(3);
GO=1;
while(GO_DONE==1);
return((ADRESH<<8)|ADRESL);
}