Using Timer0 in MPLAB® Xpress

Timer0 can be used as an 8- or 16-bit timer/counter. Timer0 is useful for creating delays that do not stop the entire program or creating interrupts to trigger events in your code after a certain amount of time. Using Timer0 within MPLAB® Xpress is made easier with the MPLAB Code Configurator (MCC). You can launch MCC from MPLAB Xpress.

Locating the Timer0

The Timer0 peripheral can be found in the Device Resources of MPLAB® Code Configurator (MCC).

Figure%201.JPG

Double click on TMR0 under Timers to open the Easy Setup window for this peripheral:

Figure%202.JPG

Basic Functionality

Timer0 has several setup options that you can select in the Easy Setup screen. You can also see the Register setup under the Register tab. Here are the main options to select:

Clock prescalar: This value changes how often the clock cycles are recorded to the timer. It is useful for increasing the period of the timer.

Postscalar: This value changes how often the output of the timer sends an interrupt. In other words, you can use it to trigger something based upon how often the timer buffer overflows.

Timer mode: Timer0 can be used as an 8-bit or 16-bit timer. In 8-bit mode, Timer0 can be used similarly to Timer2/4/6 by setting the value at which the timer will automatically overflow. In contrast, in 16-bit mode, it acts like Timer1/3/5. In 16-bit mode, the timer always overflows with the 16-bit buffer fills, and changing the period simply changes how much of the 16-bit buffer is pre-filled so that the timer overflows in the allotted period of time. 16-bit mode is heavier on the code because it requires the code to refill the buffer every period. For more information, check Section 27 of the datasheet.

Clock Source: The clock options determines what signal increments Timer0. For most applications, FOSC/4 or the system clock divided by four is sufficient.

Enable Timer Interrupt: When the timer buffer overflows, or the period is reached, the CPU will be interrupted. It is useful for waking from Sleep mode or controlling things based upon a set amount of time passing without using a delay.

Requested Period: This option defines the period length, or the length of time before the timer overflows and the buffer resets.

Configuring the I/O for an External Timer Signal

Pin configuration using Timer0 is not necessary for the basic functionality. However, it is useful to note that there are a set of pins that can be used to output the clock signal, in case your application needs to monitor the clock signal and also as an input to control the incrementing of Timer0.

timer0.png

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 MCC.
The functions, shown here, are based upon using Timer0, similar functions will be created for whatever timer is selected.

Software Function What It Does
void TMR0_Initialize(void); This function must be called before any programming using the Timer0 functions and is used to load all of the initial settings for Timer0 in your program.
void TMR0_StartTimer(void); Begins the Timer0. This will happen automatically the first time and this function is only needed if Timer0 is stopped at some point and now needs to be restarted.
void TMR0_StopTimer(void); Stops the Timer0.
void TMR0_ISR(void); This is a slightly more complicated function, called an Interrupt Service Routine. When the period is reached, the clock overflows and if enabled, triggers an interrupt bit which changes the TMR0 interrupt flag from a zero to a one. Initially, this function simply resets the interrupt flag bit. However, should you want something more complicated to happen, this function, called from tmr0.c, leaves room to call more actions after resetting the flag bit to zero.

Summary

Timers are very useful in microcontroller projects and MPLAB Xpress teamed with MCC makes setting all these up easy and effective to use. These same techniques can be used with MPLAB X and MCC. Here are a couple examples: Timer0 delay using polling and Timer0 delay using an interrupt.

© 2024 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.