Difference between revisions of "NU32: Serial Communication with the PC"

From Mech
Jump to navigationJump to search
Line 5: Line 5:
== Overview ==
== Overview ==


The NU32 uses an on board [http://www.ftdichip.com/Products/ICs/FT232R.htm FT2232] chip from FTDI to convert [http://en.wikipedia.org/wiki/RS-232 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.
The NU32 uses an on board [http://www.ftdichip.com/Products/ICs/FT2232H.htm FT2232H] chip from FTDI to convert [http://en.wikipedia.org/wiki/RS-232 RS-232 TTL serial communication] to USB. Using drivers from FTDI, the communication can be opened by any program that can access a [http://en.wikipedia.org/wiki/Serial_port virtual serial port]. On the NU32, 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, several programs that can access the serial connection are discussed, including Processing, MATLAB, and terminal programs.


== Details ==
== 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 FT2232H chip is hardwired the to UART1 (F2 and F8, for general communication) and UART4 (D14 and D15, for bootloading) modules on the PIC32M795F512L. The Receive (RX) pins, F2 and D14, and Transmit (TX) pins, F8 and D15, are also brought out to the sides of the NU32 board. '''They should not be used as general IO.'''


Serial communication is asynchronous, it does not use an external clock signal to know when to read individual bits. Instead, the sender and receiver decide beforehand what frequency to transmit data. This frequency is called the baud rate and must match on both the NU32 and computer for the data to be interpreted correctly.
The NU32 uses the UART1 and UART4 modules at 115200 baud, as does the code below, but this can be changed if desired.

The NU32 uses the UART1 and UART4 modules at 115200 baud, as does the sample code below.


== Library Functions ==
== Library Functions ==
[[Media:NU32_serial_example.zip | This code]] contains functions to read characters and write strings over the serial port.
[[Media:NU32_serial_example.zip | This code]] contains functions to read characters and write strings over the serial port. It uses the same header files as "Hello World".
*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.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 these files to use serial communication.

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:
The functions that control the serial communication are:
Line 24: Line 25:
*void PutCharacter(UART_MODULE, const char) - writes an individual character to the computer when the module is able to, called by WriteString()
*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.
NU32_serial_example.c in the .zip file above contains the following 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 ==
== Sample Code ==
Line 40: Line 41:
</pre>
</pre>


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.
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 array with 32 elements.


<pre>
<pre>
Line 47: Line 48:
</pre>
</pre>


To receive characters in an interrupt, use this ISR
To receive characters in an interrupt, use this interrupt service routine (ISR):


<pre>
<pre>
Line 61: Line 62:
}
}


// We don't care about TX interrupt
// If the interrupt was on TX (ignore it)
if(INTGetFlag(INT_SOURCE_UART_TX(UART1))) {
if(INTGetFlag(INT_SOURCE_UART_TX(UART1))) {
INTClearFlag(INT_SOURCE_UART_TX(UART1));
INTClearFlag(INT_SOURCE_UART_TX(UART1));
Line 68: Line 69:
</pre>
</pre>


NU32_serial_example.c initializes the serial communication, and then waits until it receives the character 'a' from the computer. When it gets an 'a', it replies back with numbers 0 through 9, and then waits for another 'a'.
== PuTTY ==
PuTTY is a terminal program that will let you open the virtual serial port created by the FTDI driver.


== Communicating with NU32_Utility ==
Directions to install PuTTY can be found in [[NU32: Software to Install#PuTTY Terminal Emulator]]
The right hand side of the NU32_Utility program is a debugger that will display any character received from the 'Debug' port, and will send any characters in the text entry using 'Send'.


== Communicating with a Terminal Program ==
Before launching PuTTY, note the name of your COM port, same name as you use in the NU32 Utility Application for debugging (not bootloading).
The NU32_Utility debugger is based on a more traditional Terminal program. Terminal programs let you choose the baud rate, flow control, and many other aspects of serial communication, and let you view the incoming bytes as ASCII text or hex bytes. A Terminal program is useful when you want to see hidden characters that the NU32_Utility can not display.


On Windows, you can use [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html PuTTY] or [http://realterm.sourceforge.net/index.html#downloads_Download RealTerm].


On a MAC, you can use [something else].
Launch Putty.exe. Set up your communication by:

When you use a Terminal program, make sure that you know the communication port name and baud before opening the program. '''Also be sure that the port is not already open in another program. A port can only be open is one place at a time!'''

The Terminal programs vary in use, but for example, launch Putty.exe. Set up your communication by:
*Selecting the 'Serial' radio button
*Selecting the 'Serial' radio button


Line 87: Line 94:
*Name your settings in 'Saved Sessions' and click 'Save'
*Name your settings in 'Saved Sessions' and click 'Save'
*Click the 'Open' button
*Click the 'Open' button




== Processing ==
== Processing ==
[http://processing.org/ Processing] is a free IDE that is easy to use, makes nice graphics, and easily performs serial communication. The NU32_Utility was written in Processing.
'''**To Do - Processing basics, see [[Processing]]**'''
'''See the [[Processing]] page for more specific information on Processing.'''


The following code is a simple Processing sketch that will open a serial port and print any characters received to the Debug area.
The [[Media:NU32v2_stripchart.zip | 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,


<pre>
<pre>
Processing code
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);
}
</pre>
</pre>


To send characters to the NU32 with Processing, try the following:
The [[Media:NU32v2_plot.zip | NU32v2 Plotting tool]] can be used to plot 1000 data points in each of four traces, with values between 0 and 511.


<pre>
To plot the values of an array of 1000 integers to trace a, use something like:
more code
</pre>

Processing has the ability to check for incoming characters in parallel to running code in the Draw() routine. Try the following:


<pre>
<pre>
more code
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);
}
</pre>
</pre>


== MATLAB ==
Typing a key while the tool is running will send that character to the microcontroller.
MATLAB can open a serial port and send and receive data as well. This is useful when you need to plot data, perform a complicated mathematical function, or error check a function that you wrote in C to the function in MATLAB.
To write a message to the tool from the microcontroller, '''send a string that does not start with a, b, c or d'''.


To open a port in MATLAB:
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:


<pre>
<pre>
open
void __ISR(_UART_1_VECTOR, ipl2) IntUart1Handler(void) {
</pre>
// Is this an RX interrupt?
if(INTGetFlag(INT_SOURCE_UART_RX(UART1))){
char data = UARTGetDataByte(UART1);


To send data:
// 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);
}


<pre>
// Clear the RX interrupt Flag
code
INTClearFlag(INT_SOURCE_UART_RX(UART1));
</pre>
}


To receive data:
// We don't care about TX interrupt

if(INTGetFlag(INT_SOURCE_UART_TX(UART1))) {
<pre>
INTClearFlag(INT_SOURCE_UART_TX(UART1));
code
}
</pre>
}

Put together, the code looks like:

<pre>
code
</pre>
</pre>


== MATLAB ==
'''**To Do**'''


== More Information ==
== More Information ==


NA
More detailed information on how the the serial module on the PIC32 can be configured is on this page.'''**To Do**'''

Revision as of 17:59, 23 January 2012

-----***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 FT2232H 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 virtual serial port. On the NU32, 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, several programs that can access the serial connection are discussed, including Processing, MATLAB, and terminal programs.

Details

The FT2232H chip is hardwired the to UART1 (F2 and F8, for general communication) and UART4 (D14 and D15, for bootloading) modules on the PIC32M795F512L. The Receive (RX) pins, F2 and D14, and Transmit (TX) pins, F8 and D15, are also brought out to the sides of the NU32 board. They should not be used as general IO.

Serial communication is asynchronous, it does not use an external clock signal to know when to read individual bits. Instead, the sender and receiver decide beforehand what frequency to transmit data. This frequency is called the baud rate and must match on both the NU32 and computer for the data to be interpreted correctly.

The NU32 uses the UART1 and UART4 modules at 115200 baud, as does the sample code below.

Library Functions

This code contains functions to read characters and write strings over the serial port. It uses the same header files as "Hello World".

  • 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 these files 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 in the .zip file above contains the following 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 array 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 interrupt service routine (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)); 
  }

  // If the interrupt was on TX (ignore it)
  if(INTGetFlag(INT_SOURCE_UART_TX(UART1))) {
    INTClearFlag(INT_SOURCE_UART_TX(UART1));
  }
}

NU32_serial_example.c initializes the serial communication, and then waits until it receives the character 'a' from the computer. When it gets an 'a', it replies back with numbers 0 through 9, and then waits for another 'a'.

Communicating with NU32_Utility

The right hand side of the NU32_Utility program is a debugger that will display any character received from the 'Debug' port, and will send any characters in the text entry using 'Send'.

Communicating with a Terminal Program

The NU32_Utility debugger is based on a more traditional Terminal program. Terminal programs let you choose the baud rate, flow control, and many other aspects of serial communication, and let you view the incoming bytes as ASCII text or hex bytes. A Terminal program is useful when you want to see hidden characters that the NU32_Utility can not display.

On Windows, you can use PuTTY or RealTerm.

On a MAC, you can use [something else].

When you use a Terminal program, make sure that you know the communication port name and baud before opening the program. Also be sure that the port is not already open in another program. A port can only be open is one place at a time!

The Terminal programs vary in use, but for example, 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

Processing is a free IDE that is easy to use, makes nice graphics, and easily performs serial communication. The NU32_Utility was written in Processing. See the Processing page for more specific information on Processing.

The following code is a simple Processing sketch that will open a serial port and print any characters received to the Debug area.

    Processing code

To send characters to the NU32 with Processing, try the following:

more code

Processing has the ability to check for incoming characters in parallel to running code in the Draw() routine. Try the following:

more code

MATLAB

MATLAB can open a serial port and send and receive data as well. This is useful when you need to plot data, perform a complicated mathematical function, or error check a function that you wrote in C to the function in MATLAB.

To open a port in MATLAB:

open

To send data:

code

To receive data:

code

Put together, the code looks like:

code


More Information

NA