Tuesday, January 13, 2015

PIC Microcontroller Timer1 Module

PIC16F877A Microcontroller Timer1 Module
After describing pic16f877a Timer0 in this topic:
PIC16F877A Microcontroller Timer0 module 
now we are going to see how to use pic16f877a timer1.
Timer 1:
Simply The Timer1 module is a 16-bit timer/counter consisting of two 8-bit registers (TMR1H and TMR1L) which are readable and writable. The TMR1 register pair (TMR1H:TMR1L) increments from 0000h to FFFFh and rolls over to 0000h. The TMR1 interrupt, if enabled, is generated on overflow which is latched in interrupt flag bit, TMR1IF.
REGISTERS ASSOCIATED WITH TIMER1:
The following table show the registers associated with pic timer1:

 



Timer1 calculator:

Timer1_freq = MCU_freq / {4 * Prescaler * (65536 - TMR1)}
Maximum Timer1_freq when TMR1H = TMR1L = 0 and prescaler = 1:8, for 12MHz system oscillator, so
Timer1_freq =  5.722 Hz, max time = 174.76 ms
Timer1 Example:
Let's make our timer interrupts 10 times to make a delay of 1 s an led of the previous example, so every timer1 should interrupt every 100 ms.
Make the prescaler 1:8, witch gives a preload = 28036, means that TMR1H = 0x6D, and TMR1L = 0x84.

MikroC code:

unsigned char i;
 void Interrupt(){
 if (TMR1IF_bit){
 i++;
 if(i>10){
 i=0;
 PORTB.F1 = ~PORTB.F1;}
 }
  TMR1H = 0x6D;
  TMR1L = 0x84;
  TMR1IF_bit = 0;
}
 void main() {
 T1CON = 0x31;             //timer1 prescaler 1:8
 TMR1IF_bit = 0;
 TMR1H   = 0x6D;
 TMR1L   = 0x84;
 TMR1IE_bit = 1;
 INTCON = 0xC0;              //Enabe internal interrupt
 TRISB.F1 = 0;              //Configure PORTB.F1 as output
 PORTB.F1 = 0;
 while(1);
  }


Next:
  PIC16F877A Microcontroller Timer2 Module

Most of these informations are from PIC16F877A datasheet.