Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

UART

Programming in
AVR (Part 2)
LECTURE# 17
MICROPROCESSOR SYSTEMS AND INTERFACING

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 1


Last Lecture
Revision of Timers
Quiz 2
USART (UART) registers in AVR ATmega328p

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 2


Example 2
◦ What are the values of UCSRB and UCSRC needed to configure USART for
asynchronous operating mode, 8 data bits (character size), no parity, and 1
stop bit? Enable both receive and transmit.
◦ Write a program for the AVR to set the values of UCSRB and UCSRC for this
configuration.
.INCLUDE "M32DEF.INC“

LDI R16,(1<<RXEN0)|(1<<TXEN0)
OUT UCSR0B, R16

LDI R16,(1<<UCSZ01)|(1<<UCSZ00) ;Character size = 8 bits


OUT UCSR0C, R16

UCSR0A RXC0 TXC0 UDRE0 FE0 DOR0 UPE0 U2X0 MPCM0

UCSR0B RXCIE0 TXCIE0 UDRIE0 RXEN0 TXEN0 UCSZ02 RXB80 TXB80

UCSR0C UMSEL01 UMSEL00 UPM01 UPM00 USBS0 UCSZ01 UCSZ00 UCPOL0

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 3


Example 3
In Example 1, set the baud rate to 1200 and write a program for the AVR
to set up the values of UCSRB, UCSRC, and UBRR. (Focs = 8 MHz)
Solution:
.INCLUDE "M32DEF.INC"
LDI R16,(1<<RXEN0)|(1<<TXEN0)
OUT UCSR0B, R16

LDI R16,(1<<UCSZ01)|(1<<UCSZ00)
OUT UCS0RC, R16 ;move R16 to UCSR0C

LDI R16,0x9F;see baud rate table


OUT UBRR0L,R16;1200 baud rate
LDI R16,0x1;Higher byte of the UBRR
OUT UBRR0H,R16;access UBRR0H

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 4


Transmit
Programming the AVR to transfer data serially
◦ In programming the AVR to transfer character bytes serially, the following
steps must be taken:
1. The UCSR0B register is loaded with the value 08H, enabling the USART
transmitter. The transmitter will override normal port operation for the
TxD pin when enabled.
2. The UCSR0C register is loaded with the value 06H, indicating
asynchronous mode with 8-bit data frame, no parity, and one stop bit.
3. The UBRR0 is loaded with one of the values in Table 4 (if Fosc = 8 MHz)
to set the baud rate for serial data transfer.
4. The character byte to be transmitted serially is written into the UDR0
register.
5. 5. Monitor the UDRE0 bit of the UCSR0A register to make sure UDR is
ready for the next byte.
6. To transmit the next character, go to Step 4.

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 5


Example 4 (Transmit)
Write a program for the AVR to transfer the letter ‘G’ serially at 9600
baud, continuously. Assume XTAL = 8 MHz.

.INCLUDE "M32DEF.INC"
LDI R16,(1<<TXEN0) ;enable transmitter
OUT UCSR0B, R16
LDI R16,(1<<UCSZ1)|(1<<UCSZ0);8-bit data
OUT UCSR0C, R16 ;no parity, 1 stop bit
LDI R16,0x33;9600 baud rate
OUT UBRR0L,R16;for XTAL = 8 MHz
AGAIN:
SBIS UCSR0A,UDRE0 ;is UDR0 empty
RJMP AGAIN ;wait more
LDI R16,'G' ;send ‘G’
OUT UDR0,R16 ;to UDR0
RJMP AGAIN ;do it again

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 6


Example 5
Transmitting Multiple Chars.
Write a program to transmit .INCLUDE "M32DEF.INC"
;initialize stack pointer
the message “YES ” serially LDI R16,(1<<TXEN0) ;enable transmitter
OUT UCSR0B, R16
◦ at 9600 baud, LDI R16,(1<<UCSZ01)|(1<<UCSZ00); 8-bit data
◦ 8-bit data, and 1 stop bit. OUT UCSR0C, R16 ;no parity, 1 stop bit
LDI R16,0x33 ;9600 baud rate
OUT UBRR0L,R16
Do this forever AGAIN:
LDI R17,'Y' ;move 'Y' to R17
CALL TRNSMT ;transmit r17 to TxD
LDI R17,'E' ;move 'E' to R17
CALL TRNSMT ;transmit r17 to TxD
LDI R17,'S' ;move 'S' to R17
CALL TRNSMT ;transmit r17 to TxD
LDI R17,' ' ;move ' ' to R17
CALL TRNSMT ;transmit space to TxD
RJMP AGAIN ;do it again
TRNSMT:
SBIS UCSR0A,UDRE0 ;is UDR0 empty?
RJMP TRNSMT ;wait more
OUT UDR0,R17 ;send R17 to UDR
RET

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 7


Receiving data Serial
Programming the AVR to receive data serially
◦ In programming the AVR to receive character bytes serially, the following
steps must be taken:
1. The UCSR0B register is loaded with the value 10H, enabling the USART
receiver. The receiver will override normal port operation for the RxD pin
when enabled.
2. The UCSR0C register is loaded with the value 06H, indicating asynchronous
mode with 8-bit data frame, no parity, and one stop bit.
3. The UBRR0 is loaded with one of the values in Table 4 (if Fosc = 8 MHz) to
set the baud rate for serial data transfer.
4. The RXC0 flag bit of the UCSR0A register is monitored for a HIGH to see if
an entire character has been received yet.
5. When RXC0 is raised, the UDR0 register has the byte. Its contents are
moved into a safe place.
6. To receive the next character, go to Step 5.

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 8


Example 6 (Receive)
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.

.INCLUDE "M32DEF.INC"
LDI R16,(1<<RXEN0) ;enable receiver
OUT UCSR0B, R16
LDI R16,(1<<UCSZ01)|(1<<UCSZ00) ;8-bit data
OUT UCSR0C, R16 ;no parity, 1 stop bit
LDI R16,0x33 ;9600 baud rate
OUT UBRR0L,R16
LDI R16,0xFF ;Port B is output
OUT DDRB,R16
RCVE:
SBIS UCSR0A,RXC0 ;is any byte in UDR?
RJMP RCVE ;wait more
IN R17,UDR0 ;send UDR to R17
OUT PORTB,R17 ;send R17 to PORTB
RJMP RCVE ;do it again

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 9


Example 11-7 (Tx and Rx)
Already configured (PortA input and PortB output)
◦ 1 stop bit, no parity, 9600 baud, RXEN=1 and TXEN=1
◦ Continuously
◦ Receive data serially and display it on PortB
◦ Send data serially from PortA

RECEIVE:
SBIS UCSR0A,RXC0 ;is there new data?
RJMP TRANSMIT ;skip receive cmnds
IN R17,UDR0 ;move UDR to R17
OUT PORTB,R17 ;move R17 TO PORTB
TRANSMIT:
SBIS UCSR0A,UDRE0 ;is UDR empty?
RJMP SKIP_TX ;skip transmit cmnds
IN R17,PINA ;move Port A to R17
OUT UDR0,R17 ;send R17 to UDR
SKIP_TX:
RJMP RECEIVE ;do it again

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 10


Doubling the Baud rate
Baud rate is doubled when UCSR0A.U2X bit is
◦ 𝐵𝑎𝑢𝑑𝑅𝑎𝑡𝑒 = 𝐹𝑜𝑠𝑐 / 16 ∗ 𝑋 + 1 , when U2X = 0
◦ 𝐵𝑎𝑢𝑑𝑅𝑎𝑡𝑒 = 𝐹𝑜𝑠𝑐 / 𝟖 ∗ 𝑋 + 1 , when U2X = 1

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 11


Baud Rate Error
The Baudrate of the communicating devices must b exactly matched
◦ For example 𝐹𝑜𝑠𝑐 = 8 MHz, Baud rate 9600, U2X = 0
◦ Calculated value of UBRR comes out to be 51.08
◦ .08 is discarded, which is the error

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 12


Baud Rate Error – cont.
Using a unique frequency crystal oscillator, baud error can be removed
◦ Such as 11.0592 MHz or 7.3278 MHz

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 13


UART Interrupts
.CSEG ;put in code segment
Receive data using RJMP MAIN ;jump main after reset
interrupts .ORG URXCaddr ;int-vector of URXC int.
RJMP URXC_INT_HANDLER ;jump to URXC_INT_HANDLER
◦ 9600 baud
.ORG 40 ;start main after
◦ 8-bit data MAIN:
◦ 1 stop bit ;initialize stack pointer SPH and SPL
LDI R16,(1<<RXEN0)|(1<<RXCIE0) ;enable receiver
OUT UCSR0B, R16 ;and RXC interrupt
Write received data LDI R16,(1<<UCSZ01)|(1<<UCSZ00) ;sync,8-bit data
OUT UCSR0C, R16 ;no parity, 1 stop bit
on PortB LDI R16,0x33 ;9600 baud rate
OUT UBRR0L,R16
XTAL=8M LDI R16,0xFF ;set Port B as an
OUT DDRB,R16 ;input
SEI ;enable interrupts
WAIT_HERE:
RJMP WAIT_HERE ;stay here
URXC_INT_HANDLER:
IN R17,UDR0 ;send UDR to R17
OUT PORTB,R17 ;send R17 to PORTB
RETI

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 14


UART Interrupts
Transmit letter ‘G’ .CSEG ;put in code segment
using interrupts RJMP MAIN ;jump main after reset
.ORG UDREaddr ;int. vector of UDRE int.
◦ 9600 baud RJMP UDRE_INT_HANDLER ;jump to UDRE_INT_HANDLER

◦ 8-bit data .ORG 40 ;start main after


MAIN:
◦ 1 stop bit ;initialize stack pointer SPH and SPL
LDI R16,(1<<TXEN0)|(1<<UDRIE0) ;enable transmitter
XTAL=8M OUT UCSR0B, R16 ;and UDRE interrupt
LDI R16,(1<<UCSZ01)|(1<<UCSZ00) ; sync., 8-bit
OUT UCSR0C, R16 ;data no parity, 1 stop bit
LDI R16,0x33 ;9600 baud rate
OUT UBRR0L,R16
SEI ;enable interrupts
WAIT_HERE:
RJMP WAIT_HERE ;stay here

UDRE_INT_HANDLER:
LDI R26,'G' ;send 'G'
OUT UDR0,R26 ;to UDR0
RETI

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 15


UART Interrupts
C Language
Receive data using
interrupts
◦ 9600 baud
◦ 8-bit data
◦ 1 stop bit

Write received data


on PortB
XTAL=8M

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 16


UART Interrupts
C Language
Transmit letter ‘G’
using interrupts
◦ 9600 baud
◦ 8-bit data
◦ 1 stop bit

XTAL=8M

Saad Arslan COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 17

You might also like