16-bit Digital Input

 Objective

This example project demonstrates how to configure and use digital inputs on a PIC24F MCU. You will be shown how to configure a pin on the MCU, which is connected to a mechanical switch as a digital input. You will also configure another pin, connected to a Light-Emitting Diode (LED), as an output. The source code will be added to the project which reads the immediate value of the input switch and uses that value to drive the LED.

As a result of this lab:

  • When the switch is pushed down, the LED will be on.
  • When the switch is released, the LED will be off.
digital-input.png

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).
  • LED2: connected to RA10.
  • S1: connected to RC9.
  • S2: connected to RC8.
led1.png

We will first show you how to use MPLAB® Code Configurator (MCC) to generate code to configure RA9 (LED1) as an output pin, and RC9 (S1) as an input pin. You will then enter the code to drive LED1 high when S1 is pushed.

 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 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 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 the pin connected to LED1 (RA9) as an output and the pin connected to S1 (RC9) as an input. In the Grid View window, click on the output box under RA9 and the input box under RC9. The grid view will display the "padlocks" in green indicating these pins have been configured for use.

grid-view.png
  • Ensure that RA9 is set as an output pin.
  • Rename RA9 as LED1.
  • Ensure that RC9 is set as an input pin.
  • Rename RC9 as S1.
pin-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 TRISC registers with the vaues needed to set RA9 as an output pin and RC9 as an input pin.

system-initialize.png

Please consult the PIC24FJ128GA204 data sheet for the values used to configure TRISA and TRISC registers.

5

Modify the program to use the value of S1 to drive LED1

We will now main modify main.c to drive RA9 (LED1) with the value of pin RC9 (S1). An inspection of MCC generated header file pin-manager.h, shows MCC has created several control functions for the I/O pins we have configured and named. Among these macros are LED1_SetLow, LED1_SetHigh, and S1_GetValue().

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_SetLow and LED2_SetHigh into main().

main.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)
    {
        if (S1_GetValue())
                LED1_SetLow();
         else
                LED1_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 application source files you create.

6

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

After the board is pogrammed neither LED1 or LED2 will be on; they are both turned off.

 Results

When S1 is pushed, LED1will turn on. When S1 is released, LED1 will turn off.

di-results.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.