I am designing a frequency meter with PIC16F877A,I use to read only the period of the signal and decide after:I want to read the period on the LCD after using a push button on C1 of my PIC;
In proteus the circuit is running but,when I put my program in my PIC the system stops.
See my codes below:
#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"
#define ka PIN_C3
#use fast_io(c)
#use fast_io(b)
#use fast_io(a)
void tempdisplay();
void kagaba();
void messagedisplay();
int1 flag;
float voltsf;
float rise1, rise2;
int period;
int i;
unsigned int adc_value, volts;
#int_ccp1
void isr()
{
if (flag==0)
{
rise1 = CCP_1;
flag=1;
}
else if (flag==1)
{
rise2 = CCP_1;
period=(rise2-rise1);
flag=0;
}
}
void tempdisplay()
{
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
adc_value = read_adc();
volts = ((float)(adc_value * 500)/1023.0);
voltsf=(volts*1.8)+32;
}
void main(void)
{
flag=0;
set_tris_c(0xff);
set_tris_b(0x00);
lcd_init();
lcd_gotoxy(1,1);
delay_us(200);
delay_ms(1000);
setup_ccp1(CCP_CAPTURE_RE|CCP_CAPTURE_DIV_16); // Configure CCP1 to capture rise
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // Start timer 1
enable_interrupts(INT_CCP1); // Setup interrupt on risining edge
enable_interrupts(GLOBAL);
while(true)
{
if(input(PIN_C1)==0)
{
output_high(PIN_B4);
if((input(PIN_C1)==0)&&(period==13))
{
tempdisplay();
printf(lcd_putc,"\f%3.0f",voltsf);
printf(lcd_putc,"\fWait...");
delay_ms(500);
printf(lcd_putc,"\f16%%");
output_low(PIN_B4);
break;
}
if((input(PIN_C1)==0)&&(period==15))
{
tempdisplay();
printf(lcd_putc,"\f%3.0f",voltsf);
printf(lcd_putc,"\fWait...");
delay_ms(500);
printf(lcd_putc,"\f14%%");
output_low(PIN_B4);
break;
}
if((input(PIN_C1)==0)&&(period==14))
{
tempdisplay();
printf(lcd_putc,"\f%3.0f",voltsf);
printf(lcd_putc,"\fWait...");
delay_ms(500);
printf(lcd_putc,"\f15%%");
output_low(PIN_B4);
break;
}
if((input(PIN_C1)==0)&&(period==16))
{
tempdisplay();
printf(lcd_putc,"\f%3.0f",voltsf);
printf(lcd_putc,"\fWait...");
delay_ms(500);
printf(lcd_putc,"\f13%%");
output_low(PIN_B4);
break;
}
if((input(PIN_C1)==0)&&(period==17))
{
tempdisplay();
printf(lcd_putc,"\f%3.0f",voltsf);
printf(lcd_putc,"\fWait...");
delay_ms(500);
printf(lcd_putc,"\f12%%");
output_low(PIN_B4);
break;
}
if((input(PIN_C1)==0)&&(period==18))
{
tempdisplay();
printf(lcd_putc,"\f%3.0f",voltsf);
printf(lcd_putc,"\fWait...");
delay_ms(500);
printf(lcd_putc,"\f19%%");
output_low(PIN_B4);
break;
}
if((input(PIN_C1)==0)&&(period==19))
{
tempdisplay();
printf(lcd_putc,"\f%3.0f",voltsf);
printf(lcd_putc,"\fWait...");
delay_ms(500);
printf(lcd_putc,"\f10%%");
output_low(PIN_B4);
break;
}
else
{
output_low(PIN_B4);
printf(lcd_putc,"\fWait...");
delay_ms(500);
printf(lcd_putc,"\fConnect Signal");
delay_ms(500);
break;
}
}
The percentage it the one which gives the level of the input signal,the period of the signal is tested together with the push button.
Thanks