UART is the most commonly used communication protocol in microcontrollers due to its simplicity, easy implementation, and minumum hardware requirements.
This tutorial and example code implementation will walk you through the process of achieving successful UART communication between a dspic micro-controller and PC.
Things Needed for UART Communication
The first step should be to gather all the required things necessary for UART communication. The list below summarizes these hardware and software requirements:
1. UART Communication Software for PC
- Option 1: I would prefer the Arduino IDE for this purpose. The Arduino IDE has a serial monitor which can be used to monitor sent data from the micro-controller. It also has an excellent graphical monitor to plot the received data. The same IDE can be used to send serial data also.
- Option 2: The PicKit™ tool also has a serial monitor to send and receive data but it does not have a serial plotter. It also does not supports high baud rates up to 115000 which the Arduino IDE can support.
2. USB to Serial Converter/Transceiver
- Option 1: The PL2302 USB to TTL converter can be very conveniently used as the transceiver between the USB serial port of the PC and the uart TX/RX pins of the micro-controller. It is very cheap and readily available on the internet and local electronic stores (at least in my area).
- Option 2: The programming tool of the Pic micro-controllers, the PicKit™ also has an inbuilt UART transceiver. Advantage of using this is that you will not need to purchase any extra hardware as PicKit will do the job. (Assuming that you already have it since you are programming the Pics.) Downside is that you cannot put the PicKit inside your finished projects as you might need it for later use. In this case the USB to ttl converter is a better option.
- Option 3: A UART to RS232 conversion circuit might be fabricated with the help of MAX232 ICs and RS232 cable. ICs are usually cheap but it will cost you more time to fabricate the circuit. This used to be the legacy solution before the advent of usb to ttl converter modules.
3. dsPic Micro-Controller and Its Programmer
A pic device and its programmer to load the code.
4. A Few Jumper Wires
To make the required connections between the TX/RX pins of the micro-controller and the USB to TTL converter
5. USB Extension Cable (Optional)
In case you need to wire the usb to ttl converter far from your PC when you are finally assembling your project.
After going through the list of things required you can proceed with the steps 1 through 4 below.
1. Install the Required Software and Drivers
Install the serial monitor of your choice on your PC; as I stated earlier, I prefer the Arduino IDE.
If Using USB to TTL Converter:
You will need to download its driver and install them before you can make the module to work. The drivers are available on the Prolific Systems website and elsewhere on the internet. Make sure the drivers you download are compatible with Windows 7, otherwise it will not work. Search for a separate tutorial for driver installation if you are having problems.
After installing the driver you must test the TTL converter to make sure it is working properly. It is recommended that you do not proceed ahead before testing the converter.
To test the converter:
- First install the required driver and reboot the system.
- Plug in the converter module and the PC should notice a new device connection.
- Short the TX and RX pins on the TTl converter with the help of a jumper.
- Open Arduino IDE and select the correct serial port to which the converter is connected.
- Send any data over the serial monitor.
- If you receive the same sent data back in the serial monitor then the module is working correctly.
If Using PicKit:
Just dwonload the Pickit program and connect the PicKit device to your PC.
2. Wire the Connections
For TTL Converter:
|Pin Name on the Converter||Corresponding PIN Name on the Micro-Controller|
5 V (optional)
- Pin names are printed on the back side of the converter.
- Exact pin numbers corresponding to their pin names must be found from the datasheet as they change from device to device.
- A 5 V or 3.3 V supply from the converter may optionally be used to power the micro-controller.
- It is mandatory to make the ground of the controller and converter common.
The PicKit has a 6 pin connector with pin number 1 marked with a white arrow on the device.
|Pin Number on the 6-Pin Header||Corresponding Pin Name on the MCU|
- Additionally the wiring schematic is also shown in an inset inside the PicKit's UART tool.
3. Send Data From dspic Through UART
After wiring the connections and testing the the TTL Converter module to make sure it has started working, upload the code for sending data through UART.
- Text can be sent using the printf() function in the stdio.h library.
- Variables can be sent by first converting them into array and then using printf() to print them.
Example Code to Send Data Through UART
4. Receive Data Through UART in dspic
To receive data from UART you will have to utilize a UART receive interrupt that will trigger every time a data packet is received in the UART receive registers (U1RXREG). This data is transferred to another variable and the receive buffer is cleared if full. You can use the following example code.
- To make sure that the interrupt is actually triggering, an LED has been connected at Port D0 which will blink once when the interrupt is triggered.
- If the LED blinks immediately after you send some data from the serial monitor, assume the code is working correctly.
- The value of variable 'data' might also be checked in the debug mode.
Example Code to Receive Data From UART
This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.
© 2017 StormsHalted
StormsHalted (author) from Pullman, Washington, United States on January 02, 2018:
Please see the table in the article which shows pin names of the UART module and their corresponding pin names on PIC.
Actual pin numbers are to vary, depending on the model and series of PIC used but pin names will always work.
Jeremy on January 01, 2018:
This is fantastic content!
Do you think you could please upload a circuit diagram of how you connected your dsPIC33F and Arduino?