CSE466 Lab 1: “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 end of the count is reached, 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 (Course Pak, ATmega16 datasheet, pp 6-16, 21)
- General
I/O Ports (Course Pak, ATmega16 datasheet, pp 48-52)
Helpful Hints
- Instruction
set summary is on page 333 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: Wiring up the breadboard
- Wire the circuit in the schematic
on your breadboard. Make sure the power is disconnected when wiring up
your breadboard.
- The 7-segment LEDs displays are not connected to the
ATmega16 in the schematic. You and your partner will need to decide how to
connect the 8 wires from the ATmega16 to the 7-segment LED displays. The
wiring pattern used should be exactly the same for both of the 7-segment
displays allowing the same constant to be used when displaying numbers.
Also note that the 7-segment LED displays combine to make the number (one
will represent the tens digit and the other will represent the ones
digit). To make the actual value of the counter easy to read it would be
nice if the 7-segment LEDs were either next to each other or aligned so
the bottom of the 7-segment LEDs were parallel.
- The resistors values have been purposefully left off
the schematic. You will need to consult the datasheet to find the chip’s
electrical characteristics to determine the appropriate values of the
reset pull-up resistor and the current limiting resistors for the LEDs.
For decent brightness the 7-segment LEDs should have around 5mA of
current.
Question 1: There are 3 parts to this question to help you find a
reasonable range for your pull-up resistor (HINT: A reasonable range is probably
the same as the internal pull-up resistor). Even though the ATmega16 contains
an internal pull-up resistor the goal of the question is to review how to
calculate a reasonable range for chips that do NOT have an internal pull-up
resistor. Keep in mind that you want to have large safety margins when choosing
the pull-up resistor to use in your reset circuit.
a. Calculate
the range of resistance values for the reset pull-up resistor. Refer to the
schematic to visualize the circuit in question.
The minimum
resistor value must be calculated for the case when the reset switch is
closed. When this switch is closed, Vcc
and ground are connected directly through the pull-up resistor. The maximum
current draw is limited by the 1/4 W resistors we use in this class. Calculate
the min resistor value given this information. Remember V = I * R and Power = V
* I.
The maximum
resistor value must be calculated for the case when the reset switch is open. The
minimum voltage for which the reset pin is considered high can be found on page
291. For the reset circuit to function properly, the pull-up resistor must
allow at least a minimum amount of current to flow to the chip. A good indication
of what this minimum current should be is the maximum input leakage current for
I/O pins listed on page 291. Using the value of Vcc, minimum reset high voltage and
minimum input current values, calculate the max resistor value.
What is the
theoretically largest possible range from the values you calculated? (List the
min/max values) In practice a more reliable system will narrow the range
significantly to minimize the risk of failure due to changing conditions.
b. The ATMega16 includes an internal reset pull-up resistor, what is the range
of this resistor as specified in the datasheet (see page 291)?
c. Choose a pull-up resistor that is within the acceptable range for the
internal pull-up resistor. What resistance value did you choose?
Question 2: Calculate the theoretical amount of current
flowing through a single segment of the 7-segment LEDs when it is
illuminated. In your calculation use only values from datasheets or
labels. Do not use any measured values in your calculation.
- Before powering on the breadboard, verify with a
multimeter that there are no shorts between power and ground, and that
power and ground are actually hooked up to the chips' pins.
- Power on your breadboard and connect the serial cable
from your computer.
- To verify your hardware is working properly program
your wired up breadboard with flash.hex. (This
program should cause your 7-sement LEDs to flash “88”)
- The ATMega16 is programmed via an application called
MegaLoad. When the ATMega16 is reset, it attempts to communicate with
MegaLoad over the serial port to upload the specified hex file. NOTE: This
communication can cause a delay when resetting the ATmega16 as it will
attempt to communicate with MegaLoad before executing your program. (the
delay is especially long when MegaLoad is not running because of the
timeout)
To upload a program to the ATmega16:
- Open MegaLoad (c:\program files\microsyl or from the
shortcut on your Desktop) and select the hex file you want to upload to
the flash of the ATmega16.
- Reset the ATmega16 (this will cause the
communication to begin).
Part 2: Create a simple program in Assembly
- Open
AVRStudio 4 and follow these instructions to setup a new project:
- Click
on the “Create New Project” button that is on the window that appears when
it opens.
- The
Project Type is “AVR Assembler”. Give
the project a name and change the location to a lab partner’s network
share. Then hit next.
- Select
“AVR Simulator” as the Debug Platform
and “ATmega16” as the Device then hit finished.
- You
should also verify that you output will be in .hex so go to the Project
menu and select AVR Assembler Setup.
The Additional Output file format should be set to “Intel intellec 8/MDS
(Intel hex)”.
- Copy
the following code to the assembly file. (The code should display an “8”
on one of the 7-segment LEDs)
.include "C:\Program Files\Atmel\AVR
Tools\AvrAssembler\Appnotes\m16def.inc"
.cseg
ldi r16, 0xff
out DDRB, r16
ldi r16, 0x00
out PORTB, r16
loop:
jmp loop
- Build
the hex file and upload it to the ATMega16 via MegaLoad.
- Modify
the code to determine the constants needed to generate the other 9 numbers
on the 7-segment LED displays.
Part 3: 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 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.
Question 3: 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 taken. Please show your work and list any assumtions you
made.
Deliverables
Demonstrate your
working system to a TA. You can either do this during this lab, or during the
first
1/2 hour of the next lab. Counting should happen at 1 Hz.
Turn in the
answers to the three lab questions and a hardcopy of your commented
assembly code.