MPS - Ch11 - AVR - Serial Port Programming in Assembly and C
MPS - Ch11 - AVR - Serial Port Programming in Assembly and C
Refs:
1. The AVR microcontroller and embedded systems: using
Assembly and C, Muhammad Ali Mazidi, 2011 Pearson
Education
2. Slides of Prof. Lam Phung, University of Wollongong
1
OBJECTIVES
Upon completion of this chapter, you will be able to:
• List the advantages of serial communication over parallel
• Explain serial communication protocol
• Contrast synchronous versus asynchronous communication
• Contrast half- versus full-duplex transmission
• Explain the process of data framing
• Describe data transfer rate and bps rate
• Define the RS232 standard
• Explain the use of the MAX232 and MAX233 chips
• Interface the AVR with an RS232 connector
• Discuss the baud rate of the AVR
• Describe serial communication features of the AVR
• Describe the main registers used by serial communication of the AVR
• Program the ATmega32 serial port in Assembly and C
2
Outline
1. Basics of Serial Communication
2. ATmega32 connection to RS232
3. AVR Serial Port Programming in Assembly
4. AVR Serial Port Programming in C
5. AVR Serial Port Programming in Assembly and C
Using Interrupts
3
Parallel vs Serial
• Parallel: expensive - short distance – fast – no modulation
• Serial :cheaper– long (two different cities by modem)-slow
5
Basics of Serial Communication
• 2 methods, asynchronous and synchronous
• Synchronous method transfers a block of data
(characters) at a time
• Asynchronous method transfers a single byte at a time
• Uses special IC chips called UART (universal
asynchronous receiver-transmitter) and USART
(universal synchronousasynchronous receiver-
transmitter)
• AVR chip has a built-in UART
6
Basics of Serial Communication
7
Basics of Serial Communication
• Half- and full-duplex transmission
– simplex transmissions the computer only sends data
– If one way at a time, it is half duplex
– if the data can be transmitted and received, it is a duplex
transmission
• duplex transmissions can be half or full duplex
• depends on whether or not the data transfer can be
simultaneous
• If can go both ways at the same time, it is full duplex
• full duplex requires two wire conductors for the data lines (in
addition to the signal ground)
8
Basics of Serial Communication
9
Framing
• Start and stop bits
– Asynchronous method, each character is placed
between start and stop bits
– Called framing
– Start bit is always one bit
– Stop bit can be one or two bits
– Start bit is always a 0 (low)
– Stop bit(s) is 1 (high)
– LSB is sent out first
12
RS232 – DB-25 connector
Table 10–1 RS232 Pins (DB-25)
13
RS232 – DB-9 connector
14
• Data communication classification
– DTE (data terminal equipment)
– DCE (data communication equipment)
– DTE - terminals and computers that send and receive data
– DCE - communication equipment responsible for transferring the data
– simplest connection between a PC and microcontroller requires a
minimum of three pins, TxD, RxD, and ground
15
Figure 11–6 Null Modem Connection
• Examining RS232 handshaking signals
– many of the pins of the RS-232 connector are used for
handshaking signals
– they are not supported by the AVR UART chip
• PC/compatible COM ports
– PC/compatible computers (Pentium) microprocessors
normally have two COM ports
– both ports have RS232-type connectors
– COM ports are designated as COM 1 and COM 2 (replaced
by USB ports)
– can connect the AVR serial port to the COM 1 port. In the
absence of a COM port, we can use a COM-to-USB
converter module.
16
Outline
1. Basics of Serial Communication
2. ATmega32 connection to RS232
3. AVR Serial Port Programming in Assembly
4. AVR Serial Port Programming in C
5. AVR Serial Port Programming in Assembly and C
Using Interrupts
17
RX and TX pins in the ATmega32
18
MAX232
• MAX232
– converts from RS232 voltage levels to TTL voltage
levels
– uses a +5 V power source
– MAX232 has two sets of line drivers for transferring and
receiving data
– line drivers used for TxD are called T1 and T2
– line drivers for RxD are designated as R1 and R2
– T1 and R1 are used together for TxD and RxD of the
AVR
– second set is left unused
19
MAX232
ATmega32
15
TXD (PD1)
14
RXD (PD0)
20
MAX233
– MAX233 performs the same job as the MAX232
– eliminates the need for capacitors
– much more expensive than the MAX232
ATmega32
15
TXD (PD1)
14
RXD (PD0)
22
• In this section we discuss the serial communication registers
of the ATmega32 and show how to program them to transfer
and receive data using asynchronous mode.
• The USART (universal synchronous asynchronous
receiver/transmitter) in the AVR has normal asynchronous,
double-speed asynchronous, master synchronous, and slave
synchronous mode features.
• The synchronous mode can be used to transfer data between
the AVR and external peripherals such as ADC and EEPROMs.
• The asynchronous mode is the one we will use to connect the
AVR-based system to the x86 PC serial port for the purpose of
full-duplex serial data transfer.
• In this section we examine the asynchronous mode only.
23
USART Block Diagram
Store the
sent byte Serial Port Interface
Store the
received byte
24
Serial USART ─ Hardware elements
• USART Clock Generator:
– to provide clock source.
– to set baud rate using UBRR register.
• USART Transmitter:
– to send a character through TxD pin.
– to parity bit, shift register.
• handle start/stop bit framing, USART Receiver:
– to receive a character through RxD pin.
– to perform the reverse operation of the transmitter.
• USART Registers:
– to configure, control, and monitor the serial USART.
25
Serial USART ─ Three groups of registers
• USART Baud Rate Registers
– UBRRH and UBRRL
• USART Control and Status Registers
– UCSRA
– UCSRB
– UCSRC
• USART Data Registers
– UDR
• Understanding these registers is essential in
using the serial port. Therefore, we’ll study
these registers in depth.
26
USART Baud Rate Registers
27
Figure 11-9. Baud Rate Generation Block Diagram
28
29
USART Control and Status Register A (UCSRA)
30
USART Control and Status Register B (UCSRB)
31
USART Control and Status Register C (UCSRC)
32
Setting character size
33
USART Data Register
34
UDR
Same address but different hardware
35
36
37
FE and PE flag bits
• When the AVR USART receives a byte, we can check the
parity bit and stop bit.
• If the parity bit is not correct, the AVR will set PE to one,
indicating that an parity error has occurred.
• We can also check the stop bit. As we mentioned before,
the stop bit must be one, otherwise the AVR would generate
a stop bit error and set the FE flag bit to one, indicating that
a stop bit error has occurred.
• We can check these flags to see if the received data is valid
and correct. Notice that FE and PE are valid until the receive
buffer (UDR) is read. So we have to read FE and PE bits
before reading UDR. You can explore this on your own.
38
Programming the AVR to transfer data serially
• In programming the AVR to transfer character bytes serially, the
following steps must be taken:
1. The UCSRB 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 UCSRC register is loaded with the value 06H, indicating
asynchronous mode with 8-bit data frame, no parity, and one
stop bit.
3. The UBRR is loaded with one of the values in Table 11-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
UDR register.
5. Monitor the UDRE bit of the UCSRA register to make sure UDR
is ready for the next byte.
6. To transmit the next character, go to Step 4.
39
Importance of monitoring the UDRE flag
• By monitoring the UDRE flag, we make sure that we are not
overloading
40
41
Example 11-5 (2/2)
42
Programming the AVR to receive data serially
• In programming the AVR to receive character bytes serially, the
following steps must be taken:
1. The UCSRB register is loaded with the value lOH, enabling the
USART receiver. The receiver will override normal port operation
for the RxD pin when enabled.
2. The UCSRC register is loaded with the value 06H, indicating
asynchronous mode with 8-bit data frame, no parity, and one
stop bit.
3. The UBRR is loaded with one of the values in Table 11-4 (if
Fosc = 8 MHz) to set the baud rate for serial data transfer.
4. The RXC flag bit of the UCSRA register is monitored for a HIGH
to see if an entire character has been received yet.
5. When RXC is raised, the UDR register has the byte. Its
contents are moved into a safe place.
6. To receive the next character, go to Step 5.
43
44
Transmit and receive
• In previous examples we showed how to transmit or receive
data serially.
• Next we show how do both send and receive at the same
time in a program.
• Assume that the AVR serial port is connected to the COM
port of the x86 PC, and we are using the HyperTerminal
program on the PC to send and receive data serially. Ports A
and B of the AVR are connected to LEDs and switches,
respectively.
• Example 11-7 shows an AVR program with the following
parts: (a) sends themessage "YES" once to the PC screen,
(b) gets data on switches and transmits it via the serial port
to the PC's screen, and (c) receives any key press sent by
HyperTerminal and puts it on LEDs.
45
46
Example 11-7 (2/2)
47
Doubling the baud rate in the AVR
• There are two ways to increase the baud rate of data
transfer in the AVR:
1. Use a higher-frequency crystal.
2. Change a bit in the UCSRA register, as shown below.
• Option 1 is not feasible in many situations because the
system crystal is fixed. Therefore, we will explore option 2.
• There is a software way to double the baud rate of the AVR
while the crystal frequency stays the same. This is done
with the U2X bit of the UCSRA register.
• When the AVR is powered up, the U2X bit of the UCSRA
register is zero. We can set it to high by software and
thereby double the baud rate.
48
49
Baud rate error calculation
• In calculating the baud rate we have used the integer number for the
UBRR register values because AVR microcontrollers can only use
integer values. By dropping the decimal portion of the calculated
values we run the risk of introducing error into the baud rate. There
are several ways to calculate this error.:
1. Error= (Calculated value for the UBRR- Integer part) /Integer part
Ex: With XTAL = 8 MHz and U2X = 0 we have the following for
the 9600 baud rate:
UBRR value= (500,000/ 9600)- 1 = 52.08- 1 = 51.08 =51
=> Error= (51.08- 51)/ 51 = 0.16%
2. Error= (Calculated baud rate- desired baud rate) /desired baud
rate
Ex: For XTAL = 8 MHz and 9600 baud rate, we got X = 51.
Calculated baud rate= Fosc / 16(X + 1)) (for U2X = 0)
Error= (9615- 9600) / 9600 = 0.16% 50
51
52
Outline
1. Basics of Serial Communication
2. ATmega32 connection to RS232
3. AVR Serial Port Programming in Assembly
4. AVR Serial Port Programming in C
5. AVR Serial Port Programming in Assembly and C
Using Interrupts
53
• As we have seen in previous chapters, all the
special function registers of the AVR are
accessible directly in C compilers by using the
appropriate header file.
• Examples 11-9 through 11-14 show how to
program the serial port in C. Connect your AVR
trainer to the PC's COM port and use
HyperTerminal to test the operation of these
examples.
54
55
56
57
58
59
60
Serial USART ─ Main tasks
• There are 4 main tasks in using the serial port.
1. Initialising the serial port.
2. Sending a character.
3. Receiving a character.
4. Sending/receiving formatted strings.
61
Initialising serrial port
62
Initialising serial port ─ Example
63
Sending a character
64
Sending a character ─ Example
65
Receiving a character
66
Receiving a character ─ Example
67
Sending/receiving formatted strings
68
Sending/receiving formatted strings (2/3)
69
Sending/receiving formatted strings (3/3)
70
Outline
1. Basics of Serial Communication
2. ATmega32 connection to RS232
3. AVR Serial Port Programming in Assembly
4. AVR Serial Port Programming in C
5. AVR Serial Port Programming in Assembly and C
Using Interrupts
71
USART Interrupts
• Interrupt-based data receive
– We need to set HIGH the Receive Complete Interrupt Enable
(RXCIE) bit in UCSRB.
– Upon completion of the receive, the RXC (USART receive
complete flag) becomes HIGH.
– If RXCIE = 1, changing RXC to one will force the CPU to jump
to the interrupt vector.
Highest
priority
Serial
port
Lowest
priority
73
Interrupt-based data receive – Ex (1/2)
Contents of “m32def.inc” :
...
.equ URXCaddr = 0x001a ; USART, Rx Complete
.equ UDREaddr = 0x001c ; USART Data Register Empty
.equ UTXCaddr = 0x001e ; USART, Tx Complete
...
74
Interrupt-based data receive – Ex (2/2)
75
Interrupt-based data transmit – Ex (1/2)
Contents of “m32def.inc” :
...
.equ URXCaddr = 0x001a ; USART, Rx Complete
.equ UDREaddr = 0x001c ; USART Data Register Empty
.equ UTXCaddr = 0x001e ; USART, Tx Complete
...
76
Interrupt-based data transmit – Ex (2/2)
77
USART_RXC_vect
Serial
USART_UDRE_vect
port
USART_TXC_vect
78
79
80
Summary
• This chapter began with an introduction to the fundamentals of serial
communication.
• Serial communication, in which data is sent one bit a time, is used in
situations where data is sent over significant distances. (In parallel
communication, where data is sent a byte or more at a time, great
distances can cause distortion of the data.)
• Serial communication has the additional advantage of allowing
transmission over phone lines. Serial communication uses two methods:
synchronous and asynchronous.
• RS232 is a standard for serial communication connectors.
• The AVR's UART was discussed. We showed how to interface the
ATmega32 with an RS232 connector and change the baud rate of the
ATmega32.
• In addition, we described the serial communication features of the AVR,
and programmed the ATmega32 for serial data communication. We also
showed how to program the serial port of the ATmega32 chip in
Assembly and C.
81