TMR0 interrupt
Posted: Sun Jan 20, 2013 4:56 pm
Hi all.
I am a beginner in Pic microcontroller. I bought a development board from Matrix Multimedia, HP488, but I don't know why I fail with a small program.
The program must flash a led with a delay (4 sec) using TMR0.
So, from my calculations, using formula :
Freq. out = Freq. osc / [prescaler * (256 - TMR0) * count]
The prescaler is 1 : 256; TMR0 is 0, anf the frequency oscilator is 4Mhz (osccon = 0b01100000)
An instruction is executed in 4 clock cycles. So the frequency clock is 4Mhz / 4 = 1Mhz.
count = 61.
I use sourceBoost IDE, and the compiler is BoostC.
Look my code :
#include<system.h>
unsigned int counter = 0;
void Delay(void)
{
if(intcon & 2) // check if TMR0IF is set
{
clear_bit( intcon, 2 ); // if TMR0IF is set, clear this bit
counter = counter + 1; // increments the counter
if(counter == 61)
{
counter = 0;
portb = ~portb; // flash the Led 0 of Port B
}
}
}
void main(void)
{
trisb = 0x00; // set all pins of Port B as output
portb = 0x01; // RB0 is high
tmr0 = 0; // the value of TMR0 register is zero.
cmcon = 0x07; // comparators is off.
option_reg = 0b00000111; // prescaler is assigned to the WDT, prescaler 1:256
intcon = 0b10100000; // GIE - enable, TMR0IE - enable
while(1)
{ }
}
enter code here
The program compiles fine, upload fine, but dont show me the led flashing.
What I am doing wrong ? Are there any configuration settings that I am not aware of ?
I am a beginner in Pic microcontroller. I bought a development board from Matrix Multimedia, HP488, but I don't know why I fail with a small program.
The program must flash a led with a delay (4 sec) using TMR0.
So, from my calculations, using formula :
Freq. out = Freq. osc / [prescaler * (256 - TMR0) * count]
The prescaler is 1 : 256; TMR0 is 0, anf the frequency oscilator is 4Mhz (osccon = 0b01100000)
An instruction is executed in 4 clock cycles. So the frequency clock is 4Mhz / 4 = 1Mhz.
count = 61.
I use sourceBoost IDE, and the compiler is BoostC.
Look my code :
#include<system.h>
unsigned int counter = 0;
void Delay(void)
{
if(intcon & 2) // check if TMR0IF is set
{
clear_bit( intcon, 2 ); // if TMR0IF is set, clear this bit
counter = counter + 1; // increments the counter
if(counter == 61)
{
counter = 0;
portb = ~portb; // flash the Led 0 of Port B
}
}
}
void main(void)
{
trisb = 0x00; // set all pins of Port B as output
portb = 0x01; // RB0 is high
tmr0 = 0; // the value of TMR0 register is zero.
cmcon = 0x07; // comparators is off.
option_reg = 0b00000111; // prescaler is assigned to the WDT, prescaler 1:256
intcon = 0b10100000; // GIE - enable, TMR0IE - enable
while(1)
{ }
}
enter code here
The program compiles fine, upload fine, but dont show me the led flashing.
What I am doing wrong ? Are there any configuration settings that I am not aware of ?