Simple LED with timer on PIC18 C18 Compiler

Thread Starter

skybox

Joined Mar 2, 2009
68
Hi guys,

I can't get this to work. I am using the internal oscillator at 31 kHz and have a prescale value of 1:128 with the 8-bit timer. If I did my math right, this should take about 4 seconds for the timer interrupt flag to recieve a 1 after overflow. If someone can give me tips and/or advice, it would be greatly appreicated!!!

Thanks!

Here is my code:
Rich (BB code):
#include <p18f14k50.h>

//pragma codes
#pragma code high_vector=0x08
#pragma config FOSC = IRC //enable the internal oscillator




//function prototypes
void oscillator(void);
void timer(void);
void high_isr(void);

void interrupt_at_high_vector(void)
{
   _asm goto high_isr _endasm
}

/*******Oscillator function - This function will set the oscillator to 31 kHz***********/
void oscillator(void)
{
//Set oscillator to 31 kHz
OSCCONbits.IRCF2 = 0;
OSCCONbits.IRCF1 = 0;
OSCCONbits.IRCF0 = 0;

//Device clock derived directly from the LFINTOSC internal oscillator
OSCTUNEbits.INTSRC = 0;
}
/*******End Oscillator Function********************************************************/


/*******Timer function - Will start the timer and set pre-scalar to 1:128**********/
void timer(void) 
{
//Pre-scalar value of 1:128. This should give about 4 seconds for the timer0 interrupt flag to go high 
T0CONbits.T0PS2 = 1;
T0CONbits.T0PS1 = 1;
T0CONbits.T0PS0 = 0;

T0CONbits.TMR0ON = 1; //Enables Timer0
}
/*******End Timer Function********************************************************/

void main(void)
{
        INTCONbits.GIE = 1; //enable high priority global interrupts
        RCONbits.IPEN = 1; //enable priority levels on interrupts
        
        INTCONbits.TMR0IF = 0; //clears timer interrupt flag
        INTCONbits.TMR0IE = 1; //enable Timer0 interrupt bit
        T0CONbits.T08BIT = 1; //Set Timer0 as an 8-bit timer
        INTCON2bits.TMR0IP = 1; //Timer0 Interrupt bit set to high priority
        
        TRISC = 0x00; //set all PORTC bits as an output
        
        oscillator();
        timer();
}    

        
//High Interrupt Vector function
#pragma interrupt high_isr            // declare function as high priority isr
void high_isr(void)
{
    if(INTCONbits.TMR0IF = 1)
        {
            LATCbits.LATC0=1 //Turn on LED at RC0
        }
}
 
Top