Locating the Timer0
The Timer0 peripheral can be found in the Device Resources of MPLAB® Code Configurator (MCC).
Double click on TMR0 under Timers to open the Easy Setup window for this peripheral:
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, which 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, look in the datasheet in section 27.
Clock Source: The clock options determines what signal increments Timer0. For most applications, FOSC/4 or the system clock divided by 4, is sufficient.
Enable Timer Interrupt: When the timer buffer overflows, or the period is reached, the CPU will be interrupted, which 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 teh incrementing of Timer0.
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 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 0 to a 1. 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 0.|