NU32: Serial Communication with the PC
-----***UNDER CONSTRUCTION NDM 1/23***-----
The NU32 has the ability to talk directly with a computer over a virtual serial port connection. This ability can be used to debug code, transfer data, and interact with the microcontroller using a computer.
Overview
The NU32 uses an on board FT2232 chip from FTDI to convert RS-232 TTL serial communication to USB. Using drivers from FTDI, the communication can be opened by any program that can access a serial port. On the NU32 side, sample code has been created to enable the UART module on the PIC32. Using this code you can write strings to the computer, and generate an interrupt when characters are received. On the computer side, several programs that can access the serial connection are discussed, including PuTTY, Processing and MATLAB.
Details
The FT2232 chip is hardwired to the UART1 (for general communication) and UART4 (for bootloading) modules on the PIC32M795F512L. The Receive (RX) pins, xx and xx, and Transmit (TX) pins, xx and xx, are also brought out to the sides of the NU32 board. They should not be used as general IO.
The NU32 uses the UART1 and UART4 modules at 115200 baud, as does the code below, but this can be changed if desired.
Library Functions
This code contains functions to read characters and write strings over the serial port.
NU32.h contains function prototypes, in addition to the serial functions, to initialize the LEDs on the NU32 board as well as the bootloader button.
NU32.c contains the functions. You do not need to change this file or the .h file need to use serial communication.
The functions that control the serial communication are:
- void initSerialNU32(void) - enables UART1 at 115200 baud with an interrupt at priority level 2
- void WriteString(UART_MODULE, const char *) - call with UART1 and the character array you wish to send
- void PutCharacter(UART_MODULE, const char) - writes an individual character to the computer when the module is able to, called by WriteString()
NU32_serial_example.c contains example code that shows how to use the functions. It also contains the ISR for UART1, with an example of how to receive characters.
Sample Code
To initialize the serial communication, call initSerialNU32():
initSerialNU32();
To write a string to the computer, use WriteString(UART1, charArray)
WriteString(UART1, "\r\nHello World!\r\n");
To write a string with the value of a variable in it, use sprintf(RS232_Out_Buffer,charArray) and WriteString(UART1, charArray). RS232_Out_Buffer is declared in NU32.h as a char with 32 elements.
sprintf(RS232_Out_Buffer, "the value of i is: %d\r\n", i); WriteString(UART3, RS232_Out_Buffer);
To receive characters in an interrupt, use this ISR
void __ISR(_UART_1_VECTOR, ipl2) IntUart1Handler(void) { // Is this an RX interrupt? if(INTGetFlag(INT_SOURCE_UART_RX(UART1))){ char data = UARTGetDataByte(UART1); // now do something with data // Clear the RX interrupt Flag INTClearFlag(INT_SOURCE_UART_RX(UART1)); } // We don't care about TX interrupt if(INTGetFlag(INT_SOURCE_UART_TX(UART1))) { INTClearFlag(INT_SOURCE_UART_TX(UART1)); } }
PuTTY
PuTTY is a terminal program that will let you open the virtual serial port created by the FTDI driver.
Directions to install PuTTY can be found in NU32: Software to Install#PuTTY Terminal Emulator
Before launching PuTTY, note the name of your COM port, same name as you use in the NU32 Utility Application for debugging (not bootloading).
Launch Putty.exe. Set up your communication by:
- Selecting the 'Serial' radio button
Select the 'Serial' branch in the tree on the bottom left.
- Enter the name of your COM port in 'Serial line to connect to'
- Enter 115200 in 'Speed (baud)'
- Change 'Flow control' from 'XON/XOFF' to 'None'
Go back to the 'Session' screen by selecting the 'Session' in the top left of the tree.
- Name your settings in 'Saved Sessions' and click 'Save'
- Click the 'Open' button
Processing
**To Do - Processing basics, see Processing**
The NU32v2 Stripchart plotting tool can be used to plot 1000 data points in each of six traces, with values between 0 and 511, where each new data point is added to the right side of the plot, stripchart style. For example,
for (i=0; i<1000; i++) { // plot on trace a the refValue and on trace b the ADC value sprintf(RS232_Out_Buffer, "%d %d\r\n", refValue[i]/2, adcValue[i]/2); // max adc is 1023, max plotted value is 511, // so divide by 2 before sending WriteString(UART1, RS232_Out_Buffer); }
The NU32v2 Plotting tool can be used to plot 1000 data points in each of four traces, with values between 0 and 511.
To plot the values of an array of 1000 integers to trace a, use something like:
for (i=0; i<1000; i++) { sprintf(RS232_Out_Buffer, "a %d %d\r\n", i, adcValue[i]/2); // max adc is 1023, max plotted value is 511, // so divide by 2 before sending WriteString(UART1, RS232_Out_Buffer); }
Typing a key while the tool is running will send that character to the microcontroller. To write a message to the tool from the microcontroller, send a string that does not start with a, b, c or d.
For example, to change the value of a control gain when the microcontroller receives an 'a' and return the new value of the control gain, try:
void __ISR(_UART_1_VECTOR, ipl2) IntUart1Handler(void) { // Is this an RX interrupt? if(INTGetFlag(INT_SOURCE_UART_RX(UART1))){ char data = UARTGetDataByte(UART1); // now do something with data if (data == 'a') { // increase kp kp=kp+0.01; sprintf(RS232_Out_Buffer, " kp=%4.2f\r\n", kp); WriteString(UART1, RS232_Out_Buffer); } // Clear the RX interrupt Flag INTClearFlag(INT_SOURCE_UART_RX(UART1)); } // We don't care about TX interrupt if(INTGetFlag(INT_SOURCE_UART_TX(UART1))) { INTClearFlag(INT_SOURCE_UART_TX(UART1)); } }
MATLAB
**To Do**
More Information
More detailed information on how the the serial module on the PIC32 can be configured is on this page.**To Do**