PIC24-dsPIC33 Polled Timer

Last modified by Microchip on 2024/06/24 06:33

Objective

This example project demonstrates how to configure and use an internal timer on a PIC24F 16-bit MCU. You will learn how to configure one of the timers to overflow every 1/2 second. Code will be entered to monitor the timer's overflow flag. Each time the timer overflows the program will toggle one of the LEDs on the development board.

diagram of a digital output

This lab does not use interrupts. If you are looking for an example using a PIC24F with interrupts please refer to the "Programming a PIC24/dsPIC33 Timer Using Interrupts" example.

You will be shown how to configure and monitor a timer on a PIC24F MCU. With the help of MPLAB® Code Configurator (MCC), this project demonstrates the following:

  • Project creation for a 16-bit Microchip MCU.
  • Configuring the MCU system oscillator to run off the internal RC oscillator at 4 MHz.
  • Selecting 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 1/2 second.
  • Generating MCC code.
  • Manually editing MCC-generated code to start the timer, monitor the timer's overflow flag, and toggle the LED on each overflow.
  • Building and programming the completed application into the development board.

As a result of this lab, the selected LED will change state every 1/2 second.

After completing this lab, you should be able to use MCC to set a time 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 is connected to pin RA9 (PORTA pin 9).

PIC24F curiosity board showing LED1

Once the MCU has been programmed with MCC-generated code setting LED1 high, you will have an opportunity to manually modify the source code to turn LED1 off, and LED2 on.

Back to Top

Materials

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

Software Tools

Hardware Tools

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

Back to Top

Procedure

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 in the accompanying image.

New project window showing the PIC24F curiosity board

If this is your first time creating an MPLAB X IDE 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.

projects tab showing no source files

Back to Top


Open MCC

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

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

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.

verify/modify the System Modules under the Project Resources window

Back to Top


Set the System Resources

There are three system modules 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 project does not use interrupts, so this section will not be used.

System Module

The System Module allows the user to configure the MCU's clock, and 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. 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:

verify the default settings, on the System Module tab

Pin Module

Click on the Pin Module in the Project Resources window. Three windows will open in the IDE:

  • Pin Module Window
  • Package View
  • Grid View

resources window showing three windows

You may need to resize the IDE window to replicate the screen layout.

 We will now configure 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.

  • Ensure that RA9 is set as an output pin.
  • Rename RA9 as LED1.

Ensure that RA9 is set as an output pin. Rename RA9 as LED1.

Back to Top


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

the Device Resources section of the MCC Resource Management window, locate Timer1

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

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

Back to Top


Configure Timer1 for 1/2 Second Period

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

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

Select TMR1 from the project window and select the following settings

Back to Top


Generate Code

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

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

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

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

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

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

SYSTEM_Initialize() in turn calls PIN_MANAGER_Initialize() and TMR1_Initialize(). These two functions initialize the timer and pin configuration by writing to the device's function registers.

SYSTEM_Initialize() in turn calls PIN_MANAGER_Initialize() and TMR1_Initialize(). These two functions initialize the timer and pin configuration by writing to the device's function registers

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.

Back to Top


Modify the MCC-generated Code to Monitor the Timer and Toggle the LED

We will now modify main.c. An inspection of 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 Prototypes:

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

tmr1.h Function Prototype:

  • TMR1_Start(): starts the operation of Timer1.

Consulting the MPLAB XC16 C Compiler user's manual will show that data register bit IFS0bits.T1IF allows the application to read or reset Timer1's overflow flag.

Make the following modifications to main.c:

  1. Insert the text #include "mcc_generated_files/mcc.h" near the top of the file.
  2. Insert LED1_ToggleTMR1_Start, and the access to IFS0 into main.c as follows:

mainc.c

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

int main(void)
{
  // initialize the device
  SYSTEM_Initialize();
   TMR1_Start(); // ####### Start the timer ####
  while (1)
   {
     if (IFS0bits.T1IF)
      {
         LED1_Toggle();
         IFS0bits.T1IF = 0 ;
      }
   }
  return 1;
}

#include "mcc_generated_files/mcc.h" is required to be placed in any application source file that accesses 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.

Back to Top


Build, Download, and Run the Code

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

shows the project being built and programed in the output window

Back to Top


Results

When the application is built and programmed into the MCU, LED1 will change state every 1/2 second.

When the application is built and programmed into the MCU, LED1 will change state every 1/2 second.

Back to Top