Timer Peripheral Library for MPLAB® Harmony v2
This library allows you to:
- Set the clock source for the timer
- Read and set the timer value
- Read and set the timer period match value
- Start and stop the timer
- Set 8-, 16-, or 32-bit data width for the timer
- Operate in Sleep, Idle, or Debug modes
- Set the pre- and post-scaler settings
- Control timer special event trigger settings
- Control asynchronous clock counter mode for Timer1
- Control gated clock counter mode
The Timer Peripheral Library (PLIB) controls the timers by accessing the timer's Special Function Registers (SFRs). Timer SFR access is by function rather than by name. The developer does not need to know an SFR name or the specific SFR bit-field when calling a timer PLIB function. You have to identify the specific timer and the function to be performed on that timer.
Example Library Functions
Stopping a Timer
Function Name | Description |
---|---|
PLIB_TMR_Stop (TMR_ID_3) | Stops Timer3 |
Configuring a Timer
Function Name | Description |
---|---|
PLIB_TMR_ClockSourceSelect (TMR_ID_1, TMR_CLOCK_SOURCE_PERIPHERAL_CLOCK) | Uses internal peripheral clock to drive Timer1 |
PLIB_TMR_PrescaleSelect (TMR_ID_2, TMR_PRESCALE_VALUE_256) | Sets Timer2 prescale value to 256 |
PLIB_TMR_Mode16BitEnable (TMR_ID_3) | Configures Timer3 as a 16 bit timer/counter |
PLIB_TMR_Counter16BitClear (TMR_ID_2) | Clears 16-bit Timer2 value |
PLIB_TMR_Period16BitSet (TMR_ID_1, 0x1000) | Sets 16-bit Timer1 period |
Starting a Timer
Function Name | Description |
---|---|
PLIB_TMR_Start (TMR_ID_4) | Starts Timer4 |
Reading a Timer's Current Value
Function Name | Description |
---|---|
MyVar = PLIB_TMR_Counter32BitGet (TMR_ID_2) | Gets 32-bit Timer2 value |
The PLIB parameter for specifying a timer (TMR_ID_x) is defined by the enum TMR_MODULE_ID. The default values for this enumeration equates Timer1 with TMR_ID_1, Timer2 with TMR_ID_2, …etc