CCP compare module interrupt not triggering -pic16f887 - XC8

Discussion in 'Embedded Systems and Microcontrollers' started by nitz49, Mar 10, 2016.

  1. nitz49

    Thread Starter New Member

    Mar 9, 2016
    1
    0
    I am using pic16f887 compiler xc8 to count 20 pulses from external source by using Timer1 and compare module and on compare match, a software interrupt will enable to toggle RA6 pin.
    i am unable to execute this code and interrupt is not triggering.

    Code (C):
    1.  
    2. [SIZE=3]#include <xc.h>
    3.  
    4. #define _XTAL_FREQ 4000000
    5. #define LED1 RA0
    6.  
    7. // #pragma config statements should precede project file includes.
    8. // Use project enums instead of #define for ON and OFF.
    9. // CONFIG1
    10. #pragma config FOSC = INTRC_NOCLKOUT// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
    11. #pragma config WDTE = OFF  // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
    12. #pragma config PWRTE = OFF  // Power-up Timer Enable bit (PWRT disabled)
    13. #pragma config MCLRE = ON  // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
    14. #pragma config CP = OFF  // Code Protection bit (Program memory code protection is disabled)
    15. #pragma config CPD = OFF  // Data Code Protection bit (Data memory code protection is disabled)
    16. #pragma config BOREN = OFF  // Brown Out Reset Selection bits (BOR disabled)
    17. #pragma config IESO = OFF  // Internal External Switchover bit (Internal/External Switchover mode is disabled)
    18. #pragma config FCMEN = OFF  // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
    19. #pragma config LVP = OFF  // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
    20.  
    21. // CONFIG2
    22. #pragma config BOR4V = BOR40V  // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
    23. #pragma config WRT = OFF  // Flash Program Memory Self Write Enable bits (Write protection off)
    24.  
    25. void main(void){
    26.   TRISC0=1;  //input pin external Pulse
    27.   TRISC2=0;
    28.   TRISA0=0;  // LED1
    29.   TRISA6=0;  // output pin
    30.  
    31.   RC0=0;
    32.   RC2=0;
    33.   LED1=0;
    34.   RA6=0;
    35.  
    36.   CCP2CON=0x0A;
    37.   T1CON=0x02;
    38.   CCP2IE=1;
    39.   GIE=1;
    40.   PEIE=1;
    41.   CCPR2L=20;
    42.   CCPR2H=0;
    43.   TMR1H=0;
    44.   TMR1L=0;
    45.  
    46.   while(1) {
    47.   CCP2IF=0;
    48.   TMR1ON=1;
    49.   while(CCP2IF==0); // On match when CCP1IF=1, generate software interrupt
    50.   }
    51.  
    52. }
    53.  
    54. void interrupt compare_isr(void)
    55. {
    56.   if(CCP2IF==1){
    57.   TMR1ON=0;
    58.   TMR1H=0;
    59.   TMR1L=0;
    60.   CCP2IF=0;
    61.   LED1=1;
    62.   RA6=~RA6;
    63.   LED1=0;
    64.   }
    65. }[/SIZE]
    Mod edit: used CODE tags for readable code - removed excessive line breaks.
     
    Last edited by a moderator: Mar 10, 2016
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    1,835
    367
    Read the data sheet, particularly the portion about putting the pins into gpio mode.
     
  3. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    A few things:
    You are clearing CCP2IF in a 'while' loop. CCP2IF should only be cleared in the interrupt routine when the interrupt is serviced. The way you have written the interrupt code, the work is done there. Remove this from main - that is not a software interrupt. Replace it with a while(1) to hold the code there while the interrupt does the work.
    Code (C):
    1. while(1) {
    2.   CCP2IF=0;
    3.   TMR1ON=1;
    4.   while(CCP2IF==0); // On match when CCP1IF=1, generate software interrupt
    Enable interrupts only after configuring the CCP.
    Visit the I/O port descriptions in the datasheet, particularly ANSEL.
    If you are going to count transitions like this, be sure the switch is debounced and makes clean transitions from low to high for each press of the button.
    You should always initialize unused ports to output 0.

    What textbook are you using?

    Good luck.
     
Loading...