M3GAPL3X
Junior Member level 3
timer0 overflow
Hi guys,
I am trying to make a simple Timer0 program where LED's light off and than on after 1 second. I am using a 8 bit timer, 31 kHz internal LPINSRC clock with a 1:30 pre-scaler. The LED's are not toggling off and than on and than off again. They are just going from off to on almost instanteously.
Here is my code:
Any help would be appreciated
Hi guys,
I am trying to make a simple Timer0 program where LED's light off and than on after 1 second. I am using a 8 bit timer, 31 kHz internal LPINSRC clock with a 1:30 pre-scaler. The LED's are not toggling off and than on and than off again. They are just going from off to on almost instanteously.
Here is my code:
Code:
//LED will blink on and off every 1 second without interrupts
#include <p18f14k50.h>
//pragma codes
#pragma config FOSC = IRC //enable Internal Oscillator
//function prototypes
void init(void);
void main()
{
TRISC = 0x00; //set PORTC's as outputs
LATC = 0x00;
init();
while(1)
{
//if overflow flag is set for timer0, light up LED, reset the overflow flag bit and reset TMR0L low byte to 0x05
if (INTCONbits.TMR0IF == 1)
{
LATC = 0xff;
// INTCONbits.TMR0IF = 0;
TMR0L = 0x05;
}
}
}
//Function to initalize oscillator registers
void init(void)
{
OSCCONbits.SCS1 = 1;
//enable 31 kHz kHz clock
OSCCONbits.IRCF2 = 0;
OSCCONbits.IRCF1 = 0;
OSCCONbits.IRCF0 = 0;
OSCCONbits.OSTS = 0; //device is running from internal oscillator
OSCTUNEbits.INTSRC = 0; //31 kHz device is derived directly from LFINTOSC
//configure Timer0
T0CONbits.T08BIT = 1; //Timer0 configured as an 8 bit register
//Internal Instruction Cycle Clocka
T0CONbits.T0CS = 0;
T0CONbits.PSA = 0;
//configure prescale value of 1:32
T0CONbits.T0PS2 = 0;
T0CONbits.T0PS1 = 1;
T0CONbits.T0PS0 = 0;
//load TMR0L with a value of 05
TMR0L = 0x05;
INTCONbits.TMR0IF = 0;
//enable TMR0 after enabling all related registers
T0CONbits.TMR0ON = 1;
}
/****************NOTES*************
With a 31 kHz clock, frequency is 31 kHz / 4 = about 8 kHz. 8 kHz corresponds
to .125 ms per instruction so 8000 instructions per second. With a 32 bit pre-scalar,
instructions per ms is 4. Therefore, about 250 counts per second. An 8-bit timer can hold
a max value fo 256, so load TMR0L with 0x05.
*/
Any help would be appreciated