|
|
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
| Signal | Teensy 1.0 | Teensy 2.0 | Teensy++ 1.0 | Teensy++ 2.0 |
| Receive (Rx) | Pin 2 | Pin 7 | Pin 2 | Pin 2 |
| Transmit (Tx) | Pin 3 | Pin 8 | Pin 3 | Pin 3 |
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.
Example Code
This 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 Object
To 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 Functions
All 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.
|