Locating the Timer2/4/6 Module
The Timer2 (Timer4 or Timer6) module can be found in the MPLAB® Code Configurator (MCC) under Device Resources.
Choosing TMR2, TMR4, or TMR6 will open the following window:
Basic Module Functionality:
Clock Source: This drop down menu gives a multitude of options for clock source inputs. Some sources will give time increments, such as FOSC/4, while others will send pulses that will effectively turn the timer into a counter.
Postscalar: This value changes how often the output of the timer sends an interrupt. This is one method to extend the length of the timer period.
Note that the Postscalar adjustments will not affect the PWM signal if using the Timer2/4/6 for PWM purposes. Always remember to check the notifications and actual period values compared to the PWM values in the PWM module.
Prescalar: This value changes how often the clock edges are recorded to the timer, which is useful for increasing the period of the timer.
Polarity: This menu chooses whether the Timer will count on the rising or falling edge of the input clock cycle.
Clock Sync: The Timer can be set to sync with the system clock by checking the Enable Prescaler Sync and the Enable Clock Sync.
Timer Period: This defines the total cycle length for one clock cycle.
Ext Reset Source: The Timer2/4/6 allows for some external sources to reset the timer should your application use this function. This can be useful for controlling circuits that need to be shut down, or reset, if a certain event happens. For more information, see the datasheet section 29.3.
Control Mode: Timer2/4/6 can run in three modes.
- Roll Over Pulse is a continuous timer which, once the time has been completed and the timing buffer filled, will automatically restart. This is useful for most basic timing operations.
- One Shot Mode works similarly to Roll Over Pulse but will only cycle once. Once the value set in the T2PR register is reached, then the T2ON bit must be reset. Finally,
- Monostable Mode works similarly to one shot, but can be continued through use of an external reset.
Start/Reset Option: This sets the available options to use for starting and stopping the timer, as well as continuing based upon the aforementioned control mode.
Configuring the I/O for an External Timer Signal
Similar to the Timer0 module, generally the Timer2/4/6 is used for internal purposes and does not need external pins. However, should you want to output the clock signal, there are certain ports that will do that as seen below:
Using Basic Software Functionality in main.c file
MCC will generate functions to make using the timers easier. These are placed in the tmrx.h file generated by the MCC.
The functions, shown here, are based upon using Timer2 but similar functions will be created for whatever timer is selected.
|Software Function||What it does|
|void TMR2_Initialize(void);||This function is only necessary if the Timer2 is not enabled during initialization, meaning the box Enable Timer was unchecked. This must be called before any other functions are called.|
|void TMR2_Start(void);||If you do not enable the Timer to start, then this must be called after the timer is initialized to physically start the Timer.|
|void TMR2_Stop(void);||Calling this function will stop the operation of Timer2, which could be useful if the Timer is not necessary for part of an application.|
|uint8_t TMR2_ReadTimer(void);||As mentioned above, this timer overflows at the register value dictated by the period. This function allows you to read the timer at any moment in time, which could be useful for reading passages of time or running delays without delaying the whole CPU. This function may also be useful to trigger part of the program at a particular time.|
|uint8_t TMR2_ReadTimer(void);||This function allows you to write a value directly to the TMR2 register. This could be useful when your program uses the Timer to trigger something, and then should restart at a particular time after the trigger has been completed.|
|void TMR2_LoadPeriodRegister(uint8_t periodVal);||This function allows you to change the timer period, which could be particularly useful if your system needs to send different PWM signals at different times. This function will allow the period of your signal to change dynamically.|
|bool TMR2_HasOverflowOccured(void);||Every time the timer reaches your defined period, the bit resets back to 0x00 and restarts counting. This function allows you to watch for that to happen, and then returns TRUE, or 1, every time it occurs.|