PIC C18 Timer Interrupt Problems

Discussion in 'Embedded Systems and Microcontrollers' started by crazyengineer, Oct 19, 2012.

  1. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    Hello again. I'm doing a timer interrupt example using the PIC. Right now I have it configured to turn on and off the LED every 500ms. I'm using the PIC18F2420 on a 20Mhz crystal connected to 22pf capacitors. However, it seems like it's either not activating the interrupt or its taking a long time to do so. Here's the code

    Code ( (Unknown Language)):
    1.  
    2. //===============================================================================================
    3. //  Includes, Defines, and Global Variables
    4. //===============================================================================================
    5. /*  Includes */
    6. #include <p18F2420.h>
    7. #include <delays.h>
    8. #include <portb.h>
    9. #include <usart.h>
    10. #include <stdio.h>
    11. #include <stdlib.h>
    12. #include <timers.h>
    13. /*  PIC Configuratings */
    14. #pragma config OSC = HS
    15. #pragma config WDT = OFF
    16. #pragma config PBADEN = OFF
    17. #pragma config LVP = OFF
    18. #pragma config PWRT = OFF
    19. #pragma config DEBUG= ON
    20. #pragma config BOREN = OFF
    21.  
    22. int counter=0;
    23. //===============================================================================================
    24. //
    25. //===============================================================================================
    26.  
    27.  
    28.  
    29.  
    30. //===============================================================================================
    31. //  Function Prototypes
    32. //===============================================================================================
    33. void PIC_CONFIG(void);
    34. //===============================================================================================
    35. //
    36. //===============================================================================================
    37.  
    38.  
    39.  
    40.  
    41. //===============================================================================================
    42. //  Interrupt Handler
    43. //===============================================================================================
    44. #pragma interrupt ISR
    45. void ISR (void)                                     //  Main Interrupt handler
    46. {
    47.     if(INTCONbits.TMR0IF==1)
    48.     {
    49.         INTCONbits.TMR0IF=0;
    50.         PORTCbits.RC4=~(PORTCbits.RC4);
    51.         WriteTimer0(0xD9DA);
    52.     }
    53.        
    54. }
    55.  
    56.  
    57. #pragma code InterruptVectorHigh = 0x08             //  This function simply jumps to the ISR code shown above.
    58. void InterruptVectorHigh (void)
    59. {
    60.     _asm
    61.     goto ISR //jump to interrupt routine
    62.     _endasm
    63. }
    64. #pragma code
    65. //===============================================================================================
    66. //
    67. //===============================================================================================
    68.  
    69.  
    70.  
    71.  
    72. //===============================================================================================
    73. //  Main Loop
    74. //===============================================================================================
    75. void main(void)
    76. {
    77. PIC_CONFIG();
    78.     while(1);
    79.  
    80. }
    81. //===============================================================================================
    82. //
    83. //===============================================================================================
    84.  
    85.  
    86.  
    87.  
    88. //===============================================================================================
    89. //  PIC Configuration
    90. //===============================================================================================
    91. void PIC_CONFIG(void)
    92. {
    93.     /*  Setting PIC to use interrupts   */
    94.     RCONbits.IPEN=1;                    //  Enable interrupt priority
    95.     //  Setup Timer 0 for interrupts
    96.     INTCONbits.TMR0IE=1;
    97.     INTCONbits.TMR0IF=0;
    98.     INTCON2bits.TMR0IP=1;
    99.     OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_EXT & T0_EDGE_FALL & T0_PS_1_256);
    100.     WriteTimer0(0xD9DA);
    101.     INTCONbits.GIE=1;                   //  Enable global interrupts
    102.     TRISC=0x00;                         //  Configure all PORTC pins as outputs
    103.     PORTC=0x00;                         //  Turns off all PORTC ports
    104. }
    105.  
    106.  
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    What have you done to debug this?
     
  3. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    I used THE pickit3 in MPLAB
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Can you elaborate on which registers or variables you did use a watch function on. And how you used any breakpoints
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    Do you have the external clock running on T13CKI? That is what you are saying to use when you call for T0_SOURCE_EXT in OpenTimer0().

    From the fine manual:
    T0_SOURCE_EXT: Transition on TxCKI pin acts as source of clock


    If so, drop a breakpoint on the "if(INTCONbits.TMR0IF==1)" line of the ISR and run the program. Go for coffee. Come back and see if the breakpoint was hit.


    If not, you have another set-up problem where the interrupt is not set.

    As you can set a stimulus clock input on that pin MPLAB sim would also catch this problem without any hardware at all.
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    Those are the tools you are using not the procedure you used to debug.

    I am having the exact same issue as you. This is how I plan to debug.

    1. Set breakpoint in ISR and see if the breakpoint gets called.
    2. If it works then check my code and datasheet again.
    3. If it fails switch to internal clock for the timer.
    4. If it works then check my xtal and cap connections. Go back to step 1,
    5. If it fails check my code and datasheet again.

    Don't ASSUME that the openTimer function is working. Check microchip's code too.
     
  7. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    You mean do I have an external oscillator running on RA7 and RA6?
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    No.

    When I ask "Do you have the external clock running on T13CKI?" I am asking if you have an external (not internal, separate and distinct from the PIC) clock (multivibrator or such square or pulse wave source) connected to pin 11, also known as the T13CKI input pin to the Timer 1.

    I ask because THAT is the clock source you picked in your code to drive Timer 1.
     
Loading...