Lab_11_Embedded System AVR
Lab_11_Embedded System AVR
Contents
1 Acknowledgements ................................................................................................................ 2
2 Administrivia ............................................................................................................................2
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:
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.
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.
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.
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.
1 https://tera-term.en.lo4d.com/
2 https://github.com/Uthmanhere/EE222/blob/master/teratermtutorial.pdf
5
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.
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.
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 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.
8
Task A
Code given in section 4.3 and 4.4 was used to implement the following hardware.
Task B
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
// Global variables
9
// Lookup table for 7-segment display (Common Cathode)
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
void UART_init(void) {
UBRR0H = 0;
10
void send_UART_message(const char *message) {
while (*message) {
while (!(UCSR0A & (1 << UDRE0))); // Wait for the buffer to be empty
message++;
} else {
if (received_char == 'u') {
} else {
return;
11
}
int main(void) {
// Configure PORTB for 7-segment display and PD7 for 7th segment
// Initialize UART
UART_init();
sei();
display_digit(digit);
// Main loop
while (1) {
return 0;
}
12
Hardware Implementation:
13