| ||
|
Shopping Cart
|
| Home | MP3 Player | 8051 Tools | All Projects | PJRC Store | Site Map |
|
You are here:
Teensy
| Search PJRC |
|
Using the UART (Real Serial Port)The Teensy and Teensy++ contain a UART which is the same as the serial port on a standard Arduino. However, it is not used for programming and can be used independantly of the USB Serial object.Hardware
The serial signals on the Rx and Tx pins are "TTL level". To convert to RS-232 level a MAX232 or similar chip is needed.
TODO: Bluetooth hardware example - using "BlueSMiRF" module TODO: Modbus RS-485 example TODO: Show 2 Teensy boards connected to each other Example CodeThis simple example shows how to use both the UART and USB Serial at the same time. Both are monitored for incoming bytes, and when either receives data, the results are printed to both.// This line defines a "Uart" object to access the serial port HardwareSerial Uart = HardwareSerial(); void setup() { Serial.begin(9600); Uart.begin(38400); } void loop() { int incomingByte; if (Serial.available() > 0) { incomingByte = Serial.read(); Serial.print("USB received: "); Serial.println(incomingByte, DEC); Uart.print("USB received:"); Uart.println(incomingByte, DEC); } if (Uart.available() > 0) { incomingByte = Uart.read(); Serial.print("UART received: "); Serial.println(incomingByte, DEC); Uart.print("UART received:"); Uart.println(incomingByte, DEC); } } Instantiate Uart ObjectTo use the Uart object, you must add line to your program which creates the Uart object, of type HardwareSerial(). Simply copy this line to the beginning of your program, before any place you use Uart.
// This line defines a "Uart" object to access the serial port
HardwareSerial Uart = HardwareSerial();
If you prefer a different name, you can replace "Uart" with any name,
such as "Serial1", "RealSerialPort", or "WhateverYouLike".
Standard Serial FunctionsAll of the standard Serial functions are supported.Uart.begin()Initialize the Uart object. The baud rate must be given.Uart.print() and Uart.println()Print a number or string. Uart.print() prints only the number or string, and Uart.println() prints it with a newline character.// Uart.print() can print many different types int number = 1234; Uart.println("string"); // string Uart.println('a'); // single character Uart.println(number); // number (base 10 if 16 or 32 bit) Uart.println(number, DEC); // number, base 10 (default) Uart.println(number, HEX); // number, base 16/hexidecimal Uart.println(number, OCT); // number, base 8/octal Uart.println(number, BIN); // number, base 2/binary Uart.println(number, BYTE); // number, as a single byte Uart.println(3.14); // number in floating point, 2 digits Uart.write()Transmit a byte.Uart.available()Returns true if at least 1 byte is available, or false if nothing has been received.
Uart.read()Read 1 byte (0 to 255), if available, or -1 if nothing available. Normally Uart.read() is used after Uart.available(). For example:if (Uart.available()) { incomingByte = Uart.read(); // will not be -1 // actually do something with incomingByte } Uart.flush()Discard any received data that has not been read.Usable Baud RatesSerial communcation can tolerate about 2.5% error. Because the baud rates are created using the CPU clock, which is not an exact multiple of standard baud rates, higher speed baud rates can not be used if the clock is set (in the Tools > CPU Speed menu) to slower speeds.
Arduino at 57600 Baud - It's Actually 58824Arduino creates the same baud rates as Teensyduino, except at 57600, where Arduino's error is +2.12%. When communicating with an Arduino board, the combined +2.12% and -0.79% error is too much. Successful communication with Arduino requires 58824 baud.
HardwareSerial Uart = HardwareSerial(); void setup() { Uart.begin(58824); // Arduino 57600 is actually 58824 } Alternately, if you edit the Arduino code to use "Serial.begin(57601)", Arduino will create a baud rate with only -0.79% error, which exactly matches Teensy. The Arduino bootloader on Duemilanove and Diecimila, and the "8u2" chip on Arduino Uno when set to 57600, are always 58824 baud. These are not easily reconfigured, so setting Teensy to 58824 baud is the best solution if you must communicate with these. |