Home     Contact     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
 Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   

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:

  • 0000: 1:1 division
  • 0001: 1:2 division
  • 0010: 1:3 division
  • 0011: 1:4 division
  • 0100: 1:5 division
  • 0101: 1:6 division
  • 0110: 1:7 division
  • 0111: 1:8 division
  • 1000: 1:9 division
  • 1001: 1:10 division
  • 1010: 1:11 division
  • 1011: 1:12 division
  • 1100: 1:13 division
  • 1101: 1:14 division
  • 1110: 1:15 division
  • 1111: 1:16 division

T2CON register BITS <2> (TMR2ON):

This bit is named "Timer2 On bit". The Timer2 module can be turned on and off through this bit:

  • 0: Timer2 is off
  • 1: Timer2 is on

T2CON register BITS <1:0> (T2CKPS):

These 2 bits are the "Timer2 Clock Prescale Select bits". They control the prescaler division rate:

  • 00: 1:1 division
  • 01: 1:4 division
  • 1x: 1:16 division

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:

Previous page ---- Next page

Go back to the book contents

Go to the discussion forum of this book




  Email (shall not be published)


Notify me of new posts via email

Write your comments below:
BEFORE you post a comment:You are welcome to comment for corrections and suggestions on this page. But if you have questions please use the forum instead to post it. Thank you.


  • At 23 November 2014, 7:31:23 user Giorgos Lazaridis wrote:   [reply @ Giorgos Lazaridis]
    • @Fung If i understand the question correct - no it won't. But one interrupt may delay if it happens when the PIC runs the ISR at that time

  • At 22 November 2014, 15:42:10 user Fung wrote:   [reply @ Fung]
    • In my program for 16F628A I use all 3 timers (TMR0, TMR1 and TMR2), the interrupt frequency of these three timers are 5kHz, 4Hz and 4.16kHz respectively. TMR0 is used for MUX display, TMR1 for clock and TMR2 for piezo output.

      I write the interrupt actions in individual if-statement blocks in interrupt function, i.e.:
      //clear flag and do something to update display

      //clear flag and do something for clock

      //clear flag and do something for piezo

      If I change the interrupt frequency of TMR2 for example, would it affect the others (especially TMR1)?

  • At 28 March 2011, 19:22:55 user Kammenos wrote:   [reply @ Kammenos]
    • @aaron go to the nect pages then :D i have more

  • At 28 March 2011, 18:55:20 user aaron wrote:   [reply @ aaron]
    • to the author, well done Sir.
      but as someone new to PIC, if i like to design something, where i should go next? only led blinking procedure was mentioned here.

  • At 1 December 2010, 19:00:41 user Luis Lugo wrote:   [reply @ Luis Lugo]
    • Simply Excellent job, all the stuff are very well linked.

      I read your book this morning, it covers nicely all the topics until now, don't drop it.

      Best Regards.

  • At 28 November 2010, 13:59:47 user vishy wrote:   [reply @ vishy]
    • thaaaaaaaaaaanks very much!
      i hope can learning assemble for pic!
      you site is really good! and cant be better!

    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.

     HOT in heaven!

  • Disclaimer
  • Book Contents
  • Discussion forum

  • Basics
  • What will you need
  • Choosing the right PIC
  • The MPLAB
  • Getting familiar with the MPLAB environment
  • Creating a new project
  • Open and close projects
  • Creating new files and including them in the project
  • Your very first assembly program
  • Compile a program and transfer to the PIC
  • Section 1: Beginner's theory
  • Memory Organization
  • The Data Memory Organization
  • The Program Memory Organization
  • The instructions
  • General knowledge about instructions
  • Value Loading Instructions
  • Program Flow Instructions
  • Mathematic Instructions
  • Logic Function Instructions
  • Bit Orientated Instructions
  • Byte Orientated Instructions
  • Miscellaneous Instructions
  • The Basic Special Function Registers
  • The Status Register
  • The Option_Reg Register
  • The TRIS and PORT registers
  • Beginner's PIC Tutorials
  • How to use our PIC Tutorials
  • A Pushbutton turning an LED on and off
  • A Simple LED Flasher
  • Interfacing Multiple Switches - The internal Pull-Up resistors
  • An LED Sequencer
  • Interface a Single 7seg Digit
  • Interface Multiple 7seg Digits
  • A 3-digits Decimal Counter
  • A Clever Button
  • Section 2: Intermediate theory
  • Instruction Cycle Duration and Calculated Delays
  • The Timer Modules - Timer0
  • The Timer Modules - Timer1
  • The Timer Modules-Timer2

  • NEW in heaven!

    New Theory: AC electric motor working principle

     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS   

    Site design: Giorgos Lazaridis
    © Copyright 2008
    Please read the Terms of services and the Privacy policy