The Timer Modules-Timer2
The third and last timer module of the PIC 16F88 is the Timer2 module. This module is originated to work along with the PWM module of the PIC. It can be used of course as a timer as well. It is an 8-bit counter, and has a programmable prescaler and postscaler. It has only one input source, the internal instruction clock (device oscillator/4). Timer2 module is equipped also with a Period Register.
Setting up and accessing the Timer2 module
The Timer2 value register is located in the first BANK (BANK0) and is named TMR2. This module will be turned off after Sleep operation. To set up and control the Timer1 module, you need to access the T2CON register located also in BANK0. The bits of this register are explained bellow:
T2CON register BITS <6:3> (TOUTPS):
These 4 bits are the "Timer2 Output Postscale Select bits". They control the postscaler division rate:
T2CON register BITS <2> (TMR2ON):
This bit is named "Timer2 On bit". The Timer2 module can be turned on and off through this bit:
T2CON register BITS <1:0> (T2CKPS):
These 2 bits are the "Timer2 Clock Prescale Select bits". They control the prescaler division rate:
The Timer2 Period Register (PR2)
The Timer2 module is equipped with a period register. This register is named "PR2" and is located in BANK1. The TMR2 register is increased, until it is matched to the PR2 register. When this happens, on the next increament cycle, the TMR2 register will be cleared and the count will restart from 0x00. The PR2 register is both readable and writable. It is located BEFORE the postscaler, which you must take into account when setting the value. The postscaler division is NOT taken into account when comparing the PR2 to the TMR2 register!
The Timer2 Interrupt
The Timer2 module has an 8-bit register for the counts, the TMR2 register. This means that it can count up to 255. If the TMR2 value is 255 (0xFF) and it is increased once more, it will become 0 (0x00) and the TMR2 interrupt shall be raised. The Timer2 interrupt can be controlled by the PIE1 register bit 1 (TMR2IE: TMR2 Overflow Interrupt Enable bit). When the Timer2 Interrupt is raised, the PIR1 bit 1 (TMR2IF: TMR2 Overflow Interrupt Flag bit) is raised to indicate that the TMR2 interrupt is occurred. The TMR2IF must be cleared in software (BCF PIR1,TMR2IF) for the TMR2 interrupt to be re-enabled. More info about the PIC Interrupts can be found in this link.
Examples with the Timer2 Module
For the sake of understanding, find below some examples on how to setup the Timer2 module.
Example 1 - Set Timer2 module to raise the Timer2 interrupt every 65536 instruction cycles
MOVLW b'01111011' ; MOVWF T2CON ; Prescaler = 1:16 | Postscaler = 1:16 | Stop Timer2 BANK1 ; Go to BANK 1 MOVLW 0xFF ; MOVWF PR2 ; Set PR2 register to 0xFF BANK0 ; Go to BANK 0 BSF T2CON,TMR2ON ; Start Timer2
Confirm your knowledge
There is an online test to check your knowledge on this page. You may reveal the test with the following button:
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise without the prior written permission of the author.
Read the Disclaimer
All trademarks used are properties of their respective owners.
Copyright © 2007-2009 Lazaridis Giorgos.
All rights reserved.