0% found this document useful (0 votes)
12 views53 pages

Microcontroller Serial Communication Guide

The document provides an overview of interfacing microcontrollers, specifically the ATMega32, with external peripherals using serial communication. It covers the basics of serial communication, including asynchronous and synchronous methods, data framing, and the RS232 standard, along with practical programming examples for data transmission and reception using AVR C. Key components such as the USART, UBRR, and UDR registers are explained for configuring and managing serial communication effectively.

Uploaded by

cyt78566
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)
12 views53 pages

Microcontroller Serial Communication Guide

The document provides an overview of interfacing microcontrollers, specifically the ATMega32, with external peripherals using serial communication. It covers the basics of serial communication, including asynchronous and synchronous methods, data framing, and the RS232 standard, along with practical programming examples for data transmission and reception using AVR C. Key components such as the USART, UBRR, and UDR registers are explained for configuring and managing serial communication effectively.

Uploaded by

cyt78566
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

Interfacing

Microcontroller with
External Peripherals
Module 3
ATMega32 – Pinout diagram
Introduction
• When a microcontroller communicates with the outside world, it
provides the data in byte-sized chunks.
• 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 feet to millions of miles
apart.
Basic of Serial Communication
• In serial data communication, 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 line.
• At the receiving end there must be serial-in-parallel-out shift register
to receive the serial data and pack them into a byte.
• When the distance is short, the digital signal can be transmitted as it
is on a simple wire and requires no modulation.
• For long distance data transfers using communication lines such as a
telephone requires a modem to modulates (convert from 0s and 1s to
audio tones) and demodulate (convert from audio tones to 0s and 1s).
• Serial communication uses two methods – asynchronous and
synchronous.
• The synchronous method refers to a block of data 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 such as UART (Universal Asynchronous
Receiver Transmitter) and USART (Universal Synchronous Asynchronous
Receiver Transmitter) are made for serial communication.
• The AVR chip has a built-in USART.
Asynchronous serial communication
and data framing
• In a serial data transfer, the data coming in at the receiving end of the
data line 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 (protocol) on how the data is packed, how
many bits constitute a character and when the data begins and ends.
Start and Stop bits
• In the asynchronous method, each character is placed between start
and stop bits. This is called framing.
• The start bit is always one bit, but the stop bit can be one or two bits.
• The start bit is always a 0 (low) and the stop bit(s) is 1 (high).
• When there is no transfer, the signal is 1 (high), which is referred to as
mark.
• The 0 (low) is referred to as space.
• In asynchronous serial communications, peripheral chips and modems
can be programmed for data that is 7 or 8 bits wide.
• This is in addition to the number of stop bits, 1 or 2.
• In some systems, the parity bit of the character byte is included in the
data frame in order to maintain data integrity.
• 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 communication is stated in bps (bits
per second).
• Another widely used terminology is the baud rate.
• It is defined as the number of signal changes per second.
• The data transfer rate of a given system depends on communication
ports incorporated into that system.
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.
• In 1963 it was modified and called RS232A.
• RS232B and RS232C were issued in 1965 and 1969 respectively.
• Because the standard was set long before the advent of the TTL logic family, its
input and output voltage levels are not TTL compatible.
• In RS232, a 1 is represented by -3 to -25V, while a 0 is +3 to +25V, 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.
RS232 Pins
• The original RS232 uses DB-25 connector as shown in the following
figure:
RS232 Pin Description
RS232 Pins - DB-9
• Because not all the pins were used in PC cables, IBM introduced the
DB-9 version of the serial I/O standard, which uses only 9 pins, as
shown in the following:
DB-9 Pin Description
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,
while DCE refers to communication equipment, such as modems, that
are responsible for transferring the data.
• The simplest connection between a PC and a microcontroller requires
a minimum of three pins, TX, RX and ground.
ATMega32 Connection to RS232
• The ATmega32 has two pins that are used specifically for transferring
and receiving data serially.
• These two pins are called TX and RX and are part of the PORTD (PD0
and PD1).
• These pins are TTL compatible; therefore, they require a line driver
such as MAX232 chip to make them RS232 compatible.
• The MAX232 has two sets of line drivers for transferring and receiving
data as shown in the following figure:
• The line drivers used for TX are called T1 and T2, while the line drivers
for RX are designated as R1 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.
• In MAX232, 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.
• The R1 line driver has a designation of R1IN and R1OUT on pin numbers
13 and 12 respectively.
• The R1IN pin is the RS232 side that is connected to TX of the RS232 DB
connector, and R1OUT is the TTL side that is connected to the RX pin of
the microcontroller.
AVR Serial Port Programming in C
• The USART 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 used to connect the AVR based system to
the x86 PC serial port for the purpose of full duplex serial data
transfer.
• In the AVR microcontroller five registers are associated with the
USART.
• They are:
• UDR (USART Data Register)
• UCSRA (USART Control Status Register A)
• UCSRB (USART Control Status Register B)
• UCSRC (USART Control Status Register)
• UBRR (USART Baud Rate Register)
UBRR Register and Baud Rate in
AVR
• The AVR transfers and receives data serially at many different rates
• The baud rate in the AVR is programmable.
• Done with the help of UBRR.
• For a given crystal frequency, the value loaded into the UBRR decides
the baud rate.
• Desired Baud Rate = FOSC/ (16 (X + 1))

• FOSC is the crystal frequency


• X is the value we load into the UBRR register.

• To get the value of X:

X = (FOSC/ (16(Desired Baud Rate))) – 1


UDR Register and USART data I/O in
AVR
• In the AVR, to provide a full-duplex serial communication, there are
two shift registers
• Transmit Shift Register
• Receive Shift Register.
• Each shift register has a buffer that is connected to it directly.
• Transmit Data Buffer Register
• Receive Data Buffer Register.
• These registers share the same I/O address, which is called USART
Data Register or UDR.
• When we write data to UDR, it will be transferred to the Transmit
Data Buffer Register (TXB) and when we read data from UDR, it will
return the contents of the Receive Data Buffer Register (RXB).
UCSR Registers and USART
Configuration in 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.
• UCSRA
• UCSRB
• UCSRC
UCSRA
UCSRB
UCSRC
Values of UCSZ2:0 for different
character size
Programming AVR to Transfer Data
Serially
1. The UCSRB register is loaded with the value 08H, enabling the USART
transmitter. It will override normal port operation for the TxD pin when
enabled.
2. The UCSRC register is loaded with desired value, indicating synchronous or
asynchronous mode, number of characters in the data frame, parity and
number of stop bit.
3. The UBRR is loaded with appropriate value to set the baud rate for serial
communication.
4. The character byte to be transmitted serially is written into the UDR register.
5. Monitor UDRE bit of the UCSRA register to make sure UDR is ready for next
byte.
6. To transmit next character, go to Step 4.
Transmit a Single Character
• Write a C program to transfer a character ‘*’ serially at 9600 baud,
continuously. Use 8 bit data and 1 stop bit. Assume XTAL = 16 MHz
Transmit Character - Program
#include <avr/io.h>

void usart_init (void)


{
UCSRB = (1 << TXEN);
UCSRC = (1 << UCSZ1) | (1 << UCSZ0) | (1 << URSEL);
UBRRL = 103; // X = (FOSC/ (16(Desired Baud Rate))) – 1
}
void usart_send (unsigned char ch)
{
while ( ! (UCSRA & (1 << UDRE))); //wait until UDR is empty
UDR = ch;
}
void main( )
{
usart_init();

while (1)
usart_send(‘*’);
}
Transmit a String
• Write a program to send “IPT & GPTC, Shoranur” to the serial port
continously
Programming AVR to Receive Data
Serially
1. The UCSRB register is loaded with the value 01H, enabling the USART receiver. It
will override normal port operation for the RxD pin when enabled.
2. The UCSRC register is loaded with desired value, indicating synchronous or
asynchronous mode, number of characters in the data frame, parity and number
of stop bit.
3. The UBRR is loaded with appropriate value to set the baud rate for serial
communication.
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 space.
6. To receive the next character, go to Step 5.
Receive a Character
Write a program to receive a character from serial port and put them
on port A. Given that baud rate = 9600, stop bits = 1, XTAL = 16 MHz
Receive Character - Program
#include <avr/io.h>

void main()
{
DDRA = 0xff;
UCSRB = (1 << RXEN);
UCSRC = (1 << UCSZ1) | (1 << UCSZ0) | (1 << URSEL);
UBRRL = 103; // X = (FOSC/ (16(Desired Baud Rate))) – 1
while (1)
{
while (! (UCSRA & (1 << RXC))); // wait until new data
PORTA = UDR;
}
}
Receive and Transmit
• Write an AVR C program to receive a character from the serial port, If
the character is lower case, change it to upper case and send it back.
Given that baud rate = 9600, stop bits = 1, XTAL = 16 MHz.
#include <avr/io.h>

void main()
{
UCSRB = (1 << RXEN) | (1 << TXEN);
UCSRC = (1 << UCSZ1) | (1 << UCSZ0) | (1 << URSEL);
UBRRL = 103;

unsigned char ch;


while (1)
{
while (! (UCSRA & (1 << RXC)));
ch = UDR;

if (ch >= ‘a’ && ch <= ‘z’)


{
ch = ch – ‘a’ + ‘A’;

while ( ! (UCSRA & (1 << UDRE)));


UDR = ch;
}
}
}
Transmit Character using Interrupt
• Write a C program to transfer a character ‘*’ serially at 9600 baud,
continuously. Use 8 bit data and 1 stop bit. Assume XTAL = 16 MHz.
Use Interrupt
Transmit Character Using Interrupt -
Program
#include <avr/io.h>
#include <avr/interrupt.h>

unsigned char ch;

ISR (USART_UDRE_vect)
{
UDR = ch;
}
void main( )
{
UCSRB = (1 << TXEN);
UCSRC = (1 << UCSZ1) | (1 << UCSZ0) | (1 << URSEL);
UBRRL = 103;
sei();

while (1);
}
Receive Character using Interrupt
• Write an AVR C program to receive a character from the serial port
and put them on Port A using interrupt. Given that baud rate = 9600,
stop bits = 1, XTAL = 16 MHz.
Receive Character using Interrupt -
Program
#include <avr/io.h>
#include <avr/interrupt.h>

ISR (USART_RXC_vect)
{
PORTA = UDR;
}
void main()
{
DDRA = 0xff;
UCSRB = (1 << RXEN);
UCSRC = (1 << UCSZ1) | (1 << UCSZ0) | (1 << URSEL);
UBRRL = 103;
sei();

while (1);
}

You might also like