Timers on 16-bit MCUs

Timer Overview

Simplified Timer Block Diagram

basic-timer.svg

Note: On many 16-bit MCUs, Timer1 has a low power oscillator that can be used as an input. This oscillator is tuned to work with a 32 kHz crystal, allowing Timer1 to be used to keep accurate time-of-day values.

Operation

Timers on 16-bit MCUs can be driven by one of two inputs:

  • Tcs - the MCU's internal clock
  • TxCK pin connected to either an asynchronous signal or to a clock source.

Tcs Input

In Timer mode, the Timer is driven by the internal clock (Tcs). The TMRx register contains the current value of the timer. When a timer is turned on ( TCON = 1) the time increments at a frequency determined by the speed of Tcs and timer's prescaler settings. The value of TMRx increments until it equals the ((PRx)). When TMRx equals PRx, three things happen:

  1. TMRx is reset to 0. The timer continues operating incrementing TMRx at regular intervals.
  2. The interrupt request flag ( TxIF) is set to one.
  3. If the Timer's interrupt is enabled and the priority assigned to the timers is sufficient (see the interrupt page) an interrupt will occur sending program control to the assigned interrupt service routine.

TxCK Input

Timers can be driven by either an asynchronous signal or clock, on the TxCK pin. Using the Gate Synch facility a timer can be set to count the number of timers the TxCK pin is toggled. This can be useful when TxCK is driven by an external asynchronous signal. In addition to generating an interrupt when the count equals the value loaded into PRx, the timer gate can generate an interrupt request upon each falling edge of TxCK.

The selection of the Timer input has no effect upon the operation of TMRx or PRx. These two registers have the same functionality regardless of the timer input.

Timer Initialization

Each 16-bit MCU timer has one control register TxCON, a period register PRx, and a Timer value register TMRx ( where x = the "number" of the timer)

  • TMRx is the value of the timer. Application programs typically load a value into this register at initialization to ensure the first timer period is of a known time.
  • PRx defines the timer period of the timers. The value loaded into the PRx determines the time interval between matches of TMRx and PRx. When driven by Tcs, the period of a timer is a product of the Tcs, the pre-scaler value, and PRx. The value loaded into PRx can be calculated manual, but most people find using Microchip Code Configuration(MCC) a better option. See the following code example to see how MCC is used to calculate PRx.
  • TxCON configures the clock source and the prescaler to the timer. TxCON provides the ability to start or stop the timer.

TxCON: Timer x Control Register

R/W-0 U-0 R/W-0 U-0 U-0 U-0 R/W-0 R/W-0
TON TSIDL TECS1 TECS0
bit 15 bit 8

U-0 R/W0 R/W0 R/W0 R/W-0 U-0 R/W-0 U-0
TGATE TCPS1 TCPS0 T32 TCS
bit 7 bit 0

bit 15

TON: Timer x On bit

1 = Timer x is Enabled
0 = Timer x is Disabled

bit 13

TSIDL: Timer stops in Idle mode bit

1 = Timer x stops when MCU enters Idle mode
0 = Timer x continues to operate when MCU enters Idle mode

bit 9-8

TECS<1:0>: Timer x Extended Clock Source Select bits (useed when TCS = 1)

11 = Generic Timer (TMRCLK) external pin
10 = LPRC Oscillator
01 = TxCLK external input
00 = SOSC

bit 6

TGATE: Timer x Gated Time Accumulation Enable bit

1 = Timer Gate Accumulation Enabled
0 = Timer Gate Accumulation Disabled

bit 5-4

TCPS<1:0>: Timer x Input Clock Prescaler select bits

11 = 1:256
10 = 1:64
01 = 1:8
00 = 1:1

bit 3

T32: 32-bit Timer Select mode bit

1 = Timer x joins with Timer y to form 1 32-bit timer
0 = Timer x remains a stand-alone 16-bit timer

bit 1

TCS: Timer x Clock Source select bit

1 = Timer x source selected by TECS<1:0>
0 = Internal Clock (Fosc/2)

© 2020 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.