Troubleshooting TMR0

Thread Starter

leech20e

Joined Feb 24, 2012
1
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:
Rich (BB code):
#include <p18f4520.h>
#include <string.h>
#include "main.h"
#include "messages.h"

/********************************/
/*************DEFINE*************/
#define TRISDATA            TRISD
#define PORTDATA            PORTD     
#define TRISFUNCTION        TRISC
#define RS                    LATCbits.LATC6     
#define E                     LATCbits.LATC7    
//#define RW NULL

/********************************/
/***********INTERRUPTS***********/
volatile int count = 0;
void low_isr(void);

#pragma code low_vector=0x18
void interrupt_at_low_vector(void)
{
    _asm GOTO low_isr _endasm
}
#pragma code

#pragma interruptlow low_isr
void low_isr (void)
{
    if(INTCONbits.TMR0IF == 1)
    {
        INTCONbits.TMR0IF = 0x00;
        count++;
    }
    else
    {
    }    
}

/********************************/
/**********MAIN SECTION**********/

void main(void)
{
    Initialize();
    TRISA = 0;
    TRISAbits.RA4 = 1;
    Command(0b00000001); //clear display
    Command(0b00111000); //function set
    Command(0b00001111); //display control 
    T0CON = 0b11100000; //Config T0
    INTCONbits.GIE = 1;
    RCONbits.IPEN = 1;
    for(;;)
    {
    Message(Overflow); //Print message to LCD
    Command(0b11000000); //goto second line
    if(count == 1)
    {
    WriteChar('1'); //Write 1 to LCD if the timer EVER overflows
    DLY_S(5);
    }
    else
    {
    Message(Times); //Print message to LCD
    DLY_MS(500);
    Command(0b00000001); //clear display
    }
    }

}
 

ErnieM

Joined Apr 24, 2011
8,377
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"?
 
Top