Troubleshooting TMR0

Discussion in 'Embedded Systems and Microcontrollers' started by leech20e, Feb 24, 2012.

  1. leech20e

    Thread Starter New Member

    Feb 24, 2012
    1
    0
    Hey guys, would greatly appreciate a little bit of help to figure out why my TMR0 isn't working to my desire. First off the general idea of my design is to make a frequency counter. i.e. How many rising edge (positive) pulses happen on T0CKI over a certain period. I am using a PIC18F4520 /w 20MHz HS osc. Of course an LCD as a display. Using MPLAB v8.83 and C18. I've been trying to learn interrupts for a while now. Looking for a few pointers. Right now I basically have it set up to display a 1 on the Lcd when and if the TMR0 overflows. Here is my relevant code:
    Code ( (Unknown Language)):
    1.  
    2. #include <p18f4520.h>
    3. #include <string.h>
    4. #include "main.h"
    5. #include "messages.h"
    6.  
    7. /********************************/
    8. /*************DEFINE*************/
    9. #define TRISDATA            TRISD
    10. #define PORTDATA            PORTD    
    11. #define TRISFUNCTION        TRISC
    12. #define RS                    LATCbits.LATC6    
    13. #define E                     LATCbits.LATC7    
    14. //#define RW NULL
    15.  
    16. /********************************/
    17. /***********INTERRUPTS***********/
    18. volatile int count = 0;
    19. void low_isr(void);
    20.  
    21. #pragma code low_vector=0x18
    22. void interrupt_at_low_vector(void)
    23. {
    24.     _asm GOTO low_isr _endasm
    25. }
    26. #pragma code
    27.  
    28. #pragma interruptlow low_isr
    29. void low_isr (void)
    30. {
    31.     if(INTCONbits.TMR0IF == 1)
    32.     {
    33.         INTCONbits.TMR0IF = 0x00;
    34.         count++;
    35.     }
    36.     else
    37.     {
    38.     }    
    39. }
    40.  
    41. /********************************/
    42. /**********MAIN SECTION**********/
    43.  
    44. void main(void)
    45. {
    46.     Initialize();
    47.     TRISA = 0;
    48.     TRISAbits.RA4 = 1;
    49.     Command(0b00000001); //clear display
    50.     Command(0b00111000); //function set
    51.     Command(0b00001111); //display control
    52.     T0CON = 0b11100000; //Config T0
    53.     INTCONbits.GIE = 1;
    54.     RCONbits.IPEN = 1;
    55.     for(;;)
    56.     {
    57.     Message(Overflow); //Print message to LCD
    58.     Command(0b11000000); //goto second line
    59.     if(count == 1)
    60.     {
    61.     WriteChar('1'); //Write 1 to LCD if the timer EVER overflows
    62.     DLY_S(5);
    63.     }
    64.     else
    65.     {
    66.     Message(Times); //Print message to LCD
    67.     DLY_MS(500);
    68.     Command(0b00000001); //clear display
    69.     }
    70.     }
    71.  
    72. }
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Do you need to set INTCONbits, TMR0IE?
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    It looks to me like you have a race set up here. You print to the display ONLY when count == 1, perhaps the ISR is firing multiple times during your half second delay so count == 1 is a very rare event indeed. Perhaps try just if (count != 0) to change your display.

    Have you tested your display code yet? Can you make it say "HELLO WORLD"?
     
Loading...