嵌入式系统代写代做代考 Embedded Systems Reconfigurable computing

Reconfigurable computing

Small Embedded Systems

Unit 4.1
Asynchronous Serial Communication

Introduction
Serial and Parallel communications
Characteristics of asynchronous communication
Transmission media
Using virtual serial ports to send data to and from a microcontroller
Arduino functions for serial port transfers

Suppose I have an 8 bit data word to transfer
How should I design the bus?
Parallel communication: 8 parallel wires
Whole data word can be sent in one go
Communication with External Devices

Word 1

Word 2

Serial Busses
Suppose I have an 8 bit data word to transfer
How should I design the bus?
1-bit wide
Transmit bits one after the other
8 time slots (at least) to transfer 1 data word

Slower, but saves pins

5

4

3

2

1

Asynchronous Serial Communication
Asynchronous:
Sender does not provide clock to receiver
Device that manages asynchronous serial transfer is UART
Universal Asynchronous Receiver Transmitter
Both ends of communication must agree on data rate
Both ends of communication must apply exactly the same protocol
Data bits (how many bits? lsb first or msb first?)
Synchronization bits
Parity bits
Baud rate

Baud Rate
Baud rate is number of symbols per second
In a simple system, the symbols are 1 or 0
two possible values, i.e. one bit of information
baud rate is the same as bit rate
Some systems (e.g. DSL internet-through-phone-line) use different voltage levels and phase shifts to allow a single symbol to take hundreds (or thousands) of different possible values
Each symbol can encode many bits
Bit rate is much higher than baud rate
For serial port communications, we normally assume simple 1,0 symbols and bit rate is same as baud rate

Physical Interface
A variety of physical interfaces can be used:
TTL: 1 is in range 3.3V to 5V; 0 is 0V
RS232: 1 is in range -3V to -15V; 0 is +3V to +15V
RS422 and RS485 use differential signalling over a twisted pair to get longer range at higher data rates
Line driver is used to connect UART to physical interface (voltage shifting, etc.)
Common ground may also be sent

Communicating device 1

Communicating device 2
Line driver
Line driver
TX
TX
RX
RX

Physical Interface
RS2322 was originally devised to connect dumb terminals to computers
Voltage levels are high to get satisfactory transmission over long and noisy cables
Data rates depend on distance, but here are some typical figures

Standard Voltage signals Max distance Max speed Number of devices per port

RS232 Logic 0: +3V to +15V
Logic 1: -3V to -15V ~15 m ~20 kbps 1 master 1 receiver
RS-422 Differential (-6V to +6V) 12 m
120 m
1200 m 10 Mbps
1 Mbps
100 kbps 1 master 10 receivers

RS-485 12 m
120 m
1200 m
10 Mbps
1 Mbps
100 kbps Up to 32 transmitter/receiver pairs

Physical Interface
Single ended signalling uses a high or low voltage (relative to ground). There is no cancellation of noise that is injected into the signal as it travels
Differential signalling uses a pair of wires:
Logic 1: wire A goes high and wire B goes low
Logic 0: wire A goes low and wire B goes high
Noise that is injected is a common-mode signal that is removed by subtraction

Diagram by Jdc1197, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=59618480

Some RS232 problems
Low transmission speed
Voltage swing
power consumption
power supply design
Large connectors
Baud rate needs to be set manually
But…
Can still be preferable to USB when cabling over long (>5m) distances

Virtual Serial Ports
Serial port asynchronous communications can be piggybacked over some other medium
USB
Bluetooth
and is a standard way to talk to embedded systems
We have seen how we can send data from microcontroller to host PC using Arduino Serial library:

Serial.begin(9600);
Serial.print(“Hello”);
Serial.print(val);
Serial.print(val, BIN);
Serial.print(val, HEX);
Serial.println(val);

Start a connection at 9600 baud
Print a string
Print the value of a variable
Print the value in binary
Print the value in hexadecimal
Print value followed by newline

Sending Data to the Microcontroller
We can also send data from the PC to the microcontroller (though the process is a little more complicated)
To check if there is any data available for the microcontroller to read, we use the function

This returns the number of bytes that have been sent to the microcontroller

We read a single byte using the function

This returns a char value

Serial.available();
Serial.read();

Sending Data to the Microcontroller
Here is an example that will turn the LED on when ‘1’ is pressed on the host PC, and turn it off when ‘0’ is pressed

const int LED=13;
char data;

void setup()
{
Serial.begin(9600);
pinMode(LED, OUTPUT);
}

Holds the incoming character
Start serial communication

Sending Data to the Microcontroller
Here is an example that will turn the LED on when ‘1’ is pressed on the host PC, and turn it off when ‘0’ is pressed

void loop()
{
if (Serial.available() > 0){
data = Serial.read();
if (data == ‘1’) {
digitalWrite(LED, HIGH);
Serial.println(“LED ON”);
} else if (data == ‘0’) {
digitalWrite(LED, LOW);
Serial.println(“LED OFF”);
}
}
}

Only act if there is data
Read a byte
Turn on if the byte was char ‘1’
Turn off if the byte was char ‘0’

Sending more than just one byte
The Serial.read() function just gets one byte (type char)
Not very convenient
The function Serial.parseInt() continues reading until it reaches a non-numeric character and then converts the bytes up to that point into a number, which is returned.

Suppose our input data stream was 123,201,345
Serial.parseInt() would return the integer value 123 the first time it is called
Serial.parseInt() would return the integer value 201 the second time it is called
And so on …

Sending more than just one byte
Here is an example that will turn the LED on when an integer smaller than 150 is typed on the host PC, and turn it off when an integer ≥150 is typed

void loop()
{
if (Serial.available() > 0){
int val = Serial.parseInt();
if (val < 150) { digitalWrite(LED, HIGH); Serial.println("LED ON"); } else { digitalWrite(LED, LOW); Serial.println("LED OFF"); } } } Only act if there is data Read an int Turn on if the int was <150 Turn off if the int was ≥150 Summary Asynchronous communication does not assume a shared clock Baud rate and data format must be agreed in advance by both ends of the link UART handles low level details of asynchronous serial communications /docProps/thumbnail.jpeg