TMR0 interrupt

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

mircea2012
Posts: 3
Joined: Thu Jan 10, 2013 3:53 pm
Contact:

TMR0 interrupt

Postby mircea2012 » 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 ?
Attachments
Untitled.png
(113.45 KiB) Downloaded 3225 times

dazz
Posts: 1314
Joined: Sun Jan 15, 2012 3:15 pm
Has thanked: 514 times
Been thanked: 467 times
Contact:

Re: TMR0 interrupt

Postby dazz » Mon Jan 21, 2013 5:28 pm

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
To sign up to the V5 forum follow this link http://www.matrixmultimedia.com/forum_upgrades.php

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8440
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2412 times
Been thanked: 3450 times
Contact:

Re: TMR0 interrupt

Postby medelec35 » Mon Jan 21, 2013 8:01 pm

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
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.