Communication 1 - UART
Communication 1 - UART
(ME-3007)
Communication –
UART
Phung Thanh Huy
Department of Mechatronics
Ho Chi Minh City University of Technology
Vietnam National University Ho Chi Minh City
Ver2024.1
Transmission
• Data can be transmitted in one direction or both directions
Only 1 direction 2 directions, but 1 per time 2 directions
Simplex Half duplex Full duplex
Device Device
1 2
Rx Tx
Communication|2
Digital Communication
Serial and Parallel Data transfer
Parallel communication
- In parallel communication, data is transmitted simultaneously via the lines.
- The number of lines is equal to the length of the data word.
Communication|3
Digital Communication
Serial and Parallel Data transfer
Serial Parallel
Typically slower for short
Typically faster as multiple
Speed distances than parallel
bits are sent at once
communication.
Complexity Simple for long distances Simple for short distances
Typically cheaper for long Typically more expensive for
Cost
distances long connections
May suffer signal
Reliable over long
Reliability degradation over long
distances
distances
More prone to crosstalk in
Interference Less prone to crosstalk
longer connections
Serial Parallel
Communication|6
Digital Communication
Data encoding types
- To recognize the bit value (0, 1),
the voltage levels should be
encoded.
Communication|8
Universal Synchronous/ Asynchronous
Receiver/ Transmitter (USART)
Synchronous Transmission
Referred as USART
Communication|9
Universal Synchronous/ Asynchronous
Receiver/ Transmitter (USART)
Asynchronous Transmission
Referred as UART
Communication|10
Universal Synchronous/ Asynchronous
Receiver/ Transmitter (USART)
• A UART/USART defines the signaling method, but not the voltage
levels.
• The voltage levels are based on MCU I/Os, which is almost equal to
VDD.
• The way the voltage levels are translated to allow serial
communication outside the board is demanded to other
communication standards.
• The EIA-RS232 or EIA-RS485 are two popular standards that define
signaling voltages, in addition to their timing and meaning, and the
physical size and pinout of connectors.
• UART/USART interfaces can be used to exchange data using other
physical and logical serial interfaces such as USB interface.
Communication|11
USART/ UART on PIC
• In PICs, USART: Can be configured to be Synchronous or
Asynchronous.
• PICs use EUSART (Enhanced USART) – Non-to-zero coding
• Voltage logic levels: TTL
• If USART is configured as Synchronous: half-duplex. If USART is
configured as Asynchronous: Full-duplex
• UART: Asynchronous transmission.
Communication|12
UART on PIC
Parameters and Data
Start Bit: The first bit in the data frame and indicates the beginning of the
data transmission. Usually low level (0).
Parity Bit (optional): The parity bit is set to either a 0 or 1 to ensure that
the total number of bits with a value of 1 in the data frame (including the
parity bit) is either even (even parity) or odd (odd parity). This allows the
receiver to detect certain types of transmission errors.
Stop Bit(s): indicate the end of the data transmission. Usually high level
(1). The number of stop bits is usually 1 or 2 bits.
Communication|13
UART on PIC
Pin out
UART on PIC16F887/ 877a use RC6 and RC7 to transmit and received data
Communication|14
UART on PIC
Registers
• Enable: TXSTA
• Configure the protocol BAUDCTL, SPBRG, SPBRGH
• Send data, get data from a buffer to send. Buffer: TXREG
• Interrupt: recognize data sending INTCON, PIE1, PIR1
Communication|15
UART on PIC
Registers
• Enable: TXSTA
• Configure the protocol BAUDCTL, SPBRG, SPBRGH
• Receive data, get data from a buffer register. Buffer: RCREG.
• Interrupt: recognize data receiving : INTCON, PIE1, PIR1
Communication|16
UART on PIC
Registers
Communication|17
UART on PIC
Registers
Communication|18
Example 1: Echo via COM Port
Problem
Write a program, PIC receive the message from the Computer via COM Port
and Feed back the same message
UART to RS232
PL2303. UART to USB COM. (Other ICs are OK!)
Need drivers
Note: UART is not RS232! RS232, RS485, USB… are standards that signal from UART
is converted.
Communication|19
Example 1: Echo via COM Port
Step 1: Configure the MCU
Communication|20
Example 1: Echo via COM Port
Step 2: Init UART: Reception and transmission
Enable UART
Operation of the USART/UART is controlled by two registers:
• TXSTA and
• RCSTA
• The port is enabled by the SPEN bit of RCSTA,
• and selection of synchronous or asynchronous modes is by the SYNC bit
of the TXSTA register.
SYNC = 0; // Default
Communication|21
Example 1: Echo via COM Port
Step 2: Init UART: Reception and transmission
Configure Transmission and UART
Communication|22
Example 1: Echo via COM Port
Step 2: Init UART: Reception and transmission
Configure Reception and PORT
Communication|23
Example 1: Echo via COM Port
Step 2: Init UART: Reception and transmission
Configure Transmission
Communication|24
Example 1: Echo via COM Port
Step 2: Init UART: Reception and transmission
// Initialize UART
void UART_Init(unsigned long baudrate) {
Communication|25
Example 1: Echo via COM Port
Step 2: Init UART: Reception and transmission
Control the Baud Rate
• Baud Rate (bit per sec) is the data transfer speed.
• Since Asynchronous Communication does not share a clock between the
devices, they need to define a baud rate to understand each other.
Communication|26
Example 1: Echo via COM Port
Step 2: Init UART: Reception and transmission
Control the Baud Rate
• Baud Rate Generator (BRG) is an 8-bit or 16-bit timer that is dedicated to the
support of both the asynchronous and synchronous EUSART operation.
• By default, the BRG operates in 8-bit mode. Setting the BRG16 bit of the
BAUDCTL register selects 16-bit mode.
Communication|27
Example 1: Echo via COM Port
Step 2: Init UART: Reception and transmission
Control the Baud Rate
1, Select the baud rate.
Some popular baud rate
110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600,
115200, 128000 and 256000
Communication|28
Example 1: Echo via COM Port
Step 2: Init UART: Reception and transmission
Control the Baud Rate
2, Calculate the values to assign the register
Communication|29
Example 1: Echo via COM Port
Step 3: Code to Transmit and Receive Data Prototype declaration
#define _XTAL_FREQ 4000000
#include <xc.h>
// Function prototypes
void UART_Init(unsigned long baudrate);
Communication|31
Interrupt and UART
Communication|32
Interrupt and UART
To use interrupt with UART:
1, Initialize UART (as usual)
2, Enable and configure Interrupt.
3, Set up ISRs
Enable Interrupt Globally INTCON (Interrupt Control)
Communication|33
Example 2: Echo via COM Port using Interrupts
#define _XTAL_FREQ 8000000
void UART_Init();
void UART_Write(uint8_t data); Prototypes
uint8_t UART_Read();
void __interrupt() ISR();
// Configure UART
SYNC = 0; // Asynchronous mode
SPEN = 1; // Enable serial port pins Calculate and assign
CREN = 1; // Enable reception
TXEN = 1; // Enable transmission
}
Enable UART
Communication|35
Example 2: Echo via COM Port using Interrupts
void UART_Write(uint8_t data) {
// Wait for transmit buffer to be empty
while(!TXIF);
TXREG = data; // Transmit data
}
uint8_t UART_Read() {
// Wait for data to be received
while(!RCIF);
return RCREG; // Return received data
}
Communication|37
Serial COM Port
Arduino Terminal
Communication|38
Serial COM Port
Debug with MPLab X IDE and Pickit 3
Communication|39