#include "18f2520.h"
#fuses H4, NOWDT
#use delay(clock=16000000) // 4Mhz crystal with PLL enabled. 4Mhz * 4 => 16Mhz
#define output_pin PIN_A4
#define LED PIN_C4
#define FLAG_ADDRESS 10
#define FLAG_STATUS_ADDRESS 20
int8 old_f, f = 0;
int16 temp_data;
int32 local;
int8 HIGH;
int8 flag1;
int16 isr_ccp_value;
int16 current_ccp_value;
int32 frequency;
int flag;
#int_ccp1
void ccp1_isr(void)
{
int16 current_ccp;
static int16 old_ccp = 0;
f++;
current_ccp = CCP_1; // Read the 16-bit hardware CCP1 register
isr_ccp_value = current_ccp - old_ccp;
old_ccp = current_ccp; // Save the current ccp value for the next pass.
}
void main()
{
set_tris_a(0x00);
set_tris_c(0x00);
set_timer1(0); // Timer value to 0
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1); // Timer1 internal & 1:1 Prescaler
setup_ccp1(CCP_CAPTURE_RE); // Looks for Rising edge
clear_interrupt(INT_CCP1); // disables the CCP1 interrupt
enable_interrupts(INT_CCP1); // enables the CCP1 Interupt
enable_interrupts(GLOBAL); // Enables global interrupt
HIGH = 1;
while(1)
{
current_ccp_value = isr_ccp_value;
enable_interrupts(GLOBAL); // enable interrupts
while(old_f == f);
old_f = f;
if(f < 64)
{
do
{
temp_data=current_ccp_value;
} while (current_ccp_value!=temp_data);
local += temp_data;
}
else
{
disable_interrupts(GLOBAL);
frequency = (int16)((640 * 393822) / (local));
if((frequency < 2000))
{
output_high(LED);
output_high(output_pin);
flag = 1;
}
flag1 = read_eeprom(FLAG_ADDRESS); // read the value in eeprom address - flag
if((frequency > 2000) && (frequency < 3000) && (flag1 == 1))
{
output_high(LED);
output_high(output_pin);
flag = 1;
}
if((frequency > 3000))
{
output_low(LED);
output_low(output_pin);
flag = 0;
}
if((frequency > 2000) && (frequency < 3000) && (flag == 0))
{
output_low(LED);
output_low(output_pin);
flag = 0;
}
write_eeprom(FLAG_ADDRESS,flag);
local = f=old_f=0;
}
}
}