PIC24/dsPIC33 Digital Output Example

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

Objective

This example project will show you how to program a digital output pin on a 16-bit PIC24F MCU. You will learn how to configure and control a digital I/O pin on a PIC24F MCU using Microchip development tools.

One of the very basic functions of an MCU is to alter the state of a digital pin. A digital output pin can turn on or off anything from a jet engine to a simple Light Emitting Diode (LED).

diagram of a digital output

By following this project you will generate code to turn on the two LEDs on the PIC24F Curiosity Development Board. LED1 is connected to pin RA9 (PORTA pin 9), LED2 is connected to RA10. We will first show you how to use MPLAB® Code Configurator (MCC) to generate code that configures RA9 and RA10s as digital output pins. RA9 will be configured by MCC to start in a high position and RA10 will be initialized low.

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.

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 "PIC24F and dsPIC33 Examples" 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 IDE 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.

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 in MPLAB X IDE" 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.

The Pin Manager: Grid View window shows that pins RB1 and RB0 have been reserved as the programming pins.

We will now set up pins RA9 and RA10:

  • In the grid window, click on the output box for pins RA9 and RA10. RA9 and RA10 will be added to the Pin Manager window as shown in the accompanying image:

pin manager - grid view

  • Ensure that both RA9 and RA10 are set as output pins.
  • Click on the Start High box next to IO_RA9 to program this pin to be turned on after initialization.
  • Rename IO_RA9 and IO_RA10 to names of your choosing. For this example, we have named them LED1 and LED2.

showing the Start High box next to IO_RA9 to program this pin to be turned on after initialization

Back to top


Generate Code

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

MCC window showing the Generate code button

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) 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.c file showing the main function

SYSTEM_Initialize() in turn calls PIN_MANAGER_Initialize() to configure the I/O pins. PIN_MANAGER_Initialize() loads the TRISA and LATA registers with the values needed to set RA9 and RA10 as output pins and initializes RA9 as high and RA10 as low.

SYSTEM_Initialize function and the PIN_MANAGER_Initialize calls

Please consult the PIC24FJ128GA204 datasheet for the settings for the LATA and TRISA registers.

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 program target project icon. 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.

click on the Make and Program Device Main Project button in mplab x

You should notice that LED1 is turned on and LED2 is turned off.

PIC24F Curiosity board showsing LED1 on and LED2 off

Back to top


Modify the Program to Turn on LED2 and Turn off LED1

We will now modify main.c to turn RA9 off and turn RA10 on. It is possible to reconfigure MCC and generate the code to change the state of the LEDs. Since the purpose of this exercise is to demonstrate the use of MCC-generated functions, we will manually enter the code to control the LEDs.

An inspection of the MCC-generated pin-manager header file shows that MCC has created several control functions for the I/O pins we have used. Among these macros are LED1_SetLow() and LED2_SetHigh().

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 calls to the functions LED1_SetLow() and LED2_SetHigh into main() as shown.

mainc.c

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

int main(void)
{
  // initialize the device
  SYSTEM_Initialize();

  while (1)
   {
      LED1_SetLow();
      LED2_SetHigh();
   }

  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.

Build the application and program it into the device on the development board.
program target project button

Back to top


Results

After programming the board, LED1 should be off and LED2 should be on.

After programming the board, LED1 should be off and LED2 should be on.

Back to top