PIC24/dsPIC33 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 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).
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.
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
- PIC24F Curiosity Development Board (Microchip part number DM240004)
- USB Micro-B cable to connect the PIC24F Development Board to your computer
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.
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.
Open MCC
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.
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:
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
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:
- 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.
Generate Code
To 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 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.
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.
Build, Download, and Run the Code
To run the program on the development board, click on the Make and Program Device Main 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.
You should notice that LED1 is turned on and LED2 is turned off.
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:
- Insert the text #include "mcc_generated_files/mcc.h" near the top of the file.
- Insert calls to the functions LED1_SetLow() and LED2_SetHigh into main() as shown.
mainc.c
#include "mcc_generated_files/mcc.h"
/*
Main application
*/
int main(void)
{
// initialize the device
SYSTEM_Initialize();
while (1)
{
LED1_SetLow();
LED2_SetHigh();
}
return 1;
}
Build the application and program it into the device on the development board.
Results
After programming the board, LED1 should be off and LED2 should be on.