#include <16f877a.h>
#device adc=10
#fuses HS,NOWDT,NOLVP,NOCPD,NODEBUG
#use delay(clock=4M)
float volt,x, oldval, volt1;
int count= 0;
int data,time ;
void main()
{
set_tris_a(0xff);
set_tris_b(0x00);
set_tris_c(0x01);
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);
output_low(PIN_D3);
while(1)
{
//time=delay_ms(2500);
delay_us(20);
read_adc(adc_start_only);
delay_us(100);
volt = read_adc(adc_read_only);
volt = (volt * 5.0)/ 1023.0;
set_timer1(0);
setup_timer_1(T1_EXTERNAL| T1_DIV_BY_1);
delay_ms(x);
setup_timer_1(T1_DISABLED);
data=get_timer1();//getting the value of frequency
x=(1/data); //calaculation of period=x (delay generated corresponding to the measured frequency)
if(volt>1)
{
delay_ms(1);
output_high(PIN_D3);
delay_ms(1500);
}
if((x>500)&&(x<3500)&&(volt>1))
{
output_low(PIN_D3);
delay_ms(1500);
}