NU32v2: Serial Communication with the PC

From Mech
Jump to navigationJump to search

THIS PAGE REFERS TO A PRE-RELEASE VERSION OF THE NU32 PIC32 DEVELOPMENT BOARD. FOR INFORMATION, SAMPLE CODE, AND VIDEOS RELATED TO THE PRODUCTION VERSION (2016 AND LATER), AND TO THE CORRESPONDING BOOK "EMBEDDED COMPUTING AND MECHATRONICS WITH THE PIC32 MICROCONTROLLER," VISIT THE NU32 PAGE.


The NU32v2 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 NU32v2 uses an on board FT233RL 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 NU32v2 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 FT232RL chip is hardwired to the UART3 module on the PIC32M795F512L. The Receive (RX) pin, G7, and Transmit (TX) pin, G8, are also brought out to the sides of the NU32v2 board. They should not be used as general IO.

The NU32v2 Serial Bootloader uses the UART3 module at 115200 baud, as does the code below, but this can be changed as desired.

Library Functions

This code contains functions to read characters and write strings over the serial port.

NU32v2.h contains function prototypes, in addition to the serial functions, to initialize the LEDs on the NU32v2 board as well as the bootloader button.

NU32v2.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 initSerialNU32v2(void) - enables UART3 at 115200 baud with an interrupt at priority level 2
  • void WriteString(UART_MODULE, const char *) - call with UART3 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()

NU32v2_serial_example.c contains example code that shows how to use the functions. It also contains the ISR for UART3, with an example of how to receive characters.

Sample Code

To initialize the serial communication, call initSerialNU32v2():

  initSerialNU32v2();

To write a string to the computer, use WriteString(UART3, charArray)

  WriteString(UART3, "\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(UART3, charArray). RS232_Out_Buffer is declared in NU32v2.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_3_VECTOR, ipl2) IntUart3Handler(void) {
  // Is this an RX interrupt?
  if(INTGetFlag(INT_SOURCE_UART_RX(UART3))){
    char data = UARTGetDataByte(UART3);

    // now do something with data

    // Clear the RX interrupt Flag
    INTClearFlag(INT_SOURCE_UART_RX(UART3)); 
  }

  // We don't care about TX interrupt
  if(INTGetFlag(INT_SOURCE_UART_TX(UART3))) {
    INTClearFlag(INT_SOURCE_UART_TX(UART3));
  }
}

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 NU32v2: Software to Install#PuTTY Terminal Emulator

Before launching PuTTY, note the name of your COM port, same name as you use in the Serial Bootloader Application.


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(UART3, 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(UART3, 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_3_VECTOR, ipl2) IntUart3Handler(void) {
  // Is this an RX interrupt?
  if(INTGetFlag(INT_SOURCE_UART_RX(UART3))){
    char data = UARTGetDataByte(UART3);

    // 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(UART3, RS232_Out_Buffer);
    }

    // Clear the RX interrupt Flag
    INTClearFlag(INT_SOURCE_UART_RX(UART3)); 
  }

  // We don't care about TX interrupt
  if(INTGetFlag(INT_SOURCE_UART_TX(UART3))) {
    INTClearFlag(INT_SOURCE_UART_TX(UART3));
  }
}

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**