Sujit Mishra
New Member
Dear sir, I am trying to use CCP module of PIC16F877A uC in capture mode too read input frequency. I have written a small code for it but unfortunately its not working. Can anyone guide me what mistake am I making in it. Below is my code, Thanks.
Code:
#include <htc.h>
#include "LCD.h"
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_ON
& LVP_OFF & CPD_ON & WRT_OFF & CP_ON); // configuration bits
#define _XTAL_FREQ 4000000 // 4MHZ external crystal
void InitGPIO(void)
{
__delay_ms (10); // Power up delay
TRISCbits.TRISC2 = 1; // CCP1: Configure as input pin
ADCON1 = 0x0F;
}
void CCPInit(void)
{
CCP1IE = 0; //Disables the CCP1 interrupt
CCP1IF = 0; //No TMR1 resister capture occurred
T1CON = 0x01;
CCP1CON = 0x05; //Capture mode, every rising edge
}
void main (void)
{
unsigned int start, end, period;
float result, frequency;
InitGPIO();
InitLCD();
CCPInit();
lcd_gotoxy(0,4);
WriteStringToLCD("FREQUENCY");
while(1)
{
while(!(CCP1IF)); // Wait first rising edge
CCP1IF = 0; // Clear flag not next round
start = CCPR1L; // Save value of first rising edge
while(!(CCP1IF)); // Wait first rising edge
CCP1IF = 0; // Clear flag not next round
//CCP1CON = 0x00; // Disable CCP1 capture module
end = CCPR1H; // Save value of second rising edge
period = (end - start);
period = (float)period;
frequency = (1/period);
lcd_gotoxy(1,4);
disp_num(frequency);
__delay_ms(1);
}
}
Attachments
Last edited by a moderator: