TMR0 interrupt pic microcontroller

microcontrollerpic

I bought a development board from Matrix Multimedia, HP488(datasheet), but I dont 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)
    {   } 
}

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 ?

Best Answer

To make it works as it stands, in your while(1) routine, you need to keep calling your Delay function.
Or, preferably set up an actual ISR (Interrupt Service Routine - this is a routine the main loop jumps to when the interrupt occurs, then jumps back to where if left off from)

Your compiler manual will have information on setting up an ISR.

Related Topic