Lab 11
Lab 11
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:
3. Setup serial link between your PC and ATmega328p microcontroller through UART
interface.
2.2 Deliverables
You are required to submit:
• Code
next lab.
3 Hardware Resources
• Arduino Uno board with ATmega328p microcontroller
• 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.
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.
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.
1 https://tera-term.en.lo4d.com/
2 https://github.com/Uthmanhere/EE222/blob/master/teratermtutorial.pdf
9
Explanation
– 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
– 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.
• 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.
}
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 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.
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
uint8_t digit_map[] = {
0b00111111,
0b00000110,
0b01011011,
0b01001111,
0b01100110,
0b01101101,
0b01111101,
0b00000111,
0b01111111,
0b01101111
};
void UART_send_char(char c) {
while (!(UCSR0A & (1 << UDRE0)));
UDR0 = c;
}
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