8-Bit Timer0 Delay Using Polling

Hardware timers are built into most microcontrollers and can be a useful timebase for many projects. This project demonstrates how to use a timer to create a delay for a blinking a Light Emitting Diode (LED) using Interrupt Polling.

proj1.png

Step by Step Instructions

Using the MPLAB® Code Configurator (MCC), this project generates the code to blink an LED using a hardware timer, Timer0, to generate the delay between the on and off state.

project4.png

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.

The setup is described in the "Setup and Installation" section of this training module. You should see a screen similar to the one below before proceeding to step 1.

mplabx.png

1

Create a new 'Stand-Alone' 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.

2

Open MCC under the Tools > Embedded menu of MPLAB X IDE.

mcclaunch.png

3

Select the peripherals for your project. For this first 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, except TMR0. TMR0 needs to be added from Device Resources. Just click on Timer and then double-click on TMR0 to add it to the project. The result should look like the picture below:

proj4per.png

4

Open the 'Pin Manager' and then click on the PORTA pin 7 (RA7) output row, blue lock symbol. It should turn green. This adds this RA7 I/O pin to the project which is connected to the D5 LED of the HPC Curiosity Board.

proj4ioset.png

5

Open the Pin Module under Project Resources.

gpio.png

The center screen should show RA7 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 pin name to D5_LED to make it easier to understand in the code.

proj4pin.png

6

The System needs to be setup next. Click on the System name under Project Resources.

system.png

In this section, the oscillator settings and the configuration settings are selected.

Oscillator

  1. Select the HFINTOSC from the Oscillator Select drop-down menu.
  2. Select the 4MHz selection from the HF Internal Clock drop-down menu.
  3. Select Clock Divider value of 4.

This will enable the internal 1 MHz internal oscillator as the system clock.

oscillator.png

Also, make sure the Low-voltage Programming Enable mode is selected at the bottom of the System Module screen.

lvp.png

7

Timer0 needs to be set up. Click on the TMR0 label under Project Resources to make the Timer0 setup appear.

tmr0.png
  1. Check the Enable Timer box and select the 1:256 prescaler selection from the drop-down menu.
  2. Click on Clock Source and select (FOSC/4). This will select the instruction clock as the clock input to the Timer0.
  3. Type '250' into the Period box and it will change to the exact value of 249.856 ms. This is the closest the setup can get to a 250 ms delay. This will set the Timer0 to overflow every 250 ms (249.856 ms approximately).

The window should look like the one below after the proper setup:

timer0.png

8

Click on the Generate button to have the MCC create the software libraries for this project.

generate.png

9

The project will now have both generated Header Files and Source Files. It should also have a generated main.c file.
MCC will generate a main.c file.

proj4files.png

Double click on the main.c file to open it up in the Editor window.

mainfile.png

10

Timer0 will run until it overflows and then it will set a Timer Overflow Bit (T0IF) and keep running. The software routine just has to monitor that T0IF bit and when it's set, toggle the LED from OFF to ON or ON to OFF. Then software must clear the T0IF bit so it can be set again on the next overflow.

Add the following code to the end of the main.c file:

 while (1)
    {
        // Add your application code
        if (TMR0_HasOverflowOccured()) //Has Timer0 Overflowed?
        {
            D5_LED_Toggle(); //Switch state of D5 LED when overflow occurs
            TMR0IF = 0;      //Clear the Timer0 overflow flag
        }
    }

Note: The TMR0_HasOverflowOccured() and D5_LED_Toggle() functions are generated by the MCC and their definitions are located in the pin_manager.h file.

This is a simple example of how the MCC generates a library of useful functions to make creating project code much easier and quicker.

11

Click on Build Project (hammer icon) to compile the code; you should see a "BUILD SUCCESSFUL" message in the Output window of MPLAB X IDE.

Main_Build_Project.png
BUILD SUCCESSFUL (total time: 274ms)

12

Make sure your project has the Curiosity Board selected and the USB cable is connected to the board.

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/Verify complete" message and the D5 LED connected to RA7 will be blinking quickly on the HPC Curiosity Board.

Main_Program_Target_Project.png
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**
ra7lit.jpg

If it's 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

13

The project can be closed in MPLAB X IDE. 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 File > Close Project.

closeproject.png

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 4 Files
© 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.