Timer 2 Programming for PIC18 Microcontroller

Timer 2 Programming for PIC18

Introduction to Timer 2 Programming

The Timer2 programming is different from the Timer0 and Timer1 programming, as it is used for generating the PWM signal. Timer2-associated registers are the following:

  • TMR2 Register: 8-bit register that can take value from 0 to 255
  • PR2 Register: 8-bit register that takes the threshold and when TMR2 reaches this value, the TMR2IF is raised
  • TMR2CON Register: This is the timer 2 control register that is used for setting the prescaler and postscaler value for PIC18 microcontroller

Basically, we set the decimal value in the PR2 register, and then the TMR2 is initialised at 00. When TMR2=PR2, then TMR2IF in PIR1 (peripheral interrupt register 1) is raised.

TMR2CON

The TMR2CON register is an 8-bit control register that manages:

  • Timer2 ON/OFF control
  • Prescaler selection
  • Postscaler selection
TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0

TOUTPS3:TOUTPS0 (Bits 6–3) – Postscaler Select

These bits define the postscaler value for Timer2 output.

Value Postscaler
0000 1:1
0001 1:2
1111 1:16

TMR2ON (Bit 2) – Timer2 Enable Bit

  • 1 = Turns ON Timer2
  • 0 = Turns OFF Timer2

This bit starts or stops the Timer2 operation.

T2CKPS1:T2CKPS0 (Bits 1–0) – Prescaler Select

Value Prescaler
00 1:1
01 1:4
10 1:16
11 Reserved

The prescaler divides the input clock before it reaches Timer2.

What is Prescaler in Timer 2 Programming?

In a microcontroller like PIC18, a prescaler is a hardware divider that reduces the frequency of the incoming clock signal before it reaches a timer. This allows the timer to count more slowly, making it possible to generate longer time delays or lower-frequency signals without needing a large counter value. For example, if a prescaler of 1:4 is selected, the timer increments once for every four clock pulses, effectively slowing down the counting process and increasing the timing range.

Example of Prescaler

For example, suppose a PIC18 is running at 4 MHz (instruction clock = 1 MHz, so 1 µs per tick). If no prescaler is used, the timer increments every 1 µs. But if you select a prescaler of 1:4, the timer will increment once every 4 µs instead of 1 µs. This means the timer counts more slowly, allowing you to generate longer delays without changing the timer size.

What is a prescaler in Timer 2 programming?

On the other hand, postscaling is applied after the timer has completed its counting cycle. It divides the output of the timer (such as an overflow or match event) before it triggers an interrupt or output action. This means the timer may overflow multiple times before a final output is generated. For instance, a postscaler of 1:8 will allow eight timer overflows to occur before producing a single interrupt. While the prescaler affects the timer’s counting speed and resolution, the postscaler controls how often the timer’s output event is observed, making it useful for reducing interrupt frequency without altering the timer’s base operation.

Example

For example, if Timer2 overflows every 1 ms and you set a postscaler of 1:5, the interrupt will be generated only after 5 overflows, i.e., every 5 ms. The timer itself is still running at the same speed, but the output event is delayed.

Key Functionality

  • Controls Timer2 timing resolution and speed
  • Used in PWM frequency generation (with PR2 register)
  • Helps generate periodic interrupts
  • Essential for configuring CCP PWM mode

Video Lecture for Timer 2 Programming  for PIC18 Microcontroller

Leave a Reply

Your email address will not be published. Required fields are marked *