Difference between revisions of "NU32v2: Counters and Timers"

From Mech
Jump to navigationJump to search
Line 13: Line 13:


== Details ==
== Details ==
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed:
The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x:
*'''TxCON''': This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual.
*'''TxCON'''
*'''TMRx''': This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx.
*'''TMRx'''
*'''PRx'''
*'''PRx''': 16-bit value to reset TMRx.

Timer interrupts are controlled with the following SFRs:
Timer interrupts are controlled with the following SFRs:
*'''TxIE''': This SFR sets up the interrupt associated with the timer.
*'''TxIE'''
*'''TxIF''': This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated.
*'''TxIF'''
*'''TxIP''': This SFR sets up the priority of the interrupt associated with the timer.
*'''TxIP'''
*'''TxIS''': This SFR sets up the subpriority of the interrupt.
*'''TxIS'''


== Library Functions ==
== Library Functions ==

Revision as of 15:11, 23 January 2011

Timers are the simplest way to generate accurate time-based events such as flashing an LED at a given frequency. They are also useful for counting external pulses or accurate timing measurements of events.

Overview

The PIC32 is equipped with 5 16-bit timers (Timer1, Timer2, Timer3, Timer4, and Timer5). The basic idea of a timer is that it increments based on a clock signal.

In software, the user selects whether the timer increments using an internal clock or an external clock. The internal clock is based on the frequency of the peripheral bus. For the NU32v2, the peripheral bus frequency is set at 80 MHz. The external clock would be used if you wanted to count external pulses such as from an encoder. Each of the timers has a pin labeled 'TXCK' that can be used for the external clock. Additionally, the user selects a prescalar that determines how many pulses the timer receives from the clock before it adds one to the existing value.

Each timer can be configured to generate interrupts at a given priority.

The PIC32 has two types of timers:

  • Timer A: (Timer1) is an asynchronous timer with a built in oscillator, can operate in sleep mode and has prescalars of 1:1, 1:8, 1:64, and 1:256
  • Timer B: (Timer2, Timer3, Timer4, Timer5) has the ability to form a a 32-bit timer, has prescalars of 1:1, 1:2, 1:4, 1:16, 1:32, 1:64 and 1:256 and can have event triggers.

Details

The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, only 16 of the bits are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed for Timer x:

  • TxCON: This SFR sets up the timer. There are many different ways to configure the timers. The main options for TxCON are to turn on or off the timer, choose the pre-scalar, specify if the clock is internal or external and change mode to 32-bit timer. Other options can be seen on the PIC32 reference manual.
  • TMRx: This register stores the 16-bit counts. It resets to 0 when it reaches the number stored in PRx.
  • PRx: 16-bit value to reset TMRx.

Timer interrupts are controlled with the following SFRs:

  • TxIE: This SFR sets up the interrupt associated with the timer.
  • TxIF: This SFR corresponds to the interrupt Flag Status bit which is either 0 or 1 depending on whether or not the interrupt has been generated.
  • TxIP: This SFR sets up the priority of the interrupt associated with the timer.
  • TxIS: This SFR sets up the subpriority of the interrupt.

Library Functions

Sample Code

More Information