Timer0 Example using MPLAB Code Configurator

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

Timer0 Example

The first step after launching the MCC within MPLAB® X is to select the peripherals we will use in this example to set up the System settings, Timer0 configuration, and the I/O setup.

resource_timer0.png

System Setup

The System setup requires two sections to be configured - the oscillator and the configuration.
The first is the oscillator. A 4 Mhz internal oscillator is selected and the Timer0 clock sources are the internal clock.

osc_timer0.png

The configuration settings include the defaults. Also, the MCLR pin is set to be a digital I/O pin so the MCLR pull-up is internal to the PIC® MCU.

config_timer0.png

Timer0 Setup

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.

timer0_timer0.png

I/O Setup

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.

pin_timer0.png

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.

io_timer0.png

Generate Code

The generate code button generate.png 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.
headers_timer0.png
source_timer0.png

The main.c file is shown below. The MCC places a SYSTEM_Initialize function at the top. This function 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:

TMR0_HasOverflow_Occurred
TMR0_Initialize

There is also a function created in the pin_manager files that automatically toggles the I/O pin:

TMR0_HasOverflow_Occurred

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 makes generating the I/O toggle quite easy as all the port register settings are handled in the pin_manager functions.

mainc_timer0.png

Output Waveform

The oscilloscope capture below shows the result. The RA2 pin toggles every 2.52 milliseconds. This is a little longer than the settings in the TMR0 setup screen. 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.

scope_timer0.jpg
© 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.