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

From Mech
Jump to navigationJump to search
Line 1: Line 1:
'''Under construction NDM 1/25/2010'''
'''Under construction NDM 1/25/2010'''


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.
Digital inputs and outputs (DIO) are the simplest of interfaces between the PIC and other electronics, sensors, and actuators. The PIC32 has many DIO pins, each of which can take one of two states: high or low.


== Overview ==
== Overview ==


The NU32v2 uses an on board [http://www.ftdichip.com/Products/ICs/FT232R.htm FT233RL] chip from FTDI to convert [http://en.wikipedia.org/wiki/RS-232 RS-232 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 Serial 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 PIC32


== Details ==
== Details ==


The functions of the DIO pins are controlled by special function registers (SFRs). Each of these SFRs has 32 bits on the PIC32, but for many, not all 32 are relevant. Depending on whether these bits are set to 0 or 1, different actions or settings are performed:


* '''AD1PCFG''': This SFR determines which of the 16 pins with an analog input function (port B) is treated as an analog input or a DIO pin. The 16 most significant bits (high bits) AD1PCFG<31:16> are ignored, but AD1PCFG<15:0> determine which of the 16 pins is treated as an analog input. 0 indicates analog input, and 1 indicates DIO. So if the bit AD1PCFG<7> is 0, then the pin with the function AN7 is an analog input; if AD1PCFG<7> is 1, then it is a DIO. AD1PCFG resets to 0x0000, so all 16 pins of port B are treated as analog inputs by default. To make AN15 and AN1 analog inputs and the rest DIO, we can use the C command <tt>AD1PCFG = 0x7FFD</tt>. To make AN15 and AN1 analog inputs but leave the other settings unchanged, we can use the C command <tt>AD1PCFG &= 0x7FFD</tt>. Note we only have to specify 16 bits, or 4 hex digits, as the 16 most significant bits have no role. Instead of acting on the entire SFR, we could instead address an individual bit, using <tt>AD1PCFGbits.PCFG15 = 1</tt>, for example, to set the value of a single bit.


== Library Functions ==
<!--
A block diagram of a typical input/output port is shown below, taken from the Reference Manual.




[[Image:pic-gpio-module.png|center]]


OK, this looks pretty complicated, so let's go through it systematically. First, there may be some symbols you are unfamiliar with in this figure. These are all digital logic. Consult the figure below:

<table border=1 cellpadding=5 align=center>
<tr>
<td>'''Symbol'''</td>
<td>'''Name'''</td>
<td>'''Description'''</td>
</tr>
<tr>
<td>[[Image:pic-hysteresis-buffer.png]]</td>
<td>Schmitt trigger</td>
<td>The output (on the left) is digital, high or low, and matches the input if it is high or low. Because the input may be noisy and take values in between clearly low and clearly high, the gate implements "hysteresis." This means that if the output is currently low, the input must go quite high before the output will switch to high, and if the output is high, the signal must go quite low before the output will switch.</td>
</tr>
<tr>
<td>[[Image:pic-tristate-buffer.png]]</td>
<td>tri-state buffer</td>
<td>If the input coming in from the top is high, the output matches the input. If it is low, the output is high impedance (disconnected).</td>
</tr>
<tr>
<td>[[Image:pic-multiplexer.png]]</td>
<td>multiplexer</td>
<td>Depending on the input coming in from the top, one or the other inputs is passed through to the output.</td>
</tr>
<tr>
<td>[[Image:pic-dflipflop.png]]</td>
<td>D flip-flop</td>
<td>If the D flip-flop is enabled (bottom input), then the input at D is latched through to the output Q when the clock CK goes high. The output only changes on a rising clock edge, and does not change if the flip-flop is not enabled. </td>
</tr>
<tr>
<td>[[Image:pic-logic-or.png]]</td>
<td>logic OR</td>
<td>Outputs the logical OR of the two inputs.</td>
</tr>
<tr>
<td>[[Image:pic-invert-and.png]]</td>
<td>logic AND (with inverted input)</td>
<td>Outputs the logical AND of the two inputs (where one input is inverted, as indicated by the circle).</td>
</tr>
</table>
-->

== Library Functions ==

The peripheral library offers a number of function calls to help you use the DIO pins. These functions have names such as <tt>PORTRead</tt>, <tt>PORTSetPinsDigitalIn</tt>, <tt>PORTSetPinsDigitalOut</tt>, etc. C source code can be found in pic32-libs/peripheral/ports/source/*.c, and .h header files can be found in pic32-libs/include/peripheral/ports.h. Documentation can be found in your MPLAB C32's doc directory, in Microchip-PIC32MX-Peripheral-Library.chm. The DIO functions are simple enough, however, that we will usually just set the appropriate SFRs manually, as described above and demonstrated in the samples below.


== Sample Code ==
== Sample Code ==


The example
This example uses two of the PIC32 pins as digital outputs and one pin as digital input. In particular, it uses pins RG12 and RG13 as outputs, since these are connected to LEDs on the NU32v2 board (by the NU32v2 circuit schematic, the associated LED is on when the pin is low). It configures RG6 as an input for a button press. If RG6 is high, the LEDs will be off. If RG6 is low, the LEDs will be on. Let's assume RG6 is low when the button is pressed (as it is with the NU32v2 and your button in the breadboard).


<pre>
<pre>
code
/*
</pre>
* Simple digital input/output.
*/


And more
#include <plib.h>


== PuTTY ==
// No need to configure the configuration bits controlling clock speed, etc.;
// these definitions are in the bootloader.


== Processing ==
// Lines below could go into a separate .h file, since they're the same
// for all projects using the NU32v2 and the NU32v2 bootloader.
#define LED0 LATGbits.LATG12
#define LED1 LATGbits.LATG13
#define SW PORTGbits.RG6
#define SYS_FREQ 80000000 // 80 MHz

int main(void) {
// Turn on the pre-fetch cache, choose the maximum possible peripheral
// bus frequency, and choose minimum flash wait states. Could be done in
// bootloader. See pic32-libs/include/peripheral/system.h.
SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL);

AD1PCFG = 0xFFFF; // sets all port B to DIO. Only needed if using port B.

// Initialize LEDs as output, switch as input. Could be done in bootloader.
// Note we don't technically have to set switch as an input, since it's the
// default, but let's do it in case someone inserts code before this line.
TRISGCLR = 0x3000; TRISGSET = 0x0040;

while(1) {
while(!SW) { // if button is pressed, turn LEDs on
LED0 = 0; LED1 = 0;
}
LED0 = 1; LED1 = 1; // when button is released turn LEDs off
}
return 0;
}
</pre>


== MATLAB ==
Now, for fun, let's make a couple of small changes to the code.


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


More detailed information on how the the serial module on the PIC32 can be configured is on this page (DNE).
More information on the I/O ports can be found in Chapter 12 of the Data Sheet and Chapter 12 of the Reference Manual. Some special function registers (SFRs) are listed in Chapter 4 of the Data Sheet. The SET, CLR, and INV registers are described in Section 2 of the Reference Manual.

Revision as of 16:18, 25 January 2011

Under construction NDM 1/25/2010

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

Library Functions

Sample Code

The example

code

And more

PuTTY

Processing

MATLAB

More Information

More detailed information on how the the serial module on the PIC32 can be configured is on this page (DNE).