Lab 02 Introduction To AVR ATMEGA328P
Lab 02 Introduction To AVR ATMEGA328P
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
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:
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?