The Timer0 module is an 8-bit timer/counter within most PIC® devices. Timer0 can also trigger an interrupt when it overflows. This project will create a blinking Light Emitting Diode (LED) based on a Timer0 Interrupt Delay.
Step by Step Instructions
Using MPLAB® Code Configurator (MCC), this project generates the code to blink an LED using a hardware Timer0 interrupt to generate the delay between the on and off state.
The project uses:
- PIC16F18875
- HPC Curiosity Board
- MPLAB X IDE
- MPLAB Code Configurator (MCC) plug-in
- MPLAB XC8 Compiler
To follow along with these steps, MPLAB X should be open and the HPC Curiosity Board connected to the computer through a USB cable.
1
Create a new standalone project in MPLAB X for a PIC16F18875.
If this is your first time creating an MPLAB X project, please visit the "Create a Standalone Project" page to follow a step-by-step instruction on how to do this.
3
Select the peripherals for your project. For this project, the following peripherals need to be selected:
- System Module
- Interrupt Module
- Pin Module
- TMR0
These modules will automatically be included when you launch the MCC, with the exception of TMR0. TMR0 needs to be added from Device Resources. Just click on Timer and then double click on Timer0 to add it to the project. The result should look like the picture below:
5
Close the Pin Manager and then click on the Pin Module under Project Resources.
The center screen should show RA4 listed on the I/O chart. Click on the output box to make the pin an output (if not checked) and make sure Analog and WPU are not checked (click on them to uncheck them). Change the Custom Name column to say D2_LED.
6
The System needs to be setup next. Click on the System Module name under Project Resources.
In this section, the oscillator settings and the configuration settings are selected.
Oscillator
- Select the HFINTOSC from the Oscillator Select drop-down menu.
- Select the 4_MHz selection from the HF Internal Clock drop-down menu.
- Select Clock Divider value of '4'.
This will enable the internal 1 MHz internal oscillator as the system clock.
Also, make sure the Low-voltage Programming Enable mode is selected at the bottom of the System Module screen.
7
Timer0 needs to be setup. Click on TMR0 under Project Resources to make the Timer0 setup screen appear.
Timer0 will be driven by the instruction clock which was set up in step 5.
- Check the Enable Timer box.
- Set Clock Source to FOSC/4 (this is the instruction clock).
- Set the Clock prescaler box to 1:256.
- Set the Timer Period to 250 ms (The reload value should automatically adjust).
- Check the Enable Timer Interrupt box (This will add the Interrupt Manager to the Project Resources).
The window should look like this after the setup:
8
The Interrupt Manager should be reviewed. Click on Interrupt Module under Project Resources to view the setup screen.
Timer0 will be shown as an interrupt source. Make sure it is enabled by having a check inside the box next to that interrupt source. The Pin Module source should not be checked.
Note: The message "Please remember to enable the Peripheral and Global Interrupts in your main.c code!" will be described in the main.c setup section of this training.
11
There are two files that need to be modified for this project: main.c and tmr0.c. These are the main code file main.c and the Timer0 driver file tmr0.c. They are modified as described below.
main.c
The main.c file requires a few lines to be uncommented for the interrupt to work properly. To enable the interrupt to work, Global Interrupts and the Peripheral Interrupts need to be enabled. MCC already has the control commands in the default main.c file, they are just commented out with two forward slashes (//). The forward slashes need to be removed to enable these lines of code.
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
INTERRUPT_PeripheralInterruptEnable();
The main loop does not require any additional code beyond the default while(1) loop. The Interrupt Service Routine (ISR) will handle the changing of the I/O pin.
while (1)
{
// Add your application code
}
tmr0.c
The tmr0.c file contains the ISR that will run when the interrupt occurs. This is where a function to toggle the LED pin is entered. The function used is the D2_LED_Toggle() macro that is created by MCC and placed in the pin_manager.h file. This D2_LED_Toggle() is added to the ISR as shown below.
void TMR0_ISR(void)
{
// clear the TMR0 interrupt flag
PIR0bits.TMR0IF = 0;
if(TMR0_InterruptHandler)
{
TMR0_InterruptHandler();
}
// add your TMR0 interrupt custom code
D2_LED_Toggle();
}
In addition to the ISR modification, the tmr0.c file also needs to know where the definitions are for the toggle function. By adding the line #include "mcc.h" to the tmr0.c file, the definitions generated by MCC can be compiled with the tmr0.c file without errors. The #include "mcc.h" is added just after the #include "tmr0.h"
#include <xc.h>
#include "tmr0.h"
#include "mcc.h"
13
Make sure your project has the Curiosity Board selected and the USB cable is connected to the board.
Click on Make and Program Device Main Project. This will build the project again and launch the programmer. In the Output window, you should see a series of messages, and if successful, it will end with a "Programming/Verify complete" message. Additionally, the D2 LED connected to RA4 will be blinking on the HPC Curiosity Board.
Connecting to Starter Kit on Board...
Currently loaded firmware on Starter Kit on Board
Firmware Suite Version.....01.54.00
Firmware type..............Enhanced Midrange
Target voltage detected
Target device PIC16F18875 found.
Device Revision ID = 2002
Device Erased...
Programming...
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
**Programming/Verify complete**
The D2 LED will blink at a 250 ms rate on then off. The picture below shows the LED caught in the lit position, but the application should blink continuously.
If it is the first time the programmer is connected to the board, the programming tool may need to download the proper operating firmware for the exact device. You may see a series of processes if this occurs. This should only happen once.
Downloading Firmware…
Downloading bootloader
Bootloader download complete
Programming download…
Downloading RS…
RS download complete
Programming download…
Downloading AP…
AP download complete
Programming download…
Firmware Suite Version…..01.34.11
Firmware type…………..Enhanced Midrange
Download
If you have any problems with your project, the completed MPLAB X project file can be downloaded from the link below:
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project 6 Files
|
| | | |