Page 1 of 1

TMR0 interrupt

Posted: Sun Jan 20, 2013 4:56 pm
by mircea2012
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 ?

Re: TMR0 interrupt

Posted: Mon Jan 21, 2013 5:28 pm
by dazz
Hi

Im not very good with c or asm, but if you look at your config screens both internal and rc are ticked, if the oscalator is wrong it wont work

Best to start with switching on an led,if that works then switch it off, again if it works add your delays etc

Regards
Dazz

Re: TMR0 interrupt

Posted: Mon Jan 21, 2013 8:01 pm
by medelec35
Hi mircea2012,

If you would like to post your generated hex file, I can see if configuration settings are OK.
We can then take it from there.

Martin