16-bit Digital Output Example

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

proj1.png

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

led1.png

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

  • 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 MCC under the Tools > Embedded menu of MPLAB X IDE.

mcclaunch.png

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.

system-resources.png

3

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, 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

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
pin-manager-gui.png

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 below:
ra9-ra10.png
  • 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.
led-names.png

4

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() to configure the I/O pins. PIN_MANAGER_Initialize() loads the TRISA and LATA registers with the vaues needed to set RA9 and RA10 as output pins and initializes RA9 high and RA10 low.

system-initialize.png

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

5

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

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

led1-on.png

6

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 MCC generated header file "pin-manager" shows 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 which 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 applicaiton's source files you create.

Build the application and program it into the device on the development board.
Main_Program_Target_Project.png

 Results

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

led2-on.png

 Learn More

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

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