The purpose of this tutoial is to enable basic serial communication to your computer over the UART subsystem in your PIC microcontroller.
Note: This tutorial continues from the Hello World Tutorial for the PIC18F57Q43 Curiosity Nano. With small changes, it is applicable to a wide range of PIC microcontrollers, especially other Curiosity Nano Boards.
Item | Detail |
---|---|
PIC18F57Q43 Curiosity Nano | Website / User Guide |
Breadboard | |
USB-C Programming cable |
Before working through the tutorial, it is important to answer some key questions about the board. Open up the curiosity nano hardware user guide to find more detailed answers to the following questions:
Question | Answer |
---|---|
Is it possible to communicate with it once programmed over USB? | Yes, see section 4.1.1 and figure 4-1 for details on how the CDC RX/TX pins connect to the microcontroller’s UART pins. |
If closed Open up MPLabX and load your previous hello-world-uart project.
Open MCC by clicking on the MCC icon in the top ribbon of MPLabX
open MCC |
In the MCC window, select the “Device Resource” tab, scroll down and expand the “UART” item, and select “Add UART”
Add UART |
A dropdown list should become visible to the right. Select “UART1”
Select UART1 |
A configuration window should open to the right. Provide additional details for the UART subsystem
Configuration Page |
Go to the pin manager window. Referring to the hardware user guide, figure 4-1, select the appropriate RX and TX pins for the UART subsystem. These should match with the CDC TX and RX pins, respectively.
What do we mean? The CDC RX and TX pins belong to the secondary programmer / communication microcontroller on the curiosity nano board that is wired to the pins of the USB connector. Look at figure 4-1 to see which pins the CDC RX and TX pins connect to on the PIC microcontoller. The CDC RX Pin connects to the microcontroller’s TX pin, and the CDC TX pin connects to the PIC’s RX pin. Set up MCC to match this diagram.
Click the “generate” button to generate your MCC configuration.
Click “generate” |
Once you’ve generated MCC, click on the “clean and compile” button to build your project.
Research: Now it’s time to do a bit more research into the functionality you just added. In the project explorer window, click on “source files” -> “MCC generated files” -> “uart” -> “src” -> “uart1.c” to answer the following questions
navigate to “uart1.c” |
Question | Answer |
---|---|
Which function enables the UART1 subsystem | Look in uart1.c |
Which function tells you when a character is ready to be received? | Look in uart1.c |
Which function receives a single character, and what datatype is returned? | Look in uart1.c |
Which function tells you when the microcontroller is ready to send a single character? | Look in uart1.c |
Which function sends a single character? | Look in uart1.c |
Now we need to open and modify main.c in the project explorer. Inside the curly brackets after the while(1)
statement, comment out your previous code:
__delay_ms(500);
IO_RF3_Toggle();
Your goal is to:
Select the “clean and compile” button to verify your project compiles.
clean and compile |
Select the “download” button, ensuring your Curiosity Nano device is connected.
download |
Next, open up windows device manager and look for communication ports mapped to the curiosity nano. Record which COM port is used by the curiosity nano
Next, open putty (or your serial communication program of choice), and, in serial mode, open up the COM port indicated from the last step. Use the same settings as those you defined in MCC for UART1:
Type something in the window and observe what happens.
You should see the letters you type on the screen. This is a function of the microcontroller transmitting back what it sees. You should also see the user-defined LED on the curiosity nano board toggling each time you type a letter.