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

Lab 11

The document outlines Lab 11 for AVR Serial Communication, focusing on setting up UART communication with the ATmega328p microcontroller. It includes learning outcomes, hardware resources, and detailed instructions for transmitting and receiving data over UART, along with lab tasks involving code implementation. The lab aims to establish a serial link between a PC and the microcontroller while performing specific tasks with LEDs and a 7-segment display.

Uploaded by

mokishere
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 11

The document outlines Lab 11 for AVR Serial Communication, focusing on setting up UART communication with the ATmega328p microcontroller. It includes learning outcomes, hardware resources, and detailed instructions for transmitting and receiving data over UART, along with lab tasks involving code implementation. The lab aims to establish a serial link between a PC and the microcontroller while performing specific tasks with LEDs and a 7-segment display.

Uploaded by

mokishere
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Embedded System

Lab 11
AVR Serial Communication

Submitted By:
Asna Maqsood
426990
BESE13 B

1
Embedded System
Lab 11
AVR Serial Communication

Submitted By:
Muhammad Ahsan
406267
BESE13 B

2
Embedded System
Lab 11
AVR Serial Communication

Submitted By:
Muhammad Owais Khan
404262
BESE13 B

3
Embedded System
Lab 11
AVR Serial Communication

Submitted By:
Umar Farooq
406481
BESE13 B

4
Lab 11: AVR Serial Communication
EE222: Microprocessor Systems

Contents
1 Acknowledgements..............................................................................................................2
2 Administrivia........................................................................................................................2
2.1 Learning Outcomes................................................................................................................2
2.2 Deliverables............................................................................................................................2
3 Hardware Resources...........................................................................................................2
4 Serial Communication.........................................................................................................3
4.1 Universal Asynchronous Receiver Transmitter (UART).....................................................3
4.1.1 Baud Rate................................................................................................................................3
4.2 PC Communication.................................................................................................................3
4.2.1 Connecting PC with Microcontroller.....................................................................................3
4.2.2 Software..................................................................................................................................5
4.3 Transmitting data over UART...............................................................................................5
4.4 Receiving data over UART.....................................................................................................7
5 Lab Tasks...............................................................................................................................8
5.1 Task A......................................................................................................................................8
5.2 Task B......................................................................................................................................8

5
1 Acknowledgements
This lab exercise is prepared by Mohammad Azfar Tariq and Muhammad Usman under the
supervision of Dr. Rehan Ahmed for the course EE-222 Microprocessor Systems, focusing
on the ATmega16 microcontroller. Later on, the lab was revised for the ATmega328p
Arduino Uno-based microcontroller by Lab Engr. Shaiza. Reporting any errors or
discrepancies found in the text is appreciated.

2 Administrivia
2.1 Learning Outcomes
By the end of this lab you will be able to:

1. Setup ATmega328p microcontroller for UART communication.

2. Send and receive serial data from ATmega328p.

3. Setup serial link between your PC and ATmega328p microcontroller through UART
interface.

2.2 Deliverables
You are required to submit:

• Code

• Observations and experiences in the beginning of

next lab.

3 Hardware Resources
• Arduino Uno board with ATmega328p microcontroller

• Arduino USB cable

• Seven Segment Display

• Resistance 47Ω

• LEDs

• USB-TTL Converter.

6
4 Serial Communication
Computers either communicate in a serial manner or parallel manner. Each mode has its
own merits and demerits. In serial communication, data is sent bit by bit through a single
wire.

Serial communication can be “Synchronous” and “Asynchronous”. In synchronous


communication, the sender sends a clock along-with the data (through another wire) to tell
the receiver that valid data bit will be available on each positive/negative edge of the clock.
In asynchronous communication, the sender and receiver agrees upon a time unit after
which the bit transferred or received is considered valid. In this lab we will learn about
asynchronous serial communication.

4.1 Universal Asynchronous Receiver Transmitter (UART)


UART is a block of circuitry in microcontrollers responsible for implementing
asynchronous serial communication. In UART there are two pins “Rx” and “Tx”. Rx is
responsible for data reception while Tx is responsible for data transmission. This mode of
serial communication is also called “TTL serial” because in this mode data is transferred
on 0-5V or 0-3.3V voltage levels (Transistor-Transistor Logic).

4.1.1 Baud Rate


In digital communication, number of bits per second is termed as baud rate. In UART
communication, both, the sender and the receiver agrees upon a common baud rate at
which data will be transferred between them.

4.2 PC Communication
In modern computers, serial communication is standardized by industry through USB
(Universal Serial Bus) standard for computers to communicate with their peripheral
devices.

4.2.1 Connecting PC with Microcontroller


The Tx and Rx pins on ATmega328p side uses TTL volatge levels while the interface on PC
side is USB. Therefore, we need an intermediate device that converts TTL standards to USB.

7
In our lab, PL2303 USB to TTL Serial Converter Cable is available. Inside the big USB
plug is a USB to Serial conversion chip and at the end of the cable are four-wire – red
power, black ground, white RX into the USB port, and green TX out of the USB port.

The power pin provides the 5V @ 500mA direct from the USB port and the RX/TX pins
are 3.3V levels for interfacing with the most common 3.3V logic level chipsets.
The following pin configuration should be followed while using this equipment.

8
4.2.2 Software

TeraTerm is an open-source terminal emulator for serial communication. You can download
the software from here1. As you launch teraterm, a black screen will appear on your
display. Configure teraterm using this tutorial2. Whatever your microcontroller sends
through UART will appear on this screen and whatever you’ll type on this screen will be
sent by teraterm to your microcontroller.

4.3 Transmitting data over UART


The following code continuously sends “OK” to PC over UART.
#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
// function to initialize UART communication
void UART_initializer ()
{
UBRR0L=0xCF; // set baud rate to 4800
UCSR0B |= (1<<TXEN0) ; // enable transmitter
UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01) ; // set frame format, 8 data and 1 stop bit
}
// function to transmit data over UART
void UART_transmitter ()
{
char msg[ ] = {'\n', '\r', 'O', 'K', '\n', '\r'} ;
char size_msg = 6;
unsigned char i ;
for ( i = 0; i < size_msg ; i=i+1)
{
while ( !( UCSR0A & (1<<UDRE0) ) ); /* Wait for empty transmit buffer
*/
UDR0 = msg[ i ] ; /* Put data into buffer, sends the data */
}
}
int main( void )
{
UART_initializer () ; // initialize UART
while (1)
{
UART_transmitter () ; //transmit ”OK”
_delay_ms(500);
}
}

1 https://tera-term.en.lo4d.com/
2 https://github.com/Uthmanhere/EE222/blob/master/teratermtutorial.pdf

9
Explanation

• Configure UART Communication protocol

– Line 7: Baud rate is being set to 4800 bps as per following calculations

for us, fOSC = 16MHz and assuming the required BAUD rate is 4800bps, the value of
UBRR equates to

𝑈𝐵𝑅𝑅 = 16 ×
106 − 1 ≅ 207
16 ×
4800

where the hexadecimal equivalent of 207 is CF16

– Line 8: Enable transmission by raising TXEN0 bit in UCSR0B (UART Control Status
Register B).
– Line 9: In UCSR0C, setting up UCSZ02:UCSZ00 to 0112 leads to 8 bit data chunk
size for data transfer as the following table referred from datasheet suggests.

• Transmit over UART communication protocol

– Line 14: Initialize array.


∗ \r Carriage return: Move the cursor to beginning of the line.
∗ \n Newline: Move the cursor to next line.
– Line 19: Poll over UART Data Register Empty flag (UDRE0) in UCSR0A to prepare
UART Data Register (UDR0) for transferring next data packet.

• Main: The over-all functionality of this code is to initialize UART and send data to PC
every half a second.

1
0
4.4 Receiving data over UART
The following code assumes LEDs are connected to each pin of PORTB. It receives data
from UART and turns on the respective LED. For example, if character ’5’ is sent, the fifth
LED will lit up. The program will ignore characters sent other than digits from 0-7.

#define F_CPU 16000000


#include <avr/io.h>
#include <util/delay.h>
// function to initialize UART communication
void UART_initializer ()
{
UBRR0L=0xCF; // set baud rate to 4800
UCSR0B |= (1<<RXEN0) ; // enable receiver
UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01) ; // set frame format, 8 data and 1 stop bit
}
// function to receive data over UART
void UART_receive ()
{

while ( !( UCSR0A & (1<<RXC0) ) ); /* Wait for data to be received */


char val = UDR0 ; /* Get and return received data from buffer */
if (val >= '0' && val < '6' ) // check the ASCII sent and
PORTB = 1 << ( val - '0' ) ; // turn on the respective LED

}
int main( void )
{
DDRB=0xFF;
PORTB=0xFF;
UART_initializer () ; // initialize UART
while (1)
{
UART_receive () ; //receive data
}
}

Explanation This piece of code is not much different from the one we implemented in Task A. Following
differences have been highlighted.

• Line 8: Receive RXEN0 is enabled instead of transfer.

• Line 17,18: Evaluates digit out of ASCII using expression (val - ‘0’) and set the
particular bit high.

1
1
5 Lab Tasks
5.1 Task A
Establish connection between your ATmega328p and PC through UART-to-TTL converter. Run the
programs in section 3.3 and 3.4 and explain your observations through screenshots.
4.3 Output:

4.4 Output:

1
2
5.2 Task B
Connect a 7-segment-display with your ATmega328p. Establish connection between your
ATmega328p and PC through UART-to-TTL converter. Write a program in C such that;

• Initially, ZERO should be displayed on the 7-segment and the program should wait for
the next instruction from PC.

• If a ‘u’ is sent from PC to ATmega328p, the next digit on seven segment should be
displayed in ascending order.
Ascending order (0,1,2,3,...,8,9,0,1,...)

• If a ‘d’ is sent from PC to ATmega328p, the next digit on seven segment should be
displayed in descending order.
Descending order (9,8,7,...,2,1,0,9,...)

• If any character other than ‘u’ or ‘d’ is sent, do nothing with 7-segment-display and
send “ERR” back to PC in order to notify error.

Implement the above functionality using AVR SERIAL INTERRUPTS.

Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

volatile char received_char;


volatile uint8_t current_digit = 0;

uint8_t digit_map[] = {
0b00111111,
0b00000110,
0b01011011,
0b01001111,
0b01100110,
0b01101101,
0b01111101,
0b00000111,
0b01111111,
0b01101111
};

void UART_init(uint16_t baudrate) {


uint16_t ubrr = F_CPU / 16 / baudrate - 1;
UBRR0H = (ubrr >> 8);
UBRR0L = ubrr;
UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
1
3
}

void UART_send_char(char c) {
while (!(UCSR0A & (1 << UDRE0)));
UDR0 = c;
}

void UART_send_string(const char *str) {


while (*str) {
UART_send_char(*str++);
}
}

void display_digit(uint8_t digit) {


PORTD = digit_map[digit];
}

ISR(USART_RX_vect) {
received_char = UDR0;
if (received_char == 'u') {
current_digit = (current_digit + 1) % 10;
display_digit(current_digit);
} else if (received_char == 'd') {
current_digit = (current_digit == 0) ? 9 : (current_digit - 1);
display_digit(current_digit);
} else {
UART_send_string("ERR\n");
}
}

int main(void) {
DDRD = 0xFF;
PORTD = 0x00;

UART_init(9600);
sei();

display_digit(current_digit);

while (1) {
}
}

1
4
Output:

1
5

You might also like