The MPLAB Code Configurator (MCC) makes setting up the Timer0 peripheral very easy. The MCC will automatically generate the code to load the proper registers and initialize the proper values to produce the desired Timer0 operation.
The best way to show how this is done is through a simple example. In this example Timer0 will be used as a time base for toggling an I/O pin. A PIC16F1825 will be used with the 4 Mhz internal oscillator and the RA2 I/O pin will toggle on every Timer0 overflow. Timer0 will be set to overflow at a 2.5 millisecond rate using a 1:32 prescaler.
The first step after launching the MCC within MPLAB X® is to select the peripherals we will use in this example to setup the System settings, Timer0 configuration and the I/O setup.
The System setup requires two section to be be configured; the oscillator and the configuration.
The first is the oscillator. A 4Mhz internal oscillator is selected and the Timer0 clock sources is the internal clock.
The configuration settings include the defaults plus the MCLR pin is set to be a digital I/O pin so the MCLR pull-up is internal to the PIC® MCU.
Timer0 has various options to select from. The prescaler value of 32 is selected from the drop down menu. The period of the timer is then set to a value as close as possible to the desired 2.5 milliseconds. The Interrupt will not be used so this is left unchecked. Notice that the preload value will automatically change when the desired period is selected.
The pin manager is used to select the RA2 pin. Clicking on the blue unlock for RA2 will change it to a green lock to show that pin was selected.
The I/O setup screen shows the RA2 pin parameters and this is where the pin is configured as an output by checking that box. The name of the pin could also be changed to a custom name but it will be used as the default IO_RA2 in this example.
The generate code button is then clicked to produce the software files. A set of source files including a main.c and a set of header files are both produced by the MCC.
The main.c file is shown below. The MCC places a SYSTEM_Initialize function at the top that includes all the necessary files for the Timer0 setup. In addition to that, a few commands need to be added by the developer. Within the generated code are two functions that make the code easier:
There is also a function created in the pin_manager files that automatically toggles the I/O pin:
The TMR0_HasOverflow_Occurred tests the TMR0IF bit to see if it is a one. An "if statement" is used to test the result of the TMR0_HasOverflow_Occurred function. If the result is one, indicating the TMR0 has overflowed, then the RA2 pin is toggled. Timer0 has to be re-initialized which also clears the TMR0IF bit and preloads the TMR0 register with the proper preload value to create the 2.5 millisecond delay.
The function IO_RA2_Toggle is created in the pin_manager files. This helps to make generating the I/O toggle quite easy as all the port register settings are handled in the pin_manager functions.
The oscilloscope capture below shows the result. The RA2 pin toggles every 2.52 milliseconds. This is a little longer than than the settings in the TMR0 setup screen but this is because of the time it takes to toggle the pin and then re-initialize the timer0. If the timing is critical then the value in the Timer0 setup can be adjusted to change the preload value that produced the desired overflow time.