Using Timer0 in MPLAB® Code Configurator

Last modified by Microchip on 2024/01/29 17:23

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® X is made easier with the MPLAB Code Configurator (MCC).

Locating Timer0

The Timer0 peripheral can be found in the Device Resources of MCC.

The Timer0 peripheral can be found in the Device Resources

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

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

Basic Functionality

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

  • Clock prescaler: This value changes how often the clock cycles are recorded to the timer. It is useful for increasing the period of the timer.
  • Postscaler: This value changes how often the output of the timer sends an interrupt. In other words, you can use it to trigger something based on 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 determine 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.

Configuring the I/O for an External Timer Signal

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 FunctionWhat 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.


Timers are very useful in microcontroller projects and MPLAB X IDE teamed with MCC makes setting all this up easy and effective to use. These same techniques can be used with MPLAB Xpress and MCC.