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 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 (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 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: Wiring up the breadboard
- 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 resonable 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 resonable 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. Refer to the ATMega datasheet section on external reset to calculate the extreme range of resistance values for the reset pull-up resistor. The maximum current draw is limited by the 1/4 W resistors we use in this class. The minimum current draw must be greater than the maximum Input Leakage Current (refer to electrical characteristics section in the datasheet). Remember V = I * R and Power = V * I. What is this theoretically largest possible range? (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?
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.
- 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 place the 7-segment
LEDs so that they are horizontally aligned and the bottom of the 7-segment
LEDs are parallel. The left most 7-segment should be the tens-digit.
- 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. Ask a TA if you need instruction on how to use a multimeter
- 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) 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
"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 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
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.