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

Lab_11_Embedded System AVR

Lab 11 focuses on AVR Serial Communication using the ATmega328p microcontroller, detailing the setup for UART communication, including hardware requirements and software configuration. The lab includes tasks for transmitting and receiving data over UART, as well as controlling a 7-segment display based on commands received from a PC. Students are expected to complete specific tasks and submit their code and observations.

Uploaded by

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

Lab_11_Embedded System AVR

Lab 11 focuses on AVR Serial Communication using the ATmega328p microcontroller, detailing the setup for UART communication, including hardware requirements and software configuration. The lab includes tasks for transmitting and receiving data over UART, as well as controlling a 7-segment display based on commands received from a PC. Students are expected to complete specific tasks and submit their code and observations.

Uploaded by

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

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

1
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.

2
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.

3
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.

4
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

5
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.

6
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.

7
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.

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.

8
Task A
Code given in section 4.3 and 4.4 was used to implement the following hardware.

Task B
Code:

#define F_CPU 16000000UL

#include <avr/io.h>

#include <avr/interrupt.h>

#include <util/delay.h>

// Global variables

volatile uint8_t digit = 0; // Current digit to display

9
// Lookup table for 7-segment display (Common Cathode)

const uint8_t segment_codes[10] = {

0b00111111, // 0

0b00000110, // 1

0b01011011, // 2

0b01001111, // 3

0b01100110, // 4

0b01101101, // 5

0b01111101, // 6

0b00000111, // 7

0b01111111, // 8

0b01101111 // 9

};

void UART_init(void) {

// Set baud rate to 4800

UBRR0H = 0;

UBRR0L = 207; // UBRR = F_CPU / (16 * Baud) - 1

// Enable receiver and transmitter with interrupt on receive

UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);

// Set frame format: 8 data bits, 1 stop bit

UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);

10
void send_UART_message(const char *message) {

while (*message) {

while (!(UCSR0A & (1 << UDRE0))); // Wait for the buffer to be empty

UDR0 = *message; // Send the character

message++;

void display_digit(uint8_t digit) {

PORTB = segment_codes[digit] & 0x7F; // Use lower 7 bits for PORTB

if (digit == 0 || digit == 1 || digit == 7) {

PORTD &= 0b01111111; // Clear PD7

} else {

PORTD |= 0b10000000; // Set PD7 for the 7th segment

ISR(__vector_18) { // Updated to match correct interrupt vector for USART_RX

char received_char = UDR0; // Read the received character

if (received_char == 'u') {

digit = (digit + 1) % 10; // Increment digit in ascending order

} else if (received_char == 'd') {

digit = (digit == 0) ? 9 : digit - 1; // Decrement digit in descending order

} else {

send_UART_message("ERR\r\n"); // Send error message if invalid input

return;
11
}

display_digit(digit); // Update display

int main(void) {

// Configure PORTB for 7-segment display and PD7 for 7th segment

DDRB = 0xFF; // All pins of PORTB as output

// Initialize UART

UART_init();

DDRD |= 0b10000000; // Configure PD7 as output

// Enable global interrupts

sei();

// Display initial digit (0)

display_digit(digit);

// Main loop

while (1) {

// All tasks are handled in the ISR

return 0;

}
12
Hardware Implementation:

13

You might also like