The Timer 0 module is a 8-bit timer/counter within most PIC® MPLAB® Code Configurator (MCC) devices. The Timer 0 can also trigger an interrupt when it overflows. This project will create a blinking Light Emitting Diode (LED) based on a Timer 0 Interrupt Delay.
Step by Step Instructions
Using the MPLAB Code Configurator (MCC), this project generates the code to blink an LED using a hardware Timer 0 Interrupt to generate the delay between the on and off state.
The project uses:
- PIC16F1508
- PICDEM™ Lab II Development Board
- Power from a 9 V Power Adapter
- PICkit™ 3 Programmer/Debugger
- MPLAB® X IDE
- MPLAB Code Configurator (MCC) plug-in
- MPLAB XC8 Compiler
- 5mm LED
- 330 Ω resistor 1/8 watt
PICDEM Lab II Connections:
- Connect 5 V and GND to the power bus of the protoboard.
- Connect 5 V to the Vdd pin of the PIC16F1508
- Connect Gnd to the Vss pin of the PIC16F1508
- Connect a wire from the RA2 pin (pin 17) to the breadboard Row 23
- Connect a wire from the ground bus to breadboard Row 27
- Connect the LED Anode to breadboard Row 23
- Connect the LED Cathode to breadboard Row 24
- Connect a 330 Ω resistor between Row 24 and Row 27
To follow along with these steps, MPLAB X should be open and the Programmer/Debugger connected to both the computer and the Development Board. The setup is described in the "Setup and Installation" section of this training module. You should see a screen similar to the one below to move proceeding to step 1.
1
Create a new 'Stand-Alone' project in MPLAB X for a PIC16F1508.
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.
In this first project these peripherals need to be selected:
- System Module
- Interrupt Module
- Pin Module
- TMR0
These modules will automatically be included when you launch the MCC. TMR0 needs to be added from Device Resources. Just click on the 'Timer' category and then double-click on 'Timer 1' to add it to the project. The result should look like the picture below:
4
Open the 'Pin Manager' and then click on the PORTA pin 2 (RA2) output blue lock symbol. It should turn green and adds RA2 I/O pin to the project. Also, make sure the reset pin has the blue lock shown. If it's green, then click on the green lock to make it blue. This will make the reset internal. It should look like the picture here when completed:
6
The System needs to be setup next. Click on the System name under Project Resources.
The System section will appear. In this section the oscillator settings and the configuration settings are selected.
Oscillator
- Select the 'INTOSC' from the drop-down menu for the System Clock Select.
- Select the '1MHz_HF' selection from the Internal Clock drop-down menu.
This will enable the internal 1 MHz internal oscillator as the system clock.
Configuration
Each configuration setting can be changed under the 'Register tab' of the System window. Match the selections shown here.
7
The Timer0 needs to be setup. Click on 'TMR0' under Project Resources to make the Timer0 setup screen appear.
The Timer 0 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 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 Manager' under Project Resources to view the setup screen.
The Timer 0 will be shown as an interrupt source which was setup in step 7.
Note the message: "Please remember to enable the Peripheral and Global Interrupts in your code!"
This will be setup in the main.c file of the project code.
9
Click on the Generate Code button to have the MCC create the software libraries for this project.
10
The project will now have both generated Header Files and Source Files. It should also have a generated main.c file.
Note: MCC may offer to generate a main.c file. Click on YES to allow it to complete this task.
Double click on the main.c file to open it up in the Editor window.
11
There are two files that need to be modified for this project: main.c and tmr0.c. These are the main code file and the timer 0 code file. 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. The 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 IO_RA2_Toggle() macro that is created by the MCC and placed in the pin_manager.h file. This IO_RA2_Toggle() is added to the ISR as shown below.
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 screen capture below shows the proper place to add any #include statements:
13
Make sure your project has the programming tool selected (Part of Step 1 above) and connect power to your development board.
- PICkit 3 has limited power capability so we recommend you power the board separately.
- ICD 3 can power a development board, but for simplicity we recommend you power the board separately.
- RealICE cannot power the development board so powering the board separately is required.
Click on 'Make and Program Device'. 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 and Verify Successful" message.
Output Window:
Connecting to MPLAB PICkit 3...
Currently loaded firmware on PICkit 3
Firmware Suite Version.....01.41.06
Firmware type..............Enhanced Midrange
Target voltage detected
Target device PIC16F1508 found.
Device ID Revision = 2
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x5f
configuration memory
Device Erased...
Programming...
Programming/Verify complete
The RA2 LED will blink at a 250ms 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
14
The project can be closed in MPLAB X. The project is saved automatically when it is built but any changes to files or configuration may ask to be saved before the project is closed. The project can be closed under the File > Close Project.
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
|
| | | |