Home › Forums › Microcontrollers › PIC Microcontroller › PIC16F877A based CCP compare mode.
Tagged: CCP, Frequency Counter
- This topic has 1 reply, 1 voice, and was last updated 5 years, 11 months ago by mishras1132.
-
AuthorPosts
-
April 13, 2017 at 11:09 am #13659mishras1132Participant
<p>Dear all I am trying to use CCP module of PIC16F877A uC in compare mode, but unfortunately I am not abeling to do it. I have written a small code for it can anyone help me what mistake am I doing in it. Thanks. </p>
April 13, 2017 at 11:12 am #13660mishras1132Participant<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>
-
AuthorPosts
- You must be logged in to reply to this topic.