Skip to content

In this project, Melody will be used to configure the PIC18F56Q71 Curiosity Nano (Cnano) board to receive and output single ASCII characters over the integrated Serial/CDC port.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/pic18f56q71-basic-uart-comms-mcc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCHP

Basic Bidirectional UART Communication Using the PIC18F56Q71

Overview

This project uses the MPLAB® Code Configurator (MCC) Melody graphical programming tool to configure the PIC18F56Q71 device populating a Curiosity Nano (Cnano) board to receive and transmit single ASCII characters over the integrated serial (CDC) port. When the switch on the Cnano board is pressed, the pin is driven LOW, firmware will then trigger the UART peripheral to transmit the ASCII character 'S' over the UART that is converted to the USB protocol using integrated hardware on the Cnano so that it can then be displayed in the MPLAB Data Visualizer terminal window within the MPLAB® X IDE. When the ASCII character 'T' is entered in the Data Visualizer terminal window, the value is transmitted over USB and then converted to the UART protocol. A UART receive flag will trigger firmware to check the value, if it is an 'T' the LED on the Cnano board will toggle ON/OFF.

Basic Bidirectionsl UART Communications

Note: The following project can be used as a reference for most PIC® devices, with some modifications.

Related Documentation

Software Used

Hardware Used

Hardware Configuration using MCC Melody

Create a new project inside of MPLAB X IDE and open MCC Melody by either clicking the icon at the top of the IDE or by selecting Tools>Embedded>MPLAB Code Configurator v.x: Open/Close.

Once inside of the MCC Melody plug-in, add an instance of the UART peripheral from the Device Resources tab:

Add UART Peripherals

Once added, configure the UART as follows:

Next, configure the UART by selecting the UART(None) instance in either the Project Resources panel or the Builder tab to open the Easy View window.

Select UART(None)

The PIC18F56Q71 populating the Cnano board features two UART peripherals. The correct UART must be selected as the hardware that converts the UART signal to and from USB connectors to specific pins on the device. Determining which UART and pins to use can be found in the PIC18F56Q71 Cnano schematic. A link to the schematic is available in the Kit Window which opens whenever a Cnano board is connected to the computer and MPLAB X IDE is open or by selecting Window>Kit Window inside of the IDE.

Curiosity Nano Kit Window

The first page of the schematic will feature a connection interface diagram including the UART and associated pins needed to interface with the integrated Serial/CDC port. In this instance, connect UART2 to pins RB4 for the transmit signal (TX) and RB5 for the receive signal (RX).

Curiosity Nano Kit Window

The integrated 'LED' is connected to the pin RC7 and the switch is connected to pin RA0.

In Easy View select UART2 from the UART PLIB Selector.

Select UART2

The image below presents the Builder tab:

UART2 Builder

Click the UART2 Library/Driver block to open the associated Easy View. Inside of the Easy View, edit the Requested Baudrate value, ensuring that it is 9600.

Ensure Baudrate is 9600

All other configuration parameters can be left at their default.

In the Pin Grid View tab at the bottom of the Melody interface, connect the UART2>RX2 signal to pin RB5 (package pin #17) and the UART2>TX2 signal to pin RB4 (package pin #16) by clicking the lock icon associated with them:

UART2 Pin Grid View Configuration

Next, configure the pins associated with the Curiosity Nano switch and LED.

In the Pin Grid View tab, connect pin RA0 (package pin #21) that is connected to the switch (SW0) on the Curiosity Nano board as a General Purpose Input/Output (GPIO) input and pin RC7 (package pin #1) connected to the LED (LED0) as an output by clicking the appropriate grid square for the associated signal:

Connecting Switch and 'LED' pins

Go to Project Resources>System>Pins to open the associated Easy View .

Select Pins

In the Custom Name column, rename pin RA0 to 'SW0' and pin RC7 to 'LED'. This will make it easier to navigate and use the generated API.

Rename Pins

Note from the Curiosity Nano schematics that the 'SW0' is active LOW and connected directly to pin RA0 through a current limiting resistor. To eliminate unknown states, the switch pin will need to have a pull-up resistor. The PIC18F56Q71 device family features integrated Weak Pullups which can be enabled by checking the box for the associated pin in the Weak Pullup column of the Easy View :

Enabling Weak Pullups

Finally, a delay is implemented in this project using the DELAY Library. Add an Delay instance from the Device Resources from the Timer menu.

Adding the DELAY Library

The MCC Melody project will now resemble the image below:

Final Project

Click the Generate button in Project Resources to generate the API based on the above configurations:

Generate Code

Using the Generated API

Navigate to the Projects tab in MPLAB X IDE. Locate the main.c source file by expanding the folders basicUARTcomms_Q71>Source Files>main.c. Double click main.c to open and do the following:

Scroll down to main().

Wen the switch is pressed, according to the Cnano schematics, the signal on the SW0 (formerly RA0) pin will be driven LOW. When this occurs, the UART will be triggered to transmit an 'S' ASCII character over it's TX line.

To check the pin, add an if statement that will use the SW0_GetValue() which return the current pin state. If that value is LOW, then execute code to transmit 'S' character by adding that as an argument to the UART2_Write('S') macro. A 350 ms delay is next added using the DELAY_milliseconds(350) macro to allow the user to release the switch before the next pin check has a chance to occur.

An API generated macro or function can be added by starting with typing the peripheral and then on a Windows machine using the CTRL+space feature to pull up a list of available libraries and double clicking the one to add.

Adding an API Function or Macro

The user then need only add the parameters required, in this case the 'S', and ensuring that the semicolon is added at the end of the line of code. In addition, when adding the SW0_GetValue macro the () will need to be added by the user.

Adding UART2_Write()

Next, a second if statement is used to check if a character has been received over the UART using the UART2_IsRxReady() macro. If it is TRUE, an 8-bit variable rxValue initialized prior to the while(1) loop is loaded with the received value using the UART2_Read(). This value is then checked using a final if statement for the 'T' ASCII character. If this condition is TRUE, the voltage level of the pin connected to the LED (RC7) is toggle HIGH or LOW (depending on previous state), using the LED_Toggle() macro.

The final code should resemble the image below:

Final Firmware

Program the Cnano Board and Configure the Data Visualizer Plugin

Program the PIC18F56Q71 by hitting the Make and Program Device Main Project button at the top of the IDE:

Make and Program Device

The Data Visualizer MPLAB X IDE plug-in will be used to verify the operation of the project.

Click the Data Visualizer plugin button at the top of the IDE to open:

Open Data Visualizer

The PIC18F56Q71 Curiosity Nano board is recognized by the Data Visualizer, including the associated COM port. Note that the COM port number may differ than what is shown below.

Data Visualizer

The terminal window will be the only one used, so the rest can be closed, as shown below. Begin communication between the COM port and terminal by hovering the COM port instance and selecting the Display as text in the terminal button:

Select Terminal

Operation

Verify the 'SW0' pin level code by pressing the switch on the Curiosity Nano board. The character 'S' will be output to the terminal.

Verify UART receive firmware is working by clicking inside of the terminal window and entering the character 'T'. This action will toggle the LED on the Curiosity Nano board ON/OFF. Entering any other character will have no effect.

Verify Operation

Summary

This example demonstrates basic UART transmit and receive functionality using a PIC18F56Q71 microcontroller and MPLAB X with MCC Melody.

About

In this project, Melody will be used to configure the PIC18F56Q71 Curiosity Nano (Cnano) board to receive and output single ASCII characters over the integrated Serial/CDC port.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published