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

Interrupt with Timer1

Last update: 31/07/2015

We have to underline that it is always really important to read the datasheet before to attempt to program any picmicro even if the use of this timer is common to many microcontrollers. The use of the interrupt generated by the timer1 overflow is a quite useful procedure conceptually not so different from the timer0 interrupt but, naturally, a little more sophysticated:

  • it is a 16bits module (0000h to FFFFh or 0 to 65535)
  • can use a clock source internal or external
  • provide an interrupt or a wake-up on overflow
  • can operate as timer or syncro/asyncro counter

being a 16bit module its value is memorized into two different locations (or registers): TMR1H and TMR1L.

Normally the behaviour of this module is regulated by a register: T1CON but we have always to consider involved even other registers.

timer1 pic

 

Bit7 and Bit6: are not used

T1CKPS1
T1CKPS2 

Bits 5-4: set the prescaler at:
00 = 1/1 fosc/4
01 = 1/2 fosc/4
10 = 1/4 fosc/4
11 = 1/8 fosc/4

T1OSCEN

Bit3: if the INTOSC is used, sets the LP oscillator
1 = LP oscillator is ON for timer1
0 = LP oscillator is OFF 

T1SYNC

Bit2: sets the syncronization of the external clock
1 = syncro is ON with external input
0 = syncro is OFF 

TMR1CS

Bit1: sets the clock of the timer
1 = clock is provided externally from T1CKI
0 = clock is fosc4 

TMR1ON

Bit0: sets the timer ON or OFF
1 = Enable timer1
0 = timer1 disabled 

Using a very common microcontroller like the 12F683, Bit7 and Bit6 (T1GINV and TMR1GE are used respectively to invert the gate logic and to monitor the timer1 gate) while using midrange mcu these bits are not used.

The situation seems quite complicated but this is not true (or this is not completely true). Timer1 is only a more complete timer useable for differents jobs. 
To enable the interrupt we have to set even other registers:

intcon timer1

The INTCON register bit envolved with the timer1 are the first two GIE and PEIE:

  • GIE  = Global interrupt enable
  • PEIE = Peripheral interrupt enable

Both this bits must be set at '1'

pir pie timer1

PIE and PIR Register bits have different positions according to the picmicro because they are device dipendent. This is not a problem because using high level languages (and libraries) we can address them simply using their names without care too much of their position.

The TMR1IE must be enabled (set to 1)

The TMR1IF flag must be cleared every time the overflow interrupt routine is called.

Remembering what previously exposed regarding the precharge values of the counter and the prescaler the situation should be clear: the prescaler divides the clock of the mcu (fosc/4) and an overflow interrupt is generated every 65536 pulses. If we deside to modify this value we can precharge the values of TMR1H:TMR1L registers. In any case is suggested to use a timer calculator (i.e.: http://eng-serve.com/pic/pic_timer.html)

 

Using Microc (on a 16f877) we can, therefore, write:
.......
void interrupt() {
if (TMR1IF_bit) {
......                        // programm interrupt
......
TMR1IF_bit = 0;       // clear TMR1IF
TMR1H = 0x00;        // restore the starting values of the counter
TMR1L = 0x00;
}
}
void main() {
ADCON = 0x0;         // Configure AN pins as digital
ADCON1 = 0x0;
CMCON = 0x7;         // Disable comparators
TMR1IE_bit = 1;       // enable Timer1 interrupt
TMR1IF_bit = 0;       // clear TMR1IF
TMR1H = 0x00;        // Initialize Timer1 register with a precharged value if necessary
TMR1L = 0x00;
T1CON = 0x01;        // Timer1 settings - TMR1ON=1 - PRESCALER=1/1
INTCON = 0xC0;
do {
.........
.........
} while (1);
}

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.