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

Chap 6. Serial port programming

The document provides an overview of AVR serial port programming, focusing on the principles of serial communication, including the differences between parallel and serial data transfer, and the use of UART and USART for data transmission. It explains half and full duplex communication, asynchronous data framing with start and stop bits, and the RS232 standard for interfacing. Additionally, it details the MAX232 chip for voltage level conversion and outlines the programming of the AVR's USART registers for effective serial communication with PCs.

Uploaded by

namratadhiremath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chap 6. Serial port programming

The document provides an overview of AVR serial port programming, focusing on the principles of serial communication, including the differences between parallel and serial data transfer, and the use of UART and USART for data transmission. It explains half and full duplex communication, asynchronous data framing with start and stop bits, and the RS232 standard for interfacing. Additionally, it details the MAX232 chip for voltage level conversion and outlines the programming of the AVR's USART registers for effective serial communication with PCs.

Uploaded by

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

AVR Serial Port Programming

Basics of Serial Communication


Basics of Serial Communication
• Computers transfer data in two ways: parallel and serial.
• In parallel data transfers, often eight or more lines (wire conductors)
are used to transfer data to a device that is only a few feet away.
• Devices that use parallel transfers include printers and hard disks;
each uses cables with many wires.
• Although a lot of data can be transferred in a short amount of time
by using many wires in parallel, the distance cannot be great.
• To transfer to a device located many meters away, the serial
communication method is used.
• In serial communication, the data is sent one bit at a time, in contrast
to parallel communication, in which the data is sent a byte or more
at a time.
• The AVR has serial communication capability built into it, thereby
making possible fast data transfer using only a few wires.
Basics of Serial Communication
• When a microprocessor communicates with the outside
world, it provides the data in byte-sized chunks.
• For some devices, such as printers, the information is
simply grabbed from the 8-bit data bus and presented
to the 8-bit data bus of the device.
• This can work only if the cable is not too long, because
long cables diminish and even distort signals.
• Furthermore, an 8-bit data path is expensive.
• For these reasons, serial communication is used for
transferring data between two systems located at
distances of hundreds of feet to millions of miles apart.
Basics of Serial Communication
• Serial communication enables two computers located in
two different cities to communicate
over the telephone.
• For serial data communication to work, the byte of data
must be converted to serial bits using a parallel-in-serial-
out shift register.
• Then it can be transmitted over a single data line.
• This also means that at the receiving end there must be a
serial-in-parallel-out shift register to receive the serial data
and pack them into a
byte.
Basics of Serial Communication
• Also if data is to be transmitted on the
telephone line, it must be converted from 0s
and 1s to audio tones, which are sinusoidal
signals.
• This conversion is performed by a peripheral
device called a modem, which stands for
"modulator/demodulator”.
Basics of Serial Communication
• Serial data communication uses two methods, asynchronous and
synchronous.
• The synchronous method transfers a block of data (characters) at a
time, whereas the asynchronous method transfers a single byte at a
time.
• It is possible to write software to use either of these methods, but
the programs can be tedious and long.
• For this reason, special IC chips are made by many manufacturers for
serial data communications.
• These chips are commonly referred to as UART (universal
asynchronous receiver-transmitter) and USART (universal
synchronous- asynchronous receiver-transmitter).
• The AVR chip has a built-in USART
Half and full duplex
Half- and full-duplex transmission
• In data transmission, if the data can be both
transmitted and received, it is a duplex
transmission.
• This is in contrast to simplex transmissions such as
with printers, in which the computer only sends
data.
• Duplex transmissions can be half or full duplex,
depending on whether or not the data transfer
can be simultaneous.
• If data is transmitted one way at a time, it is
referred to as half duplex.
Half- and full-duplex transmission
• If the data 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), one for
transmission and one for reception, in order
to transfer and receive data simultaneously.
Asynchronous serial
communication and data framing
• The data coming in at the receiving end of
the data line in a serial data transfer is all 0s
and 1s
• It is difficult to make sense of the data unless
the sender and receiver agree on a set of
rules, a protocol, on how the data is packed,
how many bits constitute a character, and
when the data begins and ends.
Framing ASCII ‘A’(41H)
Start and stop bits
• Asynchronous serial data communication is widely
used for character-oriented transmissions, while
block-oriented data transfers use the synchronous
method.
• In the asynchronous method, each character is
placed between start and stop bits.
• This is called framing and is shown in the figure.
• In data framing for asynchronous communications,
the data, such as ASCII characters, are packed
between a start bit and a stop bit.
• The start bit is always one bit, but the stop bit can be
one or two bits.
Start and stop bits
• The start bit is always a 0 (low), and the stop bit(s) is 1 (high).
• For example, look at Figure in which the ASCII character "A"
(8-bit binary 0100 0001) is framed between the start bit and
a single stop bit.
• Notice that the LSB is sent out first.
• Notice in Figure that when there is no transfer, the signal is 1
(high), which is referred to as mark. The 0 (low) is referred to
as space. Notice that the
transmission begins with a start bit (space) followed by DO,
the LSB, then the rest of the bits until the MSB (D7), and
finally, the one stop bit indicating the end of
the character "A".
Start and stop bits
• In modern PCs, the use of one stop bit is
standard.
• Assuming that we are transferring a text file of
ASCII characters using 1 stop bit, we have a
total of 10 bits for each character: 8 bits for
the ASCII code, and 1-bit each for the start and
stop bits.
• Therefore, each 8-bit character has an extra 2
bits, which gives 25% overhead.
Start and stop bits
• In some systems, the parity bit of the character byte is
included in the data frame in order to maintain data integrity.
• This means that for each character (7- or 8-bit, depending on
the system) we have a single parity bit in addition to start and
stop bits.
• The parity bit is odd or even. In the case of an odd parity bit
the number of Is in the data bits, including the parity bit, is
odd.
• Similarly, in an even parity bit system the total number of bits,
including the parity bit, is even.
• For example, the ASCII character "A", binary 0100 0001, has 0
for the even parity bit.
• UART chips allow programming of the parity bit for odd-,
even-, and no-parity options.
Data transfer rate
• The rate of data transfer in serial data
communication is stated in bps (bits per second).
• Another widely used terminology for bps is baud
rate.
• The data transfer rate of a given computer
system depends on communication ports
incorporated into that system.
• In asynchronous serial data communication, the
baud rate is generally limited to 100,000 bps.
RS232 standards

• To allow compatibility among data


communication equipment made by
various manufacturers, an interfacing standard
called RS232 was set by the
Electronics Industries Association (EIA) in 1960.
• It was modified several times over the years
and today, RS232 is one of the most
widely used serial I/O interfacing standards.
• This standard is used in PCs and numerous
types of equipment.
RS232 standards
• Because the standard was set long before the advent
of the TTL logic family, however, its input and output
voltage levels are not TTL compatible.
• In RS232, a 1 is represented by —3 to —25 V, while a
0 bit is +3 to +25 volts, making -3 to +3 undefined.
• For this reason, to connect any RS232 to a
microcontroller system we must use voltage
converters such as MAX232 to convert the TTL logic
levels to the RS232 voltage levels, and vice versa.
• MAX232 IC chips are commonly referred to as line
drivers.
Data communication classification
•Current terminology classifies data communication
equipment as DTE (data terminal equipment) or DCE
(data communication equipment).
•DTE refers to terminals and computers that send and
receive data
• DCE refers to communication equipment, such as
modems, that are responsible for transferring the data.
• In our discussion all the RS232 pin function
definitions are from the DTE point of view.
• The simplest connection between a PC and a
microcontroller requires a minimum of three pins,
TX, RX, and ground, as shown in
Figure.
RS232 pins
RS232 hand- shaking signals

• To ensure fast and reliable data transmission


between two devices, the data transfer must
be coordinated because the receiving device
may have no room for the data in serial data
communication.
• There must be a way to inform the sender to
stop sending data.
• Many of the pins of the RS-232 connector are
used for handshaking signals.
PC (DTE) PC (DTE)

P/S S/P

RS232
RS232 MODEM MODEM
Telephone Line

Between PC and Microcontroller


Microcontroller
PC (DTE)
TX RX

MAX
232
RX TX

DB9
1. DTR (data terminal ready).
• When the terminal (or a PC COM port) is turned on, after going through
a self-test, it sends out signal DTR to indicate that it is ready for
communication.
• If there is something wrong with the COM port, this signal will not be
activated. This is an active-LOW signal and can be used to inform the
modem that the computer is working fine.
• This is an output pin from DTE (PC COM port) and an input to the
modem.
2. DSR (data set ready).
• When the DCE (modem) is turned on and has gone through the self-test, it
asserts DSR to indicate that it is ready to communicate. Thus, it is an
output from the modem (DCE) and an input to the PC (DTE).
• This is an active-LOW signal.
• If for any reason the modem cannot make a connection to the telephone,
this signal remains Inactive, indicating to the PC (or terminal) that it
cannot accept or send data.
3. RTS (request to send).
• When the DTE device (such as a PC) has a byte to transmit, it
asserts RTS to signal the modem that it has a byte of data to
transmit.
• RTS is an active-LOW output from the DTE and an input to the
modem.

4. CTS (clear to send).


• In response to RTS, when the modem has room to store the
data it is to receive, it sends out signal CTS to the DTE (PC) to
indicate that it can receive the data now.
• This input signal to the DTE is used by the DTE to start
transmission.
5. DCD (data carrier detect).
• The modem asserts signal DCD to inform the DTE (PC) that a
valid carrier has been detected and that contact between it
and the other modem is established.
• Therefore, DCD is an output from the modem and an input
to the PC (DTE).
6. RI (ring indicator).
• An output from the modem (DCE) and an input to a PC
(DTE) indicates that the telephone is ringing.
• RI goes on and off in synchronization with the ringing sound.
• Of the six handshake signals, this is the least often used
because modems take care of answering the phone.
• If in a given system the PC is in charge of answering the
phone, however, this signal can be used.
From the above description, PC and modem communication can
be summarized as follows:
• While signals DTR and DSR are used by the PC and modem,
respectively, to indicate that they are alive and well, it is RTS
and CTS that actually control the flow of data.
• When the PC wants to send data it asserts RTS, and in response,
the modem, if it is ready (has room) to accept the data, sends
back CTS.
• If, for lack of room, the modem does not activate CTS, the PC
will de-assert DTR and try again.
• RTS and CTS are also referred to as hardware control flow
signals.
• Ground is also referred to as SG (signal ground).
ATMEGA32 CONNECTION TO RS232

• The RS232 standard is not TTL compatible;


therefore, a line driver such as the MAX232 chip
is required to convert RS232 voltage levels to TTL
levels, and vice versa.

• The ATmega32 has two pins that are used


specifically for transferring and receiving data
serially.
ATmega32 Connection to RS232
• These two pins are called TX and RX and are part of
the Port D group (PD0 and PD1) of the 40-pin
package.
• Pin 15 of the ATmega32 is assigned to TX and pin 14
is designated as RX.
• These pins are TTL compatible; therefore, they
require a line driver to make them RS232
compatible.
• One such line driver is the MAX232 chip from
Maxim Corp.
MAX232 and its connection to the
ATMega32
MAX232
• One advantage of the MAX232 chip is that it
uses a +5 V power source, which is the same
as the source voltage for the AVR.
• In other words, with a single +5 V power
supply we can power both the AVR and
MAX232, with no need for the dual power
supplies.
• The MAX232 has two sets of line drivers for
transferring and receiving data.
MAX232
• The line drivers used for TX are called T1 and T2 while
the line drivers for RX are designated as RI and R2.
• In many applications only one of each is used. For
example, T1 and R1 are used together for TX and RX of
the AVR, and the second set is left unused.
• Notice in MAX232 that the T1 line driver has a
designation of T1in and T1out on pin numbers 11 and 14,
respectively.
• The T1in pin is the TTL side and is connected to TX of
the microcontroller, while T1out is the RS232 side that is
connected to the RX pin of the RS232 DB connector.
AVR Serial Port Programming in assembly
• The serial communication registers of the ATmega32 can be
programmed 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.
AVR Serial Port Programming in assembly

In the AVR microcontroller five registers are


associated with the USART: They are
UDR (USART Data Register)
UCSRA, UCSRB, UCSRC
(USART Control Status register)
UBRR (USART Baud Rate Register).
We examine each of them and show how
they are used in full- duplex serial data
communication.
UBRR register and baud rate in the AVR

• Because the x86 PCs are so widely used


to communicate with AVR-based
systems, we will emphasize serial
communications of the AVR with the
COM port of the x86 PC.
• Some of the baud rates supported by PC
HyperTerminal are listed in Table shown
here.
UBRR register and baud rate in the AVR
Baud rate in the AVR

• The AVR transfers and receives data serially


at many different baud rates.
• The baud rate in the AVR is programmable.
• This is done with the help of the 8-bit
register called UBRR(USART Baud Rate
Register).
• For a given crystal frequency, the value
loaded into the UBRR decides the baud rate.
Baud rate in the AVR

The relation between the value loaded into UBBR and


the Fosc (frequency of oscillator connected XTAL1 and
XTAL2 pins) is dictated by the following formula:

where X is the value we load into the UBRR register


Baud rate in the AVR
UBRR Register

•UBRR is a 16-bit register but only 12 bits of it are used to set


the USART baud rate.

•Bit 15 is URSEL (USART Register select) selects between


accessing the UBRRH or the UCSRC register.

•The other bits are reserved.


UDR registers and USART data I/O in the AVR
• To provide a full-duplex serial communication, there are two
shift registers referred to as Transmit Shift Register and
Receive Shift Register.
• Each shift register has a buffer that is connected to it directly.
• These buffers are called Transmit Data Buffer Register and
Receive Data Buffer Register.
• The USART Transmit Data Buffer Register and USART Receive
Data Buffer Register share the same I/O address, which is
called USART Data Register or UDR.

UDR Register
Simplified USART Transmit Block diagram
USART Transmit operation
• Two operations can happen w.r.t UDR. Write
and Read
• Data to be transmitted serially is written to
UDR, via data bus by the sending
Microcontroller and it will be transferred to
the Transmit Data Buffer Register (TXB).
• On the other hand and when you read data
from UDR, it will return the contents of the
Receive Data Buffer Register (RXB).
UCSR registers and USART
configurations in the AVR
• UCSRs are 8-bit control registers used for
controlling serial communication in the AVR.
• There are three USART Control Status
Registers in the AVR. They are
– UCSRA
– UCSRB
– UCSRC.
UCSRA: USART control and status register A

• RXC (Bit 7): USART Receive Complete


• TXC (Bit 6): USART Transmit Complete
• UDRE (Bit 5): USART Data Register Empty
• FE (Bit 4): Frame Error
• DOR (Bit 3): Data OverRun
• PE (Bit 2): Parity Error
• U2X (Bit 1): Double the USART Transmission Speed
• MPCM (Bit 0): Multi-processor Communication Mode
UCSRB: USART control and status register B

• RXCIE (Bit 7): Receive Complete Interrupt Enable


• TXCIE (Bit 6): Transmit Complete Interrupt Enable
• UDRIE (Bit 5): USART Data Register Empty Interrupt
Enable
• RXEN (Bit 4): Receive Enable
• TXEN (Bit 3): Transmit Enable
• UCSZ2 (Bit 2): Character Size
• RXB8 (Bit 1): Receive data bit 8
• TXB8 (Bit 0): Transmit data bit 8
UCSRB: USART control and status register B
• Also you have to set an identical character size
for both transmitter and the receiver.
• If the character size of the receiver does not
match the character size of the transmitter,
data transfer would fail.
• Parity mode and number of stop bits are other
factors that the receiver and transmitter must
agree on before starting USART
communication.
UCSRC: USART control and status register C

• URSEL (Bit 7): USART Register Select


• UMSEL (Bit 6): USART Mode Select
• UPM1:0 (Bit 5:4): Parity Mode
• USBS (Bit 3): Stop Bit Select
• UCSZ1:0 (Bit 2:1): Character Size
• Bit 0: UCPOL – Clock Polarity
Data Bit Settings
UCSRC: USART control and status register

• Because of some technical considerations, the


UCSRC register shares the same I/O location as the
UBRRH, and therefore some care must be taken
when accessing these I/O locations.
• When you write to UCSRC or UBRRH, the high bit of
the written value (URSEL) controls which of the two
registers will be the target of the write operation.
• If URSEL is zero during a write operation, the UBRRH
value will be updated; otherwise, UCSRC will be
updated.
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 86H, indicating asynchronous mode with 8-
bit data frame, no parity, and one stop bit.
3. The UBRR is loaded with appropriate value selected from the table discussed earlier 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.
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 10H, 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 86H, indicating
asynchronous mode with 8-bit data frame, no parity, and one
stop bit.
3. The UBRR is loaded with appropriate value selected from
the table discussed earlier 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.
a) 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
b) Write a program for the AVR to set the values of UCSRB and
UCSRC for this configuration.
In the UCSRB Register make .INCLUDE "M32DEF.INC"
RXEN = 1 & TXEN = 1 to enable LDI R16, 0b00011000
receive and transmit
OUT UCSRB, R16
UCSZ2: 0 should be 011 for 8-
LDI R16, 0x86
bit data
OUT UCSRC, R16
UMSEL = 0 for Asynchronous
mode
UPM1:0 = 00 for no parity
USBS = 0 for 1 stop bit
In the above example set the baud rate to 1200 and write a program for the
AVR to set up the values of UCSRB, UCSRC and UBRR. (Fosc= 8MHz)

INCLUDE "M32DEF.INC"
LDI R16, 0b0011000
OUT UCSRB, R16
LDI R16, 0x86
OUT UCSRC, R16
LDI R16, 0x9F ; lower byte
OUT UBRRL, R16
LDI R16, 0x01 ; higher byte
OUT UBRRH, R16 ; baud rate is 1200
Write a program for the AVR to transfer the letter ‘G’ serially at 9600 baud,
continuously. Assume XTAL 8MHz.

.INCLUDE "M32DEF.INC"
LDI R16, (1<< TXEN) ;enable transmitter
OUT UCSRB, R16
LDI R16, (1<< UCSZ1)| (1<< UCSZ0)| (1<< URSEL)
;8-bit data, no parity, 1stop bit
OUT UCSRC, R16
LDI R16, 0x33 ; 9600 baud rate
OUT UBRRL, R16
AGAIN:
SBIS UCSRA, UDRE ; is UDR empty
RJMP AGAIN ; wait more
LDI R16, 'G' ; send G
OUT UDR,R16 ; to UDR
RJMP AGAIN
Write a program to transmit the message “YES” serially at 9600
Baud, 8-bit data and 1-stop bit. Do this forever.
.INCLUDE "M32DEF.INC"
AGAIN: LDI R17,'Y' ; move ‘Y’ to
LDI R20, HIGH (RAMEND) ;
R17
initialise high
CALL TRNSMT ; transmit R17
;byte of SP
to TxD
OUT SPH, R20
LDI R17,'E' ; move ‘E’ to
LDI R20, LOW (RAMEND) ;
R17
initialise low byte of SP
CALL TRNSMT ; transmit R17
OUT SPL, R20
to TxD
LDI R16, (1<< TXEN) ;
LDI R17,'S' ; move ‘Y’ to
enable
R17
;transmitter
CALL TRNSMT ; transmit R17
OUT UCSRB, R16
to TxD
LDI R16, (1<< UCSZ1)| (1<< UCSZ0)|
LDI R17,' ' ; move ' ' to
(1<< URSEL) ;8-bit data, no
R17
parity, 1
CALL TRNSMT ; transmit R17
;stop bit
to TxD
OUT UCSRC, R16
RJMP AGAIN ; do it again
LDI R16, 0x33 ; 9600 baud
rate TRNSMT: SBIS UCSRA, UDRE ; is UDR
empty
OUT UBRRL, R16
RJMP TRNSMT ; wait more
OUT UDR,R17 ; send R17 to
UDR
RET

You might also like