0% found this document useful (0 votes)
13 views

Class5_serial_4499720bc7a8e92d93bc02127b475680

Uploaded by

a7madafefa
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)
13 views

Class5_serial_4499720bc7a8e92d93bc02127b475680

Uploaded by

a7madafefa
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/ 30

EE470

Microcontrollers and
Embedded Systems
Week 8: Serial Communication

Dr. Ahmad Bashaireh


Jordan University of Science and Technology
Fall 2023

1
Learning Objectives

• Contrast and compare serial versus parallel communication


• Explain serial communication protocol
- Contrast half- versus full-duplex transmission
- Contrast synchronous versus asynchronous communication
- Explain the process of data framing
- Interface AVR with RS232 devices
- Discuss the baud rate of the AVR
• Describe serial communication features of the AVR
• Program the AVR serial port in C

2
ATmega328 Pin-out

3
Parallel vs Serial

• Parallel • Serial
• For short distances • Less wires
• Not applicable for long distances • Slower
• More expensive
• Cross-talk problem

Sepehr Naimi, Sarmad Naimi, and Muhammad Ali Mazidi. 2017. The AVR Microcontroller and Embedded Systems Using Assembly and C: Using Arduino Uno and Atmel Studio [Textbook Slides]. Retrieved from : https://nicerland.com/avr/ 4
Communication Modes

• Simplex

• Half-Duplex

• Full-Duplex

5
Synchronous vs Asynchronous

• Synchronous:

• Asynchronous:

6
Common Serial Interfaces

• USART (RS232)
• USB
• Controller Area Network (CAN)
• Serial Peripheral Interface (SPI)
• Inter-Integrated Circuit(I2C)
• Ethernet
• 1-wire

7
RS232

• USART
• Common in old PCs
• 9 pins (or 25)
• 3 lines minimum required: TxD, RxD and ground
• Voltage levels:
- 0: +3 to +15 V
- 1: -15 to -3 V
• RS422: twisted pairs

8
TTL-to-RS232: MAX232 IC

Sepehr Naimi, Sarmad Naimi, and Muhammad Ali Mazidi. 2017. The AVR Microcontroller and Embedded Systems Using Assembly and C: Using Arduino Uno and Atmel Studio [Textbook Slides]. Retrieved from : https://nicerland.com/avr/ 9
Serial Communication - USART

• Universal Synchronous/Asynchronous Receiver Transmitter


• Full-Duplex
• Uses three wires (at least):
- Transmit (TX)
- Receive (RX)
- Ground

10
UART Data Framing

• Sender and receiver must predetermine how to communicate


• Clock:
1
- Baud rate (bitrate): number of signal changes per second 𝑇𝑏𝑖𝑡 =
baudrate
• Common values: 300, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, and 115200

• Frame:
- Start bit
- Data bits (5,6,7,8 or 9)
- Stop bit(s)
- Parity: optional
Start LSB X X X X X MSB Parity Stop
11
UART Data Framing

Start LSB X X X X X X MSB Stop

12
UART in ATMega328P

Reg. Name Function

UCSR0 (A/B/C) Control and Status Register

UDR0 I/O Data Register

UBRR0(H/L) Baud rate Register

13
UART – ATMega328P Registers
UDR0 RXB[7:0]
TXB[7:0]

UCSR0A RXC0 TXC0 UDRE0 FE0 DOR0 UPE0 U2X0 MPCM0

UCSR0B RXCIE0 TXCIE0 UDRIE0 RXEN0 TXEN0 UCSZ02 RXB80 TXB80

UCSZ01 / UCSZ00 /
UCSR0C UMSEL01 UMSEL01 UPM01 UPM00 USBS0 UCPOL0
UDORD0 UCPHA0

UBRR0L UBRR0[7:0]

UBRR0H UBRR0[3:0]

14
UDR0
UDR0 RXB[7:0]
TXB[7:0]

• Two registers at the same address


- In hardware: two separate registers
- In software: same
• Read: RXB is returned
• Write: TXB is updated
• For 5-, 6-, or 7-bit characters the upper unused bits will be ignored by
the Transmitter and set to zero by the Receiver.

15
UCSR0A
UCSR0A RXC0 TXC0 UDRE0 FE0 DOR0 UPE0 U2X0 MPCM0

• RCX0: USART Receive Complete


- One: unread data in the receive buffer
- Zero: the receive buffer is empty
- can be used to generate a Receive Complete interrupt
• TXC0: USART Transmit Complete
- One: Transmit Shift Register is shifted out and UDR0 is empty
- Cleared: write 1 to it or automatically when a transmit complete interrupt is executed
- can generate a Transmit Complete interrupt
• UDRE0: USART Data Register Empty
- One: UDR0 is empty
- can generate a Data Register Empty interrupt
16
UCSR0A
UCSR0A RXC0 TXC0 UDRE0 FE0 DOR0 UPE0 U2X0 MPCM0

• FE0: Frame Error


- One: frame does not match “standard” (stop bit is zero)
• DOR0: Data OverRun
- One: Lost character; RXC0 is set (the receive buffer is full) and a new start bit detected
• UPE0: USART Parity Error
- One: parity error
- Cleared: automatically when UDR0 is read
• U2X0: Double the USART Transmission Speed
- Set: 8 samples/bit instead of 16
• MPCM0: Multi-processor Communication Mode
- Set: multi-device communication 17
UCSR0B
UCSR0B RXCIE0 TXCIE0 UDRIE0 RXEN0 TXEN0 UCSZ02 RXB80 TXB80

• RXCIE0: RX Complete Interrupt Enable 0


• TXCIE0: TX Complete Interrupt Enable 0
• UDRIE0: USART Data Register Empty Interrupt Enable 0
• RXEN0: Receiver Enable 0
- Set: enabled. (override normal port operation for the RX pin)
• TXEN0: Transmitter Enable 0
- Set: enabled. (override normal port operation for the TX pin)
• UCSZ02: Character Size 0 (See UCSR0C)
• RXB80: Receive Data Bit 8 0
- 9th data bit of the received character (if frames with nine data bits). Must be read before reading the low bits from
UDR0.
• TXB80: Transmit Data Bit 8 0
- 9th data bit in the (if frames with nine data bits). Must be written before writing the low bits to UDR0. 18
UCSR0C
UCSR0C UMSEL01 UMSEL01 UPM01 UPM00 USBS0 UCSZ01 UCSZ00 UCPOL0

• UMSEL0n: USART Mode Select 0 n [n = 1:0]


UMSEL0[1:0] Mode
00 Asynchronous USART
01 Synchronous USART
10 Reserved
11 Master SPI (MSPIM)

• UPM0n: USART Parity Mode 0 n [n = 1:0]


UPM0[1:0] Mode
00 Disabled
01 Reserved
10 Enabled, even parity
11 Enabled, odd parity 19
UCSR0C
UCSR0C UMSEL01 UMSEL01 UPM01 UPM00 USBS0 UCSZ01 UCSZ00 UCPOL0

• USBS0: USART Stop Bit Select 0


- Cleared: 1-bit UCSZ0[3:0] Character Size
- Set: 2-bit 000 5-bit
• UCSZ0: USART Character Size 001 6-bit
010 7-bit
• UCPOL0: Clock Polarity 0 011 8-bit
- for synchronous mode only 100 Reserved
- Set: TX Data Changed at Falling XCK0 Edge 101 Reserved
RX Data Changed at Rising XCK0 Edge 110 Reserved
- Cleared: TX Data Changed at Rising XCK0 Edge 111 9-bit
RX Data Changed at Falling XCK0 Edge
20
UBRR0
UBRR0L UBRR0[7:0]

UBRR0H UBRR0[11:8]

• USART Baud Rate 0 Register: 12-bit register which contains the USART baud rate
• Ongoing transmissions by the Transmitter and Receiver will be corrupted if the baud
rate is changed.
• Writing UBRR0L will trigger an immediate update of the baud rate prescaler.
Operating Mode Baud Rate (bps) UBRRn Value
𝑓𝐶𝑃𝑈 𝑓𝐶𝑃𝑈
Asynchronous Normal mode (U2Xn = 0) Baud = UBRRn= −1
16 𝑼𝑩𝑹𝑹𝒏+1 16 𝐵𝐴𝑈𝐷
𝑓𝐶𝑃𝑈 𝑓𝐶𝑃𝑈
Asynchronous Double Speed mode (U2Xn = 1) Baud = UBRRn= −1
8 𝑼𝑩𝑹𝑹𝒏+1 8 𝐵𝐴𝑈𝐷
21
Baud rate Calculations
Baud Rate U2Xn = 0 U2Xn = 1
[bps] UBRRn Error UBRRn Error
• 9600 bps 2400 416 –0.1% 832 0.0%
4800 207 0.2% 416 –0.1%
9600 103 0.2% 207 0.2%
14.4k 68 0.6% 138 –0.1%
19.2k 51 0.2% 103 0.2%
28.8k 34 –0.8% 68 0.6%
• 115200 bps 38.4k
57.6k
25
16
0.2%
2.1%
51
34
0.2%
–0.8%
76.8k 12 0.2% 25 0.2%
115.2k 8 –3.5% 16 2.1%
230.4k 3 8.5% 8 –3.5%
250k 3 0.0% 7 0.0%
0.5M 1 0.0% 3 0.0%
1M 0 0.0% 1 0.0%
2M - - 0 0.0%

22
Clock Generation Logic, Block Diagram

23
UART – Programming the AVR (TX)

1. Enable the transmitter:


- UCSR0B = 0x08;
2. Asynchronous mode with 8-bit data frame, no parity, and one stop bit:
- UCSR0C = 0x06;
3. Baud rate: (Table 24-7 in the datasheet; slide 21)
- UBRR0 = 0x??;
4. The character byte to be transmitted serially is written into the UDR0
register
5. Monitor UCSR0A.UDRE0 (wait till it goes high)
6. Repeat 4 and 5 till end of transmission

24
UART – Programming the AVR (RX)

1. Enable the receiver:


- UCSR0B = 0x10;
2. Asynchronous mode with 8-bit data frame, no parity, and one stop bit:
- UCSR0C = 0x06;
3. Baud rate: (Table 24-7 in the datasheet; slide 21)
- UBRR0 = 0x??;
4. Monitor UCSR0A.RXC0 (wait till it goes high)
5. Read UDR0 register
6. Repeat 4 and 5

25
ee470avr.h
void serial_init(unsigned int ubrr) {
UBRR0 = ubrr;
UCSR0A = 0;
UCSR0B = 0b00011000; // no int.,Rx&Tx enable
UCSR0C = 6; // asynch mode, 8bit, 1stop, no parity
}
void putch( unsigned char ch ) {
// first wait for empty transmit buffer
while(bittst(UCSR0A,UDRE0)==0);
UDR0 = ch; // send character return;
}
unsigned char getch(void) {
// first wait till byte received (start,data bits,stop)
while(bittst(UCSR0A,RXC0)==0);
return(UDR0); // return received char as the value of the function
}

26
ee470avr.h
void putch( unsigned char ch ) {
// first wait for empty transmit buffer
while(bittst(UCSR0A,UDRE0)==0);
UDR0 = ch; // send character return;
}
void putstr(char line[]) {
unsigned char i=0;
// in C-language, strings are NULL (ASCII 0) terminated arrays of characters
while(line[i] != '\0’) {
putch(line[i]);
i++;
}
}

27
ee470avr.h
// using the ee470avr.h getch and putch functions
#include "ee470avr.h"
void main(void)
{ unsigned char c;
serial_init(103); // 9600, 8d, 1stop, no parity
while(1)
{ c = getch(); // wait for character from kbd
putch(c); // send it to screen
}
}

28
Example
/* mnn2ec11x17.c
Program the ATmega328 to receive bytes of data serially and put them on Port B.
Set the baud rate at 9600, 8-bit data, and 1 stop bit. Use Receive Complete interrupt instead
polling */
#include “ee470avr.h”
ISR(USART_RX_vect) {
PORTB = UDR0;
}
int main (void) {
DDRB = 0xFF; //make Port D an output
bitset(UCSR0B,RXEN0);
bitset(UCSR0B,RXCIE0); //enable receive and RXC int
bitset(UCSR0C,UCSZ01);
bitset(UCSR0C,UCSZ00);
UBRR0 = 103;
sei(); //enable interrupts
while (1); //wait forever
}

29
Example
/* mnn2ec11x18.c:
Program the ATmega328 to send “G” serially.
Set the baud rate at 9600, 8-bit data, and 1 stop bit. Use interrupt instead of polling */
#include “ee470avr.h”
ISR(USART_UDRE_vect) {
UDR0='G';
}
int main (void) {
DDRB = 0xFF; //make Port B an output
bitset(UCSR0B,TXEN0);
bitset(UCSR0B,UDRIE0); //enable transmit and UDRIE0 int.
bitset(UCSR0C,UCSZ01);
bitset(UCSR0C,UCSZ00);
UBRR0 = 103;
sei(); //enable interrupts
while (1); //wait forever
}

30

You might also like