0% found this document useful (0 votes)
19 views39 pages

Communication 1 - UART

UART - microchip 16f877a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views39 pages

Communication 1 - UART

UART - microchip 16f877a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Microcontrollers

(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

Tp. Hồ Chí Minh – 2024


Digital Communication
• The process of exchanging information (data) between the devices by
using digital signal.
Tx (Transmitter) Rx (Receiver)

Device 1 Data Device 2

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 Tx Rx Device Device Tx Rx Device Tx Rx


Device Device
1 2 1 2 1 2

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.

Parallel data pins


Serial communication
- In serial communication, data is transmitted sequentially via a single or couples of
lines.
- The number of lines is equal to the length of the data word.
- The bits of the data word are transmitted one by one via as a pulse chain.

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 vs Parallel Communication - Newhaven Display


Communication|4
Digital Communication
Serial and Parallel Data transfer

Serial Parallel

Complex at very high Easier to synchronize at


Synchronization
speeds short distances

High-speed scalability Can be easily scaled for


Scalability
can be challenging short distances
Requires fewer wires, Requires more wires,
Wiring
reducing bulk increasing bulk
Bandwidth limited by
Bandwidth High bandwidth potential
channel characteristics

Serial vs Parallel Communication - Newhaven Display


Communication|5
Digital Communication
Data, Frame, and Field
- The information in digital communication are represented by binary
bits (0, 1).
- A “frame” is a group of bits that is sent and received per one time.
- Inside the frame, the fields are groups of bits with different functions.
The data chunks (bits contain data) is data field.

Communication|6
Digital Communication
Data encoding types
- To recognize the bit value (0, 1),
the voltage levels should be
encoded.

Non-return to zero (NRZ)


0: Low, 1: High
Return to zero (RZ)
1: High, then return to Low.
0: Low
Non - return to zero inverted (NRZI)
1: Edge appears.
0: No edge.
Return to Zero Inverted (RZI)
1: Low voltage.
0: High ,then return to Low
Manchester Coding
1: Falling edge, 0: Rising edge
Communication|7
Universal Synchronous/ Asynchronous
Receiver/ Transmitter (USART)
• A Universal Synchronous/Asynchronous Receiver/Transmitter interface is
a device able to transmit data word serially using two I/Os, one acting as
transmitter (TX) and one as receiver (RX).
• The Synchronous types use an extra clock signal for data synchronization.

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).

Data Bits: Data field. Common configurations include 7 or 8 data bits.

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.

Baud Rate: number of bits transmitted in 1 second.

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) {

// Configure UART settings


TXSTAbits.SYNC = 0; // Asynchronous mode
RCSTAbits.SPEN = 1; // Enable serial port
TXSTAbits.TXEN = 1; // Enable transmitter
RCSTAbits.CREN = 1; // Enable continuous receive

// Configure baud rate


unsigned int baud_rate = (_XTAL_FREQ / (16 * baudrate)) - 1;
SPBRG = baud_rate; // Load baud rate value into SPBRG register
}

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

// Configure baud rate


unsigned int baud_rate =
(_XTAL_FREQ / (16 * baudrate)) - 1;
SPBRG = baud_rate; // Load baud
rate value into SPBRG register

Note: Since the clock of the MCU is not


synchronized to baud rate. We should select
the baud rate carefully to suppress baud
rate error

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);

void UART_Transmit(unsigned char data);


unsigned char UART_Receive();

unsigned char received_data = 0;


// Main function Initialization
void main() {
// Initialize UART with baud rate 9600 UART Polling
UART_Init(9600);

// Receive and echo received data


while(1) {
received_data = UART_Receive();
__delay_ms(200);
UART_Transmit(received_data);
}
} Communication|30
Example 1: Echo via COM Port
Step 3: Code to Transmit and Receive Data
Implement the function
// Transmit data via UART
void UART_Transmit(unsigned char data) {
while(!TXSTAbits.TRMT); // Wait for transmit buffer
to be empty
TXREG = data; // Write data to transmit buffer
}
Flag to know there is transmission (data goes)
Buffer
// Receive data via UART
unsigned char UART_Receive() {
while(!RCIF); // Wait for data to be received
return RCREG; // Read received data
}
Flag to know there is reception (data comes)
Buffer

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)

Group 1 Group 2 Group 3


- External (RB0) Peripheral 1: Peripheral 2:
Enable specific - TMR0 - ADC - Oscillator fail
interrupts - PORTB on-change - USART - Comparator 1, 2
- SSP - EEPROM write
- CCP1 - Bus collision
- Timer 1, Timer 2 - CCP2
Config interrupt - Ultra-low power
wake-up
INTCON INTCON INTCON
PIE1 PIE2
Interrupt Status
INTCONT PIR1 PIR2

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();

// Global variable to store received data


volatile uint8_t receivedData;
void main() {
Data receive interrupt. IRQ
// Initialize UART
invokes when data comes
UART_Init();

// Enable global and UART receive interrupts


GIE = 1;
PEIE = 1;
RCIE = 1;
// Main loop
while(1) {
// Main code
}
}
Communication|34
Example 2: Echo via COM Port using Interrupts
void UART_Init() {
// Set baud rate to 9600 (assuming Fosc = 8MHz)
BRGH = 1; // High speed baud rate
SPBRG = 51; // ((Fosc/baud rate)/16) - 1

// 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
}

void __interrupt() ISR() {


if (RCIF && RCIE) { // Check if UART receive interrupt
occurred
receivedData = UART_Read(); // Read received data
__delay_ms(200);
UART_Write(receivedData);
RCIF = 0;
}
}
Communication|36
Serial COM Port
Hercules

Communication|37
Serial COM Port
Arduino Terminal

Communication|38
Serial COM Port
Debug with MPLab X IDE and Pickit 3

Communication|39

You might also like