CSE466 Lab 2: "One-Minute Timer"
Objectives
Create a device that will count from 00 to 59 on two 7-segment
led displays at a rate of 1Hz. When the counter reaches 59 it should wrap
around and start counting from the beginning. There will be an example setup in
the lab that you can use as a reference.
In this lab you will learn the following:
-
AVR assembly
-
Basics of the ATmega16 (Memory, registers, etc)
-
Binary to decimal 7-segment conversion
-
Become familiar with AVRStudio 4 (which has a simulator)
Reading
-
AVR CPU Core & Memory ( ATmega16 datasheet, pp 6-16, 21)
-
General I/O Ports ( ATmega16 datasheet, pp 48-52)
-
Interrupts (ATmega16 datasheet, pp 43-47)
Helpful Hints
-
Instruction set summary is on page 331 of the ATmega16 datasheet that is
located in your course pack
-
Detailed instruction descriptions available
at Atmel's website.
Please don't print out (149 pages) as the rest of your programming will be in
C.
-
General purpose registers are r0-r31, watch for instructions that only operate
on some of them (ldi, load immediate, only works on r16-31)
-
Include [.include "m16def.inc"] to get logical names [PORTA] for registers (as
opposed to having to use addresses [$3B])
Resources
Part 1: BCD Counter implemented with a loop (Demonstrate Prelab)
Demonstrate
your working BCD Counter to a TA.
Requirements:
-
Count from 00 to 59.
-
Use a lookup table in program memory to store the constants needed to generate
the 10 digits ('0'-'9') on a 7-sement LED. You will need to use the LPM
instruction to load the constant from program memory. NOTE: The ATmega16 uses
16-bit instructions even though it is an 8-bit processor; therefore, the 256th
instruction is actually located at memory address 512 and 513. Keep this in
mind when declaring your constants in program memory. The code/instructions are
two bytes wide (16 bits) but when the program accesses the program memory it
can only access one byte (8 bits) at a time causing the number of addresses to
be doubled.
-
The decimal point segment should toggle on every change of counter value.
-
Counting should happen at 1 Hz and should be achieved through the use of a
delay loop. Your counter should count at a rate as reasonably close as possible
to 1 Hz since you know how many cycles each instruction takes to execute. You
can use the 'nop' instruction to fine tune where necessary.
Part 2: BCD Counter implemented with interrupts
Modify
your code from Part 1 to utilize Timer 1's output compare interrupt.
Basically, reimplement the 1Hz counter by eliminating the delay loop and
replacing it with code that uses the output compare interrupt from Timer1
to increment the counter. Remember to use the RETI instruction when returning
from the interrupt and make sure to jump from the correct interrupt address.
Requirements:
-
Count from 00 to 59.
-
Use a lookup table in program memory to store the constants needed to generate
the 10 digits ('0'-'9') on a 7-sement LED. You will need to use the LPM
instruction to load the constant from program memory. NOTE: The ATmega16 uses
16-bit instructions even though it is an 8-bit processor; therefore, the 256th
instruction is actually located at memory address 512 and 513. Keep this in
mind when declaring your constants in program memory. The code/instructions are
two bytes wide (16 bits) but when the program accesses the program memory it
can only access one byte (8 bits) at a time causing the number of addresses to
be doubled.
-
The decimal point segment should toggle on every change of counter value.
-
Counting should happen at 1 Hz and should be achieved through the use of Timer
1 output compare interrupt.
Question 1: What are the advantages of using Timer1 verses Timer2 to
increment the timer? Explain.
Question 2: Why do the Reset handler jump and Timer 1 interrupt handler jump
need to be located at a specific address?
Deliverables:
Demonstrate to a TA that Part 2
counts at 1 Hz. Turn in the answers
to the two lab questions and a hardcopy of your commented assembly
code.
If you have time and want to get a head start for next week's
lab you may do the following (Completely Optional):
Go through the entire Oscilloscope Tutorial
(consult the Oscilloscope Manual
as needed)
Add two buttons (pins 18 and 19), a potentiometer (pin 40), a LED (pin 21), a
wire to Vcc (pin 32), a photoresistor (pin 39) and bypass capacitors to
complete the circuit shown in the
schematic on your breadboard. Again, before powering your circuit back
up, you should confirm that you don't have any shorts between Vcc and ground
with the multimeter.