This web site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more.

Interrupt with Timer0

Last update: 31/07/2015

An interrupt (using the internal timer) can be considered a simple way to enable the execution of a routine of the program at fixed times. Such feature is really important while, for instance, there is a need to enable a display at fixed times or in any case there is a need to enable/disable something during the execution of the program of the picmicro.

Naturally, in these cases, we have to set properly one of the timers available and enable the interrupt function of the picmicro.  Such a possibility is managed by two different registers: INTCON and OPTION_REG.

OPTION_REG is an 8 bits register used to control the timer0 and the prescaler, to set an external interrupt and even to enable/disable the weak pull-up resistors of the PORT:

option reg interrupt

Bit7: enable (0) or disable (1) the weak pull up resistors
Bit6: edge Select: rising (1) or falling (0). See picture on the right;
Bit5: external pulse (on RA4) (if 1) or internal Fosc/4 (if 0);
Bit4: tmr0 increment on high-to-low (if 1) or increment on low-to-high (if 0);
Bit3: assignment of the prescaler. To the Watctdogtimer (if 1) ot to timer0 (if 1);
Bits 2,1,0: Set the prescaler.
  fronte salita

 
The other register to set regards the interrupt control register. It must be used to set properly the interrupt of the picmicro; where:

intcon

Bit7: Enable (1)/ disable (0) the interrupts
Bit6: Enable (1)/disable (0) the peripheral interrupts
Bit5: Enable (1)/disable (0) the TMR0 interrupt
Bit4: Enable (1)/disable (0) the RB0/INT  interrupt
Bit3: Enable (1)/disable (0) the PORTB change interrupts
Bit2: result: TMR0 overflow
Bit1: RB0/INT interrupt occurred (1)
Bit0: result: (1) when one of the PORTB pins has changed its state

Should be clear now that if we desire to use a timer interrupt we have to properly set these two register. For instance:

OPTION_REG = 0x83 or (0b10000011) disable the pull-ups resistor and sets the prescaler to 1/16
INTCON = 0xA0 or (0b10100000) Enable the interrupts selecting TMR0 as preferred (remebering that the TMR0 is an 8 bits timer (0-FF, 0,255)) .

Using mikroc, now, should be easy to create a program using such interrupt:

....

TRISA = 0;
TRISB = 0;
TMR0 = 0;
OPTION_REG = 0x83;
INTCON = 0xA0;

void interrupt() {
....                                             //routine da essere eseguita periodicamente
TMR0 = 0;                                  //timer reset
INTCON = 0x0b00100000;           //Intcon Bit5 reset (T0IE)
}

void main  (){
.....
.....
}

Diritti riservati Cuteminds.com 2008-2015. Tutti i marchi riportati sono detenuti dai legittimi proprietari. Tutto il materiale e` liberamente utilizzabile ma non per scopi commerciali. Non si assume alcuna responsabilita` sul materiale pubblicato per eventuali danni diretti o indiretti che possano derivare dall`utilizzo di schemi, progetti o altro materiale presente nel sito. Inviare una mail a: remove-admin@cuteminds.com (contenuti) remove-webmaster@cuteminds.com (webserver) per domande e/o suggerimenti.