Programming a PIC24/dsPIC33 Timer Using Interrupts

 Objective

This example project demonstrates how to configure and use an internal PIC24F timer using an interrupt. You will be shown how to configure a PIC24 timer to generate an overflow interrupt every half-second. You will write a simple C function to toggle an LED. You will register the C function as the Interrupt Service Routine (ISR) for the timer, ensuring that each time the timer overflows, the LED on the development board will toggle.

proj1.png

This lab uses interrupts. If you are looking for an example of using a PIC24F which polls the timer overflow flag, please refer to the "Programming a Microchip PIC24F Timer" example.

With the use of MPLAB Code Configurator (MCC), this project demonstrates the following:

  • Creating a project for the 16-bit Microchip MCU on the development board
  • Configuring the MCU system oscillator to run off the internal RC-oscillator at 4 MHz
  • Configuring one of the I/O pins connected to an LED as an output
  • Adding a timer to the list of peripherals used by the application
  • Configuring the added timer to overflow every half-second
  • Configuring the timer interrupt priority
  • Generating the MCC code
  • Manually editing the MCC-generated code to:
    • Create a function which toggles the LED
    • Register the just-written function as the ISR for the timer
    • Start the timer operation when the application begins execution
  • Building and programming the completed application into the development board

As a result of this lab, the selected LED will change state every half-second.

It is expected that after completing this lab, you will able to use MCC to set a timer period of any value on a 16-bit PIC MCU.

This project uses the PIC24F Curiosity Development Board. This board has two LEDs and two input switches: LED1 - connected to pin RA9 (PORTA pin 9).

led1.png

 Materials

To follow along with this example, you will need the following software and hardware:

Software Tools

  • MPLAB® X IDE
  • MPLAB XC16 Compiler
  • MPLAB Code Configurator (MCC)

Hardware Tools

  • PIC24F Curiosity Development Board (Microchip part number DM240004)
  • USB Micro-B cable to connect the PIC24F Development Board to your computer

Information on how to download the software tools or acquire the development board can be found on the "Resources Needed for PIC24F Labs" page.

 Procedure

1

Create the Project

After installing the software, connect the PIC24F Curiosity Development Board to a USB port on your computer. Create a new standalone project in MPLAB® X for a PIC124FJ128GA204. The PIC124FJ128GA204 is the microcontroller on the PIC24F Curiosity Development Board. When the project creation wizard asks for a hardware tool (Step 2 in the New Project window), select the PIC24F Curiosity Board as displayed below.

16bit-hw-tool.png

If this is your first time creating an MPLAB X project, please visit the "Create a Standalone Project" page to follow step-by-step instructions on how to do this.

After the project has been created, the Projects tab in the upper-left corner of the IDE shows that the project has been created with no source or header files.

empty-project.png

2

Open MCC

Open MPLAB® Code Configurator (MCC) under the Tools > Embedded menu of MPLAB X IDE.

mcclaunch.png

The MCC will place a Resource Management tab on the left-hand side of the IDE. Inside this tab, you will see a section for Project Resources and Device Resources. For each MCC generated project, you will need to verify/modify the System Modules under the Project Resources window. (This example does not require the use of the Device Resources.)

system-resources.png

3

Set the Project Resources

There are three system elements which need attention:

  • Interrupt Module - controls the MCU's interrupts
  • Pin Module - configures the I/O pins
  • System Module - selects and configures the clock source for the MCU

Interrupt Module

This section displays and controls the priority and the enable bit for each interrupt. This section will be reviewed after the Timer1 peripheral is configured (Step 6).

System Module

The System Module allows the user to configure the MCU's clock, the Watchdog Timer (WDT), and make changes to the debug pin assignments. This feature of the PIC24F MCU has numerous options, which are typically modified to fit the needs of the application. MPLAB® Code Configurator (MCC) provides default settings if no changes are selected by the developer. For this lab, accept the default clock settings:

  • 8 MHz Internal Free Running Oscillator with no Prescaler, but a 1:2 Postscaler (4 MHz Fosc)
  • Watchdog Timer - disabled
  • Unchanged debug pins

To verify the default settings, click on the System Module tab and verify the following selections have been made:

16bit-default-system.png

We will now set the pin connected to LED1 (RA9) as an output pin. In the Grid View window, click on the output box under RA9. The Grid View will display the padlocks in green, indicating these pins have been configured for use.

pin-manager-gui.png
  • Ensure that RA9 is set as an output pin.
  • Rename RA9 as LED1.
pin-names.png

4

Add the Timer to the List of Peripherals Being Used in the Application

From the Device Resources section of the MCC Resource Management window, locate Timer1 (TMR1).

resources.png

Double-click on TMR1 to add this peripheral to the resources available to the project.

selection.png

5

Confgure Timer1 for Half-second Period with the Interrupt Enabled

With the Fosc set at 2 MHz, Timer1 will be unable to generate a half-second period as the 16-bit counter will overflow before half-second. To lengthen the period window for this timer, we will prescale the system clock by 64 to slow the timer down. Once the timer is slowed, we will be able to select the half-second timer period.

Select TMR1 from the project window and select the following settings:

tmr-settings.png

Ensure that the Enable Timer Interrupt box is checked

6

Verify the Priority of the Timer Interrupt

Select the Interrupt Module icon in the Project Resources window to verify that the Timer1 interrupt has been enabled.

interrupts.png

For this example, Timer1 should be shown as the only enabled interrupt with the default priority of '1.' Please refer to the *16-bit Interrupts page for details on how interrupts are implemented and programmed on this device.

7

Generate Code

To generate the code, click the Generate button on the MCC window.

generate.png

The projects tab will show the source and header files created by MCC.

mcc-files.png

The main(void) is located within the main.c file. main(void) calls the MCC generated SYSTEM_Initialize() function before it enters the while(1) loop.

main-c.png

SYSTEM_Initialize() in turn calls PIN_MANAGER_Initialize(), INTERRUPT_Initialize(), and TMR1_Initialize(). These functions initialize Timer1, set up the output pin and establish the interrupt writing to the device's function registers.

system-initialize.png

If you are interested in learning more about the details of device initialization, please consult the PIC24FJ128GA204 datasheet for the specific registers and settings used to configure the I/O pins and Timer1.

8

Modify the MCC-generated Code to Complete the Application

We will now modify main.c.
An inspection of the MCC-generated header file pin-manager.h and tmr1.h shows MCC has created several control functions useful for our applications

pin-manager.h Function Prototype:

  • LED1_toggle(); - changes the value of the I/O pin connected to the LED1

tmr1.h Function Prototypes:

  • TMR1_Start(): starts the operation of Timer1
  • TMR1_SetInterruptHandler(): registers a user defined function as the ISR for Timer1

Make the following modifications to main.c

  1. Insert the text #include "mcc_generated_files/mcc.h" near the top of the file
  2. Create a function void My_ISR(void); with one line of code LED1_Toggle();
  3. Insert TMR1_Start and TMR1_SetInterruptHandler(My_ISR) into main.c as follows:

main.c

#include "mcc_generated_files/system.h"
#include "mcc_generated_files/mcc.h"  //##### must be added #####

void My_ISR(void)                     //#############
  {
    LED1_Toggle();
   }
int main(void)
{
    SYSTEM_Initialize();
    TMR1_SetInterruptHandler(My_ISR) ;  //##########
    TMR1_Start();                       //##########
    while (1)
         {
         }
    return 1;
}

#include "mcc_generated_files/mcc.h" is required to be placed in any application source file which accesses the MCC generated functions. This line must be placed "above" the application's call to an MCC function. Not all versions of MCC correctly include this code into main.c. You will also need to manually add this line to each of the application source files you create.

8

Build, Download and Run the code

To run the program on the development board, click on the Make and Program Device Main Project button Main_Program_Target_Project.png. This will build the program into the flash memory of the PIC®. The output window of the IDE will tell you when the device has been programmed and the application is running.
programmed.png

 Results

When the application is built and programmed into the MCU, LED1 will change state every half-second.

results.png

 Learn More

Here are some addtional examples of programming other 16-bit MCU peripherals:

© 2019 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.