0% found this document useful (0 votes)
18 views4 pages

Lab 02 Introduction To AVR ATMEGA328P

Uploaded by

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

Lab 02 Introduction To AVR ATMEGA328P

Uploaded by

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

Department of Electrical and Computer Engineering

Course Instructor: ____________________ Dated: ________________

Semester: __________________________ Section: ________________

ECE-261L: Introduction to Embedded Systems Lab

Lab 02: Introduction to AVR Microcontroller and Assembly Language

Name Reg. No. Lab Tasks Viva Marks Total Marks


Marks

ECE-261L: Introduction to Embedded SystemsPage 1


Lab 06: Introduction to AVR Microcontroller and Assembly Language

1. Refer to the manual ‘Asm Programming in Atmel Studio 7’ to get familiar with the
Microchip Studio. Note that we would be using Microchip Studio, as it is now the
preferred IDE by Microchip, but the steps are the same. You can use Microchip Studio to
write and debug Assembly programs for ATMEGA328P. It also generates the hex file to
be uploaded to ATMEGA328P.
2. We would be using ATMEGA328P mounted on Arduino Uno board available in the lab.
lab.ino and lab.S files are provided to you to get started with the porting of assembly
programs to Arduino. Arduino provides its own IDE and Arduino Programming
Language (similar to C but not exactly C). Open the lab.ino file provided to you.
3. Every Arduino code is divided into two main functions or parts. The setup() function
that is executed only once. Generally, we put the stuff here that we do not want to change
throughout the execution of our system. It could include things like directions of pins,
some static variables and much more. The other function loop() is the one where the
desired functionality of the system goes.
We are not going for the traditional use of these functions, instead, we would call two
other functions inside these functions, i.e., mysetup() inside setup(), and
myloop() inside loop(). We are going to declare these functions using the keyword
extern, which basically allows us to import functions defined in another file. That file is
our lab.S file (here, the file name excluding the extension needs to be same, e.g., lab.ino
file has corresponding lab.S file), which contains these two functions (mysetup() and
myloop()), but, these are written in assembly language instead of C. So technically,
these are subroutines. These subroutines are declared to be global with the help of .global
directive, so that they can be fetched by some other file as well (that file is lab.ino in our
case). If you open a .ino file, the associated .S (if it exists in the same directory) file also
appears in Arduino IDE.
extern "C"
{
void mysetup();
void myloop();
}
void setup() {
// put your setup code here, to run once:
}

void loop() {
// put your main code here, to run repeatedly:
}
; lab.ino

ECE-261L: Introduction to Embedded SystemsPage 2


#define __SFR_OFFSET 0x00
#include "avr/io.h"
;------------------------
.global mysetup
.global myloop
;------------------------
mysetup:
LDI R16,0xFF
OUT DDRB,R16
RET ;return to setup() function
;------------------------
myloop:
L1: OUT PORTB,R16
COM R16
JMP L1
RET ;return to loop() function
;lab.S

Lab Task 1:
a) Compile the given file (lab.ino) and upload the code to the Arduino Uno board available in
the lab. Show the output on an oscilloscope by connecting any pin of port b to the probe of
oscilloscope. Note down the time period and frequency of the generated signal. Understand
the code and calculate the time period and frequency.
b) You are provided another Arduino project file (lab00.ino). Compile this file as well and
upload the code to the Arduino Uno board. Compare the output with a) and give your
comments.
c) What is the maximum frequency of square wave (50% duty cycle) you can generate using i)
assembly code ii) Arduino Programming language?
d) What is the maximum frequency of square wave you can generate using i) assembly code ii)
Arduino Programming language?
Lab Task 2:
Refer to the datasheet of ATMEGA328P, and
a) Write the addresses of the following registers:

Register Address I/O Address


DDRB
DDRC
DDRD
PORTB
PORTC
PORTD
PINB
PINC

ECE-261L: Introduction to Embedded SystemsPage 3


PIND

b) Calculate the addresses for SRAM and Program Memory.


c) Write the size of PC and show that it can access the entire program memory.
d) Mention the group of instructions that does not affect the SREG register.

Lab Task 3: Write an assembly language code to generate two square waves (50% duty cycle) of
1Hz and 0.5Hz. Write subroutines for the purpose. Provide a switch on pin 3 of PORTB (or any
other pin of your choice) to select between the two waves to be generated on pin 4 of PORTB (or
any other pin of your choice) and show on oscilloscope. Show your implementation to the lab
instructor.
Lab Task 4: The trainer board provided in the lab has 7-segment led displays with built-in BCD
to 7-segment decoder. Using the delay in subroutines written in Lab Task 3, can you build a
simple 1-digit and 2-digit counter?

ECE-261L: Introduction to Embedded SystemsPage 4

You might also like