0% found this document useful (0 votes)
44 views22 pages

Port Serial: USART - Universal Serial Asynchronous Receiver Transmitter Section 24 of Datasheet

The document describes the USART (Universal Serial Asynchronous Receiver Transmitter) and serial communication features on Arduino boards. It discusses the serial port, serial communication pins, baud rates, frame formats, and various Arduino serial functions like begin(), print(), read(), etc. Block diagrams and descriptions of features like full duplex operation, asynchronous/synchronous modes, interrupts are also provided.

Uploaded by

Devi Ratna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views22 pages

Port Serial: USART - Universal Serial Asynchronous Receiver Transmitter Section 24 of Datasheet

The document describes the USART (Universal Serial Asynchronous Receiver Transmitter) and serial communication features on Arduino boards. It discusses the serial port, serial communication pins, baud rates, frame formats, and various Arduino serial functions like begin(), print(), read(), etc. Block diagrams and descriptions of features like full duplex operation, asynchronous/synchronous modes, interrupts are also provided.

Uploaded by

Devi Ratna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Port Serial

USART – Universal Serial Asynchronous Receiver Transmitter


Section 24 of datasheet
Introduction
• Used for communication between the Arduino board and a computer or
other devices.
• All Arduino boards have at least one serial port Serial.
• It communicates on digital pins 0 (RXD) and 1 (TXD) as well as with
the computer via USB. If you use these functions, you cannot also use
pins 0 and 1 for digital input or output.
• You can use the Arduino environment's built-in serial monitor to
communicate with an Arduino board (Menu Tools – Serial Monitor).
• Click the serial monitor button in the toolbar and select the same baud
rate used in the call to begin().
Features
• Full Duplex Operation (Independent Serial Receive and Transmit Registers)
• Asynchronous or Synchronous Operation
• Master or Slave Clocked Synchronous Operation
• High Resolution Baud Rate Generator
• Supports Serial Frames with 5, 6, 7, 8, or 9 data bits and 1 or 2 stop bits
• Odd or Even Parity Generation and Parity Check Supported by Hardware
• Data OverRun Detection
• Framing Error Detection
• Noise Filtering Includes False Start Bit Detection and Digital Low Pass Filter
• Three Separate Interrupts on TX Complete, TX Data Register Empty and RX Complete
• Multi-processor Communication Mode
• Double Speed Asynchronous Communication Mode
Block Diagram
• Clock Generator
• Transmitter
• Receiver
• Baud Rate Register,
Data Register,
Control and Status
Registers
Frame Formats
• St Start bit, always low.
• (n) Data bits (5, 6, 7, 8, or 9 data bits)
• P Parity bit. Can be odd or even.
• Sp Stop bit, always high.
• IDLE No transfers on the communication line (RxDn or TxDn). An IDLE
line must be high.
Arduino Functions
if (Serial)
Description: Indicates if the specified Serial port is ready.
Syntax : if (Serial)
Parameters : none
Returns : boolean; returns true if the specified serial port is available. This will only return false if querying the
Leonardo's USB CDC serial connection before it is ready.

available()
Description : Get the number of bytes (characters) available for reading from the serial port. This is data that's
already arrived and stored in the serial receive buffer (which holds 64 bytes). available() inherits from the Stream
 utility class.
Syntax : Serial.available()
Parameters : none
Returns : the number of bytes available to read
Arduino Functions
availableForWrite()
Description : Get the number of bytes (characters) available for writing
in the serial buffer without blocking the write operation.
Syntax : Serial.availableForWrite()
Parameters : none
Returns : the number of bytes available to write
Arduino Functions
begin()
Description : Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the
computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200.
You can, however, specify other rates - for example, to communicate over pins 0 and 1 with a component that requires a
particular baud rate.
• An optional second argument configures the data, parity, and stop bits. The default is 8 data bits, no parity, one stop bit.
Syntax : Serial.begin(speed)
Serial.begin(speed, config)
Parameters : speed : baud rate (in bits per second) - long;
config: sets data (5, 6, 7, 8, or 9), parity (N, E, O), and stop bits (1, 2).
Valid values are : SERIAL_5N1, SERIAL_6N1, SERIAL_7N1, SERIAL_8N1 (the default), SERIAL_5N2,
SERIAL_6N2, SERIAL_7N2, SERIAL_8N2, SERIAL_5E1, SERIAL_6E1, SERIAL_7E1, SERIAL_8E1,
SERIAL_5E2, SERIAL_6E2, SERIAL_7E2, SERIAL_8E2, SERIAL_5O1, SERIAL_6O1, SERIAL_7O1,
SERIAL_8O1, SERIAL_5O2, SERIAL_6O2, SERIAL_7O2, SERIAL_8O2
Returns : nothing
Arduino Functions
end()
Description : Disables serial communication, allowing the RX and TX pins to be used for general input and
output. To re-enable serial communication, call Serial.begin() without parameter.
Syntax : Serial.end()
Parameters : none
Returns : nothing

find()
Description : Serial.find() reads data from the serial buffer until the target string of given length is found. The
function returns true if target string is found, false if it times out.
• Serial.find() inherits from the Stream utility class.
Syntax : Serial.find(target)
Parameters : target : the string to search for (char)
Returns : boolean
Arduino Functions
findUntil()
Description : Serial.findUntil() reads data from the serial buffer until a target string of given length or terminator
string is found.
• The function returns true if the target string is found, false if it times out.
• Serial.findUntil() inherits from the Stream utility class.
Syntax : Serial.findUntil(target, terminal)
Parameters : target : the string to search for (char)
terminal : the terminal string in the search (char)
Returns : boolean

flush()
Description : Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead
removed any buffered incoming serial data.)
• flush() inherits from the Stream utility class.
Syntax : Serial.flush()
Parameters : none
Returns : nothing
Arduino Functions
parseFloat()
Description : Serial.parseFloat() returns the first valid floating point
number from the Serial buffer. Characters that are not digits (or the
minus sign) are skipped. parseFloat() is terminated by the first character
that is not a floating point number.
• Serial.parseFloat() inherits from the Stream utility class.
Syntax : Serial.parseFloat()
Parameters : none
Returns : float
Arduino Functions
parseInt()
Description : Looks for the next valid integer in the incoming serial stream. parseInt() inherits from the 
Stream utility class.
In particular:
• Initial characters that are not digits or a minus sign, are skipped;
• Parsing stops when no characters have been read for a configurable time-out value, or a non-digit is
read;
• If no valid digits were read when the time-out (see Serial.setTimeout()) occurs, 0 is returned;
Syntax : Serial.parseInt()
Serial.parseInt(char skipChar)
Parameters : skipChar: used to skip the indicated char in the search. Used for example to skip thousands
divider.
Returns : long : the next valid integer
Arduino Functions
peek()
Description : Returns the next byte (character) of incoming serial data without removing it
from the internal serial buffer. That is, successive calls to peek() will return the same
character, as will the next call to read().
peek() inherits from the Stream utility class.
Syntax : Serial.peek()
Parameters : None
Returns : the first byte of incoming serial data available (or -1 if no data is available) - int

print(), println(), read(), readBytes(), readBytesUntil(), readString(), readStringUntil(),


setTimeout(), write(), serialEvent()
Arduino Functions
print(), println()
Description : Prints data to the serial port as human-readable ASCII text. This command can take many
forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII
digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent
as is.
An optional second parameter specifies the base (format) to use; permitted values are BIN (binary, or base
2), OCT (octal, or base 8), DEC (decimal, or base 10), HEX (hexadecimal, or base 16). For floating point
numbers, this parameter specifies the number of decimal places to use. For example:
You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :
Serial.print(F(“Hello World”))
To send a single byte, use Serial.write().
Syntax : Serial.print(val)
Serial.print(val, format)
Parameters : val: the value to print - any data type
format: specifies the number base (for integral data types) or number of decimal places (for
floating point types)
Returns : size_t (long): print() returns the number of bytes written, though reading that number is optional
Arduino Functions
read()
Description : Reads incoming serial data. read() inherits from the 
Stream utility class.
Syntax : Serial.read()
Parameters : None
Returns : the first byte of incoming serial data available (or -1 if no data
is available) - int
Arduino Functions
readBytes()
Description : Serial.readBytes() reads characters from the serial port into a buffer.
The function terminates if the determined length has been read, or it times out (see 
Serial.setTimeout()).
Serial.readBytes() returns the number of characters placed in the buffer. A 0 means
no valid data was found.
• Serial.readBytes() inherits from the Stream utility class.
Syntax : Serial.readBytes(buffer, length)
Parameters : buffer: the buffer to store the bytes in (char[] or byte[])
length : the number of bytes to read (int)
Returns : byte
Arduino Functions
readBytesUntil()
Description : Serial.readBytesUntil() reads characters from the serial buffer into an array.
The function terminates if the terminator character is detected, the determined length has
been read, or it times out (see Serial.setTimeout()).
Serial.readBytesUntil() returns the number of characters read into the buffer. A 0 means no
valid data was found.
• Serial.readBytesUntil() inherits from the Stream utility class.
Syntax : Serial.readBytesUntil(character, buffer, length)
Parameters : character : the character to search for (char)
buffer: the buffer to store the bytes in (char[] or byte[]) length : the number of
bytes to read (int)
Returns : byte
Arduino Functions
readString()
Description : Serial.readString() reads characters from the serial buffer
into a string. The function terminates if it times out (see setTimeout()).
• This function is part of the Stream class, and is called by any class that
inherits from it (Wire, Serial, etc). See the Stream class main page for
more information.
Syntax : Serial.readString()
Parameters : none
Returns : A string read from the serial buffer
Arduino Functions
readStringUntil()
Description : readStringUntil() reads characters from the serial buffer into a
string. The function terminates if the terminator character is detected or it times
out (see setTimeout()).
• This function is part of the Stream class, and is called by any class that inherits
from it (Wire, Serial, etc). See the Stream class main page for more
information.
Syntax : Serial.readStringUntil(terminator)
Parameters : terminator : the character to search for (char)
Returns : The entire string read from the serial buffer, until the terminator
character is detected
Arduino Functions
setTimeout()
Description : Serial.setTimeout() sets the maximum milliseconds to wait
for serial data when using Serial.readBytesUntil(), Serial.readBytes(), 
Serial.parseInt() or Serial.parseFloat(). It defaults to 1000 milliseconds.
• Serial.setTimeout() inherits from the Stream utility class.
Syntax : Serial.setTimeout(time)
Parameters : time : timeout duration in milliseconds (long).
Parameters : None
Arduino Functions
write()
Description : Writes binary data to the serial port. This data is sent as a byte or series of bytes;
to send the characters representing the digits of a number use the print() function instead.
Syntax : Serial.write(val)
Serial.write(str)
Serial.write(buf, len)
Parameters : val: a value to send as a single byte
str: a string to send as a series of bytes
buf: an array to send as a series of bytes
len: the length of the buffer
Returns : byte
write() will return the number of bytes written, though reading that number is optional
Arduino Functions
serialEvent()
Description : Called when data is available. Use Serial.read() to capture this
data.
• NB : Currently, serialEvent() is not compatible with the Esplora,
Leonardo, or Micro
Syntax
void serialEvent(){
//statements
}
Parameters : statements: any valid statements

You might also like