Qaisar Azeemi
Member
Hi all;
i want to measure the frequency of a sin wave passed through HEF4093BE NAND gate Schmitt trigger to be furnished and this frequency is feeded into pin RA2 of PIC16F877A; and from this pin i want to read the frequency. i made the following code for that purpose but instead of reading the frequency it is counting the numbers with a very high speed i think with the clock speed of my 4MHz crystal. plz analyze my code and tell me the mistake. (NOTE: i don't want to use counter pins).
Here are frequency related parts of my code to make it understandable for you.
where
i want to measure the frequency of a sin wave passed through HEF4093BE NAND gate Schmitt trigger to be furnished and this frequency is feeded into pin RA2 of PIC16F877A; and from this pin i want to read the frequency. i made the following code for that purpose but instead of reading the frequency it is counting the numbers with a very high speed i think with the clock speed of my 4MHz crystal. plz analyze my code and tell me the mistake. (NOTE: i don't want to use counter pins).
Here are frequency related parts of my code to make it understandable for you.
Code:
void interrupt()
{
if(PIR1.TMR1IF==1)
{
frq=1;
}
}
void main()
{
ADCON1=0x06; // adc reg initialization
INTCON=0xA0; // for timer0 ; interrupt reg initilization
TRISA = 0xFF; // port A as input
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
Lcd_Out(1,1,CopyConst2Ram(msg,fv)); // Write text in first row
T1CON.TMR1ON=0;
PIR1.TMR1IF=0;
TMR1L=0XEE; // value loaded for 1 second delay.
TMR1H=0X85;
j==1;
frq=0;
while(j==1)
{
while(G_freq==1)
{G++;}
IntToStr(G,string);
Lcd_Out(2,2,string);
Lcd_Out_CP("Hz");
if(mains==P)
{j=0;}
if(frq==1)
{
k++;
T1CON.TMR1ON=0;
PIR1.TMR1IF=0;
TMR1L=0XEE; // value loaded for 1 second delay.
TMR1H=0X85;
if(k=4)
{
IntToStr(G,string);
Lcd_Out(2,2,string);
Lcd_Out_CP("Hz");
Delay_ms(1000);
G=0;
k=0;
}
T1CON.TMR1ON=1;
frq=0;
}
}
}
Code:
#define genr PORTA.F0
#define mains PORTA.F1
#define G_freq PORTA.F2
const char fv[] = "Frequency Volts";
unsigned char frq=0;
unsigned int j=1,G=0,k=0;