Reply To: PIC16F877A based CCP compare mode.

Home Forums Microcontrollers PIC Microcontroller PIC16F877A based CCP compare mode. Reply To: PIC16F877A based CCP compare mode.

#13660

mishras1132
Participant

<p>Here is my code:</p><p>#include <htc.h></p><p>#include “LCD.h”</p><p> </p><p>__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_ON </p><p>        & LVP_OFF & CPD_ON & WRT_OFF & CP_ON);    // configuration bits</p><p> </p><p>#define _XTAL_FREQ      4000000  // 4MHZ external crystal</p><p> </p><p>void InitGPIO(void)</p><p>{</p><p>    __delay_ms (10);    // Power up delay</p><p>    TRISCbits.TRISC2 = 1;     // CCP1: Configure as input pin</p><p>    ADCON1 = 0x0F;</p><p>}</p><p> </p><p>void CCPInit(void)</p><p>{</p><p>    CCP1IE = 0;     //Disables the CCP1 interrupt</p><p>    CCP1IF = 0;     //No TMR1 resister capture occurred</p><p>    T1CON  = 0x01;</p><p>    CCP1CON = 0x05; //Capture mode, every rising edge</p><p>}</p><p> </p><p>void main (void)</p><p>{</p><p>    unsigned int start, end, period;</p><p>    float result, frequency;</p><p> </p><p>    InitGPIO();</p><p>    InitLCD();</p><p>    CCPInit();</p><p> </p><p>    lcd_gotoxy(0,4);</p><p>    WriteStringToLCD(“FREQUENCY”);</p><p> </p><p>    while(1)</p><p>    {</p><p>        while(!(CCP1IF));       // Wait first rising edge</p><p>        CCP1IF = 0;             // Clear flag not next round</p><p>        start  = CCPR1L;         // Save value of first rising edge</p><p> </p><p>        while(!(CCP1IF));       // Wait first rising edge</p><p>        CCP1IF = 0;             // Clear flag not next round</p><p>        //CCP1CON = 0x00;         // Disable CCP1 capture module</p><p>        end = CCPR1H;            // Save value of second rising edge</p><p> </p><p>        period = (end – start);</p><p>        period = (float)period;</p><p>        frequency = (1/period);</p><p> </p><p>        lcd_gotoxy(1,4);</p><p>        disp_num(frequency);</p><p>        __delay_ms(1);</p><p>    }</p><p>}</p>

string(7) "9898001"