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

Lab 2 Microcontroller Group G

Uploaded by

Hazwan Haziq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab 2 Microcontroller Group G

Uploaded by

Hazwan Haziq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Malaysia-Japan International Institute of Technology

(MJIIT)

ELECTRONIC ENGINEERING LABORATORY III

SMJE 3192

MICROCONTROLLER LABORATORY

I/O Program On AVR


-LAB 2 REPORT-

NO. NAME MATRIC NO.


1 AFIQ SAIFILLAH BIN RAZALI A21MJ0002
2 CHEW ENG SING A21MJ3004

GROUP : G
SECTION : 01
LECTURER’S NAME : Ir. Dr. Mohd Hatta Bin Mohammed Arriff
DATE : 22 May 2024
OBJECTIVE

1. Learn how to develop simple I/O program on AVR.

2. Understand how to perform some addition on the board.

THEORY

When delving into the theory of IDEs designed specifically for AVR (Alf and Vegard's RISC
Processor) boards, the focus is on creating software environments tailored to facilitate the
development of embedded systems using AVR microcontrollers.

AVR microcontrollers are widely utilized in various embedded applications due to their simplicity,
reliability, and low power consumption. IDEs for AVR development typically offer a cross-
platform environment, ensuring developers have flexibility in choosing their development
platform without being constrained to a specific operating system.

These IDEs seamlessly integrate with the AVR toolchain, including compilers, assemblers, linkers,
and programmers, enabling developers to compile, assemble, link, and program AVR
microcontrollers within a unified environment.

The code editor within an AVR IDE is equipped with features specifically tailored to AVR
programming, such as syntax highlighting for AVR assembly language and C/C++ with AVR-
specific libraries. Additionally, it may include code completion, navigation, and other
productivity-enhancing tools.

To aid in debugging, AVR IDEs often incorporate simulators or emulators, allowing developers
to test code without physical hardware. These tools simulate the behavior of AVR
microcontrollers, facilitating code debugging and execution analysis in a controlled environment.
Debugging tools provided by AVR IDEs are tailored for embedded development, offering features
like real-time debugging, breakpoints, watch windows, and variable inspection to identify and
rectify bugs efficiently.

Many AVR IDEs offer features for configuring and generating code for AVR microcontroller
peripherals, simplifying the integration of hardware peripherals into embedded applications
through graphical interfaces for configuring register settings and generating initialization code.

Moreover, AVR IDEs often include libraries and frameworks specifically designed for AVR
microcontrollers, providing pre-written code for common tasks such as GPIO control, ADC
conversion, and communication protocols. These libraries enable developers to accelerate
development by reusing code and leveraging proven solutions for common challenges in
embedded systems programming.

Overall, the theory behind IDEs for AVR development centers on providing a comprehensive,
user-friendly environment that streamlines the process of creating embedded systems using AVR
microcontrollers. By integrating essential tools, features, and libraries into a single environment,
AVR IDEs aim to enhance productivity, reduce development time, and facilitate the creation of
reliable and efficient embedded applications.
EQUIPMENT

1. Computer
2. AVR Board
3. AVR Studio 4 IDE

PROCEDURE

1. Develop C code to display the number 0, 3, 6 and 9 given by keypad 0, 3, 6 and 9 into
7seg LED.
2. Expanding the above code to display the number 0-9 given by keypad 0-9 into 7seg LED.
3. Develop C code to use A and B keys as ‘+’ and ‘=’, respectively, and implement a 1-digit
calculator (if the result is larger than 9, only the right digit will be displayed).
4. Expanding the above code to be able to display results larger than 9 correctly in 2 digits.
5. Start discussion of the free project for week 4.
RESULT

Part 1 (Display number 0, 3, 6, 9 by keypad 0, 3, 6, 9 into 7 segLED)


Coding
#include<avr/io.h>
#include<util/delay.h>

int main(void)
{
DDRA = 0xFF;
DDRC = 0x0F;
DDRB = 0XFF;
While(1)
{
PORTC = 0x01;
if(PINC & 0x10)
{
PORTB = 0x00;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x20)
{
PORTB = 0x03;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x40)
{
PORTB = 0x06;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x80);
{
PORTB = 0x09;
PORTA = 0x00;
_delay_ms(200);
}

}
return 0;
}
Video
https://youtube.com/shorts/7fimPG6xy3Q
Part 2(Display number 0-9 by keypad 0-9 by keypad into 7segLED)
Coding
#include<avr/io.h>
#include<util/delay.h>

int main(void)
{
DDRA = 0xFF;
DDRC = 0x0F;
DDRB = 0XFF;
While(1)
{
PORTC = 0x01;
if(PINC & 0x10)
{
PORTB = 0x00;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x20)
{
PORTB = 0x03;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x40)
{
PORTB = 0x06;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x80);
{
PORTB = 0x09;
PORTA = 0x00;
_delay_ms(200);
}
PORTC = 0x02;
if(PINC & 0x10)
{
PORTB = 0x01;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x20)
{
PORTB = 0x04;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x40)
{
PORTB = 0x07;
PORTA = 0x00;
_delay_ms(200);
}
PORTC = 0x04;
if(PINC & 0x10)
{
PORTB = 0x02;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x20)
{
PORTB = 0x05;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x40)
{
PORTB = 0x08;
PORTA = 0x00;
_delay_ms(200);
}
}
Return 0;
}
Result (Video Link)
https://youtube.com/shorts/yxKDi1W1Ujs
Coding(Use A and B as ‘+’ and ‘=’ respectively and implemented a 1-digit calculator)
#include<avr/io.h>
#include<util/delay.h>

int main(void)
{
DDRA = 0xFF;
DDRC = 0x0F;
DDRB = 0XFF;
int a, b, c, sum;

While(1)
{
PORTC = 0x01;
if(PINC & 0x10)
{
PORTB = 0x00;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x20)
{
PORTB = 0x03;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x40)
{
PORTB = 0x06;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x80);
{
PORTB = 0x09;
PORTA = 0x00;
_delay_ms(200);
}
PORTC = 0x02;
if(PINC & 0x10)
{
PORTB = 0x01;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x20)
{
PORTB = 0x04;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x40)
{
PORTB = 0x07;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x80)
{
b=a;
PORTB = (0x20)+b;
PORTA = 0x00;
_delay_ms(200);
}
PORTC = 0x04;
if(PINC & 0x10)
{
PORTB = 0x02;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x20)
{
PORTB = 0x05;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x40)
{
PORTB = 0x08;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x80)
{
c=a+b;
PORTB = (0x20)+c;
PORTA = 0x00;
_delay_ms(200);
}
}
return 0;
}
Result (Video Link)
https://youtube.com/shorts/nfIxCv5prz4
Part 3
Coding
Coding(Correctly display results larger than 9 in 2 digits on 7segLED)
#include<avr/io.h>
#include<util/delay.h>

int main(void)
{
DDRA = 0xFF;
DDRC = 0x0F;
DDRB = 0XFF;
int a, b, c, sum;

While(1)
{
PORTC = 0x01;
if(PINC & 0x10)
{
PORTB = 0x00;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x20)
{
PORTB = 0x03;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x40)
{
PORTB = 0x06;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x80);
{
PORTB = 0x09;
PORTA = 0x00;
_delay_ms(200);
}
PORTC = 0x02;
if(PINC & 0x10)
{
PORTB = 0x01;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x20)
{
PORTB = 0x04;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x40)
{
PORTB = 0x07;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x80)
{
b=a;
PORTB = (0x20)+b;
PORTA = 0x00;
_delay_ms(200);
}
PORTC = 0x04;
if(PINC & 0x10)
{
PORTB = 0x02;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x20)
{
PORTB = 0x05;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x40)
{
PORTB = 0x08;
PORTA = 0x00;
_delay_ms(200);
}
if(PINC & 0x80)
{
c=a+b;
if(c>=10)
{
sum = c-10;
PORTA = 0x00;
PORTB = (0x10)+1;
_delay_ms(200);
PORTB = (0x20)+sum;
_delay_ms(200);
}
Else
{
PORTB = (0x20)+c;
PORTA = 0x00;
_delay_ms(200);
}
}
}
return 0;
}
Result (Video Link)
https://youtube.com/shorts/R0nIq-MD1ZE
Discussion

When we start to do the lab work, we encountered several challenges in the lab due to
damaged connections on the board. We had to replace the board multiple times before finding
one that functioned properly. Despite these setbacks, we were determined to complete the lab
and worked through each obstacle diligently. Once we successfully connected the board, we
began programming the AVR microcontroller to read inputs from switches and buttons and
output data to LEDs and displays. Using the AVR Studio IDE and C++ programming language,
we wrote and implemented simple programs to achieve these functions.

After completing our coding, we noticed there was no output on the board. This puzzled
us, so we rechecked our code and connections. We tested the code on different boards, but the
issue persisted until we ran the program on another computer, which finally produced output on
the board. The reason for this discrepancy remains unclear.

Our first task was to display the outputs 0, 3, 6, and 9 on the 7-segment displays by
pressing the button in the first column of the 3x4 Matrix Keyboard. This involved configuring
the keypad to detect the keys corresponding to these numbers and translating the input into
output signals for the 7-segment LED.

The second task was to code a program that could display all 10 digits (0-9) on the 7-
segment LED. This required more sophisticated coding, including the ability to scan multiple
keys simultaneously and convert binary values into hexadecimal or decimal representations.

The third task involved creating a simple one-digit calculator using the "A" and "B" keys
on the keypad as the addition and equal buttons, respectively. This required developing code for
basic arithmetic operations and displaying the results on the 7-segment LED. We noticed an
issue where the "0" and "2" keys on the 3x4 Matrix Keyboard were swapped during addition. To
fix this, we needed to interchange the positions of "0" and "2" in the code, allowing for
successful addition.
The fourth task expanded on the third by modifying the calculator program to correctly
display results larger than 9 using two digits. This required programming conditional statements
and loops to control the display output accurately.
Conclusion

In summary, developing simple I/O programs on the AVR microcontroller was a highly
educational experience. Through this lab, we gained a solid understanding of the basic I/Os
functionalities of AVR microcontrollers. We learned how to write and implement simple
programs using the AVR Studio IDE and the C programming language. We successfully
programmed the AVR microcontroller to read inputs from switches and buttons and output data
to LEDs and displays.

Overall, this lab gave us an excellent introduction to working with AVR microcontrollers
and provide a strong foundation for future projects involving more complex I/O operations. With
further practice and experimentation, we can continue to expand our knowledge and skills in this
area.

You might also like