Lab 1. Simple Digital Input/Output (I/O)

Last modified by Microchip on 2026/04/09 14:46

Purpose

In this lab exercise, you will create and build a new standalone dsPIC33A project using MPLAB® Extensions for Microsoft® VS Code® that demonstrates basic digital input/output pin programming. In addition, you will learn how to perform programming and debugging operations in your MPLAB Extensions for VS Code project.

To learn more about the dsPIC33A Input/Output (I/O) port, please visit the dsPIC33A I/O Ports Peripheral page, or refer to the dsPIC33AK512MPS512 Family Data Sheet.

Overview

After creating a new MPLAB Extensions for VS Code project, you will modify main.c with a code example that initializes pin RF0 for sensing switch S2 input, and pin RD9 to drive the red RGB LED output. The main loop will then read the switch level continuously, and set the LED output HIGH when pressed, and LOW when released. 

Here is a simplified connection diagram for this lab exercise:

connection diagram

This lab exercise is divided into the following sections:

Procedure

Part A. Create a New dsPIC33A Project in VS Code®

This exercise will have you create a new project and verify that it builds successfully. You will also verify that your tool is connected and visible in VS Code.

Follow the procedure covered in Lab 0, Part E to create a new MPLAB Extensions for VS Code project called "lab1" in the C:\projects\dspic33a folder.

Ensure you are running the VS Code profile created in Lab 0 (MCHP Dev).

VS Code profile

Run the MPLAB Create New Project command (Ctrl + Shift + P) and create a new project called "lab1".

Explorer pane

Perform a build of this project (Ctrl + Shift + B), and verify a successful build:

verify a successful build

 

Back to Top

Part B. Create Program and Debug Launch Configurations

In VS Code, launch targets or launch configurations refer to the settings that define how the debugger should start and interact with your application. These configurations are typically stored in a launch.json file within the .vscode sub-folder in your project workspace. 

You will create a launch.json file in your project, with a Program Project configuration that will be used to program the ELF file into the target, as well as a Debug Project configuration which will be used to start a debug session.

 Create a Debug Project launch configuration, used for debugging your project on the target MCU.

Create a new launch.json file:

  1. Navigate to the Run and Debug button located on the left sidebar, or use the shortcut Ctrl + Shift + D.
  2. Click on the create a launch.json file link.

Run and Debug button

Select MPLAB Debugger from the pull-down and press Enter. (If you have multiple debuggers, you can filter them by typing "MPLAB".)

MPLAB Debugger

Once done, you will have a launch.json file under the .vscode folder with a pre-populated Debug Configuration:

json file

Perform the following updates:

  • edit the "name" property as shown here:
    • "name": "Debug Project"
  • add a "properties" property as shown here:
    • "properties": {"debugoptions.useswbreakpoints": "false"}

The updated "Debug Project" launch configuration should be as shown here:

updated "Debug Project" launch configuration

 


Create a Program Project launch configuration, used for programming the target MCU.

Press the Add Configuration button.

Add Configuration button

Select the MPLAB: Program option from the pull-down:

MPLAB: Program option from the pull-down

You will now see a programming configuration in the launch.json file:

configuration in the launch.json file

Perform the following updates:

  • edit the "name" property as shown here:
    • "name": "Program Project"
  • add a "preLaunchTask" property as shown here:

The updated "Program Project" launch configuration should be as shown here:

updated "Program Project" launch configuration

 

 

Back to Top

Part C. Update main.c With a Code Example

The following code example demonstrates simple digital IO initialization and usage on a dsPIC33A device. The code initializes pin RF0 for sensing switch S2 input, and pin RD9 to drive the red RGB LED output. The main loop will then read the switch level continuously, and set the LED output HIGH when pressed, and LOW when released.

Replace the default application with provided example code.

Open main.c and replace the default application code with the following contents (copy/paste):

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * File:        main.c
 * Project:     lab1
 *
 * Description:
 *
 * Initializes pin RF0 for sensing switch S2 input, and pin RD9 to drive the
 * Red RGB LED output. The main loop will then simply read the switch level
 * continuously, and set the LED output HIGH when pressed, and LOW when released.
 *
 */


#include <xc.h>

#define FCPU        8000000U
#define FPB_FAST    FCPU
#define FPB_STD     FCPU/2
#define FPB_SLO     FCPU/4

#pragma config FWDT_WDTEN = SW

void clocksInit(void);
void pinsInit(void);

int main(void)
{
    INTCON1bits.GIE = 0;        // prevents interrupt flooding since reset state is a '1'    
   clocksInit();               // initialize all clocks
   pinsInit();                 // initialize all pins
   INTCON1bits.GIE = 1;        // enable global interrupts

   while(1)
    {
       if(PORTFbits.RF0)
        {
           // S2 is not pressed
           LATDbits.LATD9 = 0;
        }
       else
        {
           // S2 is pressed
           LATDbits.LATD9 = 1;
        }
    }
   
}

void clocksInit(void)
{
   /*** System Clocks Initialization ***/

   // The FRC provides the source clock
   // Instruction Clock, Fcpu = 8 MHz
   // Peripheral Bus Clocks: Fpb_fast = 8 MHz, Fpb_std = 4 MHz, Fpb_slow = 2 MHz

   // Clock settings left at defaults listed above
}

void pinsInit(void)
{
   /*** I/O Pin Function Initialization ***/
   
   // Initialize RGB Red LED pin
   LATDbits.LATD9 = 0;             // initialize output level
   TRISDbits.TRISD9 = 0;           // make pin digital output

   // Initialize Switch S2 input
   ANSELFbits.ANSELF0 = 0;         // disable analog function
   TRISFbits.TRISF0 = 1;           // make pin digital input

}

 

Press Ctrl + Shift + B to build the project. Fix any syntax errors, ensuring that the project builds successfully

Terminal tab successful build

 

Back to Top

Part D. Debug the Application

Next, we'll connect our board to the PC, select the debugger to use for our project, and then run a debug session to verify the project functionality.

Make sure the kit is connected to your computer.

Connect the dsPIC33A Curiosity Platform Development Board with dsPIC33AK512MPS512 Dual In‑line Module (DIM) to your computer using the USB cable. Verify that the green led (power) is on and that the tool appears in the Tools view (click on the MPLAB icon in the left column or use command MPLAB: Show Tools view): 

dsPIC33A with dsPIC33AK512MPS512

Tools dropdown and MPLAB icon

Note: If the tool doesn't appear:

  • Try to disconnect and connect the tool (USB cable) from the computer.
  • Press Ctrl + Shift + P, then select Developer: Reload Window to restart the MPLAB Extensions for VS Code.

 


Launch the debug configuration.

Start the configured debug session.

Launch the debug configuration by using the keyboard shortcut F5, or navigate to Run and Debug and selecting "Debug Project":

Run and Debug 

When asked to select a tool, select Curiosity/Starter Kits (PKOB4)

Select debug tool

If the launch is successful, the program will break at the start of main:

main.c file

If failing to launch:

  • Verify that the project was successfully built. Verify that your output folder contains a default.elf file. If not, rebuild and check the terminal output.  
  • Verify that the kit is properly connected and has a green LED indicating power.

 

Run Control.

Use the keyboard shortcut F5 to continue, or press the Continue control icon to run the program:

Continue control icon

When running, the application should turn on the red RGB LED when switch S2 is PRESSED:

Red RGB LED when Switch S2 is PRESSED

Once you have an active debug session, you can use the run control icons to do stepping, restart, and pause:

control icons

The following keyboard shortcuts are also available:

Continue / PauseF5 / F6
Step OverF10
Step IntoF11
Step OutShift + F11
RestartShift + Ctrl + F5
StopShift + F5

 

Terminate the debug session.

Use the keyboard shortcut Shift + F5, or press the Stop control icon to terminate the debug session:

Stop control icon

 

Back to Top

Part E. Program the Application Into the MCU

After your application is debugged, you will want to program the executable into the Flash memory of the device. You will use your newly created program project configuration.

Make sure the kit is connected to your computer.


Launch the "Program Project" configuration.

Start the configured program session.

Launch the program configuration by navigating to Run and Debug and selecting "Program Project":

Program Project

Press the Play icon. If asked to select a tool, select Curiosity/Starter Kits (PKOB4)

Curiosity/Starter Kits (PKOB4)

If the programming is successful, the Debug Console window will display the following messages:

Debug Console window

When running, the application should turn on the red RGB LED when switch S2 is PRESSED:

Red RGB LED when Switch S2 is PRESSED

 

Back to Top

Results

Re: Configuring a VS Code Project for Programming and Debugging

At this point, you should be able to configure both debug and program launch configurations in your dsPIC33A VS Code project and use the debugger to run these configurations on your kit.

launch.json file

After a successful "debug" launch, you should be able to perform basic run control using VS Code debug controls.

After a successful "program" launch, you should be able to program your firmware into the target microcontroller (MCU).

Re: dsPIC33A Simple Digital I/O Configuration

For simple digital input/output functions, such as interfacing to LEDs and switches, the following registers need to be correctly initialized:

  • TRISx Register
    • Controls the digital pin direction (input or output)
  • LATx Register
    • Used to write values to pins configured as digital outputs
  • PORTx Register
    • Used to read values from pins configured as digital inputs
Information

Note: Pins that have a shared analog input function (i.e., Analog‑to‑Digital Converter (ADC) or comparator input), default to their analog function on power up.

To use these pins for digital input, the appropriate ANSELx register bit must be cleared to disable analog functionality before configuring the pin as a digital input! 

Back to Top

Next Project

  • Start Lab 2 to learn how to use the Peripheral Pin Select (PPS) feature on dsPIC33A.