CSEP567 PreLab 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
Task: Create a BCD Counter in Assembly
Write a program in
assembly that will count from 00 to 59 on two 7-segment led displays (one will
be the tens digit and the other will be the ones digit). Implement a lookup
table in program memory to convert a 4-bit nibble (in binary) to a decimal
number. Use this skeleton program as a
starting point.
Requirements:
-
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 should have determined these
constants by the end of Lab 1. 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.
Question 1: Prove that you are counting at a rate of 1Hz through calculations
by using the number of cycles each instruction takes. In the case of branches
where a variable number of cycles may result, you may assume that the most
frequent branch is always taken. Please show your work and list any assumtions
you made.
Deliverables
Come to lab with:
-
Code ready to test on your breadboard
-
A printed out copy of your assembly code
-
A written answer to Question 1
Use the simulator in AVR Studio 4 to verify that
your code is working and the timing is correct. To set the frequency/speed
of the processor in the simulator goto the AVR simulator options in the
debug menu. (The cyrstal we are using is not present so you will need to some
math to make adjustments to get your timings to work out correctly) When
simulating the I/O tab gives very useful information such as the values of the
registers, I/O, etc (If you don't see the I/O tab on the left toggle the
workspace window button to display the project, I/O, and info tab). The
"Processor" item in the I/O tab keeps track of the cycles used and the amount
of time the processor has run.