• AVR Freaks

Hot!how to use the compare section CCP module of the PIC16F877A

Author
abhi143
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2018/05/20 09:06:52
  • Location: 0
  • Status: offline
2019/05/18 23:55:07 (permalink)
0

how to use the compare section CCP module of the PIC16F877A

I want to compared The 16-bit value of CCPR1  against the 16-bit value of TMR1. when there is a compare match of CCPR1 and TMR1 and  CCP1IF will be set when it set  toggle LED each interrupt flag
 
My code 
#include <xc.h>
 
#pragma config FOSC = XT  // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF  // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON  // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = OFF  // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF  // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF  // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF  // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF  // Flash Program Memory Code Protection bit (Code protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#define _XTAL_FREQ 20000000

int main (void)

{
    TRISC0 = 1;
    TRISD4 = 0;
 
 // TIMER1 is configured as a 16 bit counter
    T1CON = 0b00000011 ;  // 1:1 prescale, OSC disabled, Input Synchronized, External clock on T1CKI(RC0), Timer ON
 
 // CAPTURE/COMPARE mode is configured to count 0->14000
 // Compare mode using CCPR1H and CCPR1L as a 16 bit value
  CCP1CON = 0b00001011;   
  CCPR1H = 0x36;
  CCPR1L = 0xB0;
 
  CCP1IF = 0;

  while(1){
 
  if(CCP1IF == 1)  // if the CCP1 Interrupt flag is set
  {
     CCP1IF = 0;
    
     RD4 = ~RD4;  // toggle LED each interrupt flag
 
  }
}
}

 
LED doesn't toggle ? what could be wrong in program because there is nothing wrong in connection  ?
#1

6 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: how to use the compare section CCP module of the PIC16F877A 2019/05/19 00:36:47 (permalink)
    +1 (1)

        T1CON = 0b00000011 ;  // 1:1 prescale, OSC disabled, Input Synchronized, External clock on T1CKI(RC0), Timer ON

     Are you in fact driving RC0 with an external clock?
     

    Nearly there...
    #2
    abhi143
    Starting Member
    • Total Posts : 66
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: offline
    Re: how to use the compare section CCP module of the PIC16F877A 2019/05/19 03:25:44 (permalink)
    0
    qhb

        T1CON = 0b00000011 ;  // 1:1 prescale, OSC disabled, Input Synchronized, External clock on T1CKI(RC0), Timer ON

     Are you in fact driving RC0 with an external clock?
     


    yes but if I set internal clock still LED doesn't toggle
    #3
    DeutcheN
    Super Member
    • Total Posts : 110
    • Reward points : 0
    • Joined: 2010/09/01 12:35:40
    • Location: DM43BH
    • Status: offline
    Re: how to use the compare section CCP module of the PIC16F877A 2019/05/20 09:59:29 (permalink)
    0
    Try setting the T1OSCEN bit in T1CON.
    #4
    abhi143
    Starting Member
    • Total Posts : 66
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: offline
    Re: how to use the compare section CCP module of the PIC16F877A 2019/05/22 01:15:56 (permalink)
    0
    Aleman
    Try setting the T1OSCEN bit in T1CON.


    done but led doesn't change state
      T1CON = 0b00001011 ; 

    bit 7-6 Unimplemented: Read as ‘0’
    bit 5-4 T1CKPS1:T1CKPS0: Timer1 Input Clock Prescale Select bits
    11 = 1:8 prescale value
    10 = 1:4 prescale value
    01 = 1:2 prescale value
    00 = 1:1 prescale value
    bit 3 T1OSCEN: Timer1 Oscillator Enable Control bit
    1 = Oscillator is enabled
    0 = Oscillator is shut-off (the oscillator inverter is turned off to eliminate power drain)
    bit 2 T1SYNC: Timer1 External Clock Input Synchronization Control bit
    When TMR1CS = 1:
    1 = Do not synchronize external clock input
    0 = Synchronize external clock input
    When TMR1CS = 0:
    This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0.
    bit 1 TMR1CS: Timer1 Clock Source Select bit
    1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
    0 = Internal clock (FOSC/4)
    bit 0 TMR1ON: Timer1 On bit
    1 = Enables Timer1
    0 = Stops Timer1
    Legend
    post edited by abhi143 - 2019/05/22 01:20:03
    #5
    abhi143
    Starting Member
    • Total Posts : 66
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: offline
    Re: how to use the compare section CCP module of the PIC16F877A 2019/05/25 04:45:40 (permalink)
    0
    Is there any suggestion
    #6
    mpgmike
    Super Member
    • Total Posts : 212
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: online
    Re: how to use the compare section CCP module of the PIC16F877A 2019/05/26 10:03:25 (permalink)
    0
    Since you are polling the CCPIF, you have CCP1CON set up to reflect the output on the PORT.PIN.  Try CCP1CON = 0b00001010;.

    I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
    #7
    Jump to:
    © 2019 APG vNext Commercial Version 4.5