#include "16f877a.h"
#device adc=10
#fuses HS,NOWDT,NOLVP,NOCPD,NODEBUG
#use delay(clock=4M)
float volt, oldval,volt1 ;
int count=0
#int_timer2
void timer2_isr(void)
{
//clock=clock++;
x=get_timer2();
set_timer2(0);
}
void main()
{
set_tris_a(0xff);
set_tris_b(0x00);
set_tris_c(0x00);
set_tris_d(0x00);
//Analog module configuration for reading the voltage from the PIN_AN0
setup_comparator(NC_NC_NC_NC);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0);
set_adc_channel(0);
setup_vref(FALSE);
//Interrupt uses for counting 3 seconds
setup_timer_2(T0_INTERNAL | T0_DIV_BY_8);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(1)
{
delay_us(20);
read_adc(adc_start_only);
delay_us(100);
volt = read_adc(adc_read_only);
volt = volt * 30.0/ 1023.0;
if(volt>1)
{
delay_ms(3);
output_high(PIN_D0);
}
else if((x<=125)&&(volt>1) //turn off the led on D1
{
output_high(PIN_D0);
}
}
}