Lab 2: AVR Programming
Assigned: Tuesday, June 28
The goal of this lab is to become familiar with the ins and outs
of
programming on the AVR mini.
0. Set up your
environment to use the
AVR
a. Copy the pre-prepared AVR and
AVRlib files to your home
directory by running the following script:
cd ~
/usr/ccrma/web/html/workshops/mid2005/setup
b. Make
a
directory for Lab 2 in your MID directory
cd ~
mkdir MID/lab2
c. Set up AVR
connection to PC
Connect power cable, connect serial cable to
right hand port, and
power-up the AVRmini
d. Test AVR setup with
"flash"
test code
Make yourself a copy of the FLASH demo
from
avrlib-demos:
cd ~/MID/lab2
cp -r ~/avrlib-demos/flash .
cd
flash
Compile and download the code to the AVRmini.
Press the
RESET button on the AVRmini and within 2 seconds type:
make
load
You should see a sort of makeshift progress bar as a bunch
of "#" characters print. If the program transferred
successfully, then press RESET again. Connect the PBLED header to the
PORTD header.
If everything is working, a LED on the AVRmini board
should start
flashing.
1. AVRMini Input/Output
a. Review
architecture
and programming, as necessary
b. Momentary pushbutton
- Compile and load BUTTON from the avrlib-demos to your Lab 2
folder:
cd ~/MID/lab2
cp -r ~/avrlib-demos/button .
cd button
make
load
- This requires using PORT B instead of D. (Connect
the PBLED header to the PORTB header.)
- When you press SW4
("Switch number 4"), LED0 ("Light Emitting Diode number
0") should light
- Look at the code in an editor:
nedit button.c
In particular, note that there's a comment
near the top of the code that tells you
// USING THE AVRMINI
DEVELOPMENT BOARD, CONNECT
// THE LED/PUSHBUTTON JUMPER TO THE PORT B
JUMPER.
All of the
demo programs in avrlib-demos have a comments
like this.
Optional: Toggle pushbutton using
debounce.
c. Four buttons:
- Write a program to control 4 LEDs with 4 buttons. It's up to you to do some "interaction design": think of your own way for actions with the four buttons to control the states of the LEDs. Feel free to write this program from scratch, but we recommend that you start with the FOURTOGGLE demo.
d. Beep:
- Load BUTTONBEEP as you did BUTTON and FLASH above.
- Make sure the blue wire from your speaker is connected to pin D5 and the black wire from the speaker to ground.
- You should hear a beep when you press SW4.
- Try inserting a pause between turning on the LED and playing the beep. In case you didn't do part 3b, here are the step-by-step instructions for using the avrlib's timer facilities:
- Add #include "timer.h" at the top of buttonbeep.c
- Add timerInit(); to the beginning of your main() procedure.
- Add a call to the timerPause() procedure in the right place in your program.
- In your makefile, add $(AVRLIB)/timer.c to the list of files in the definition of SRC
- At what point does the delay become noticeable? What if the beep comes before the light?
- Try finding the largest unnoticeable delay time as in #3 using the beep as feedback. Is it different?
3. Compile, Run and Play the AVRsimon:
- Compile the code and program the AVR:
cd ~/MID/lab2
cp -r ~/avrlib-demos/avrsimon .
cd avrsimon
make load
You should now be
able to play the Simon game provided. Pay attention to things you like and dislike about the flow of the game and the interaction.
Improve the game to make it more interesting and engaging. Here are some suggestions, but feel free to come up with others:
- Keep stats - this is an easy addition involving adding two counters for correct and incorrect performance, and adding a way to start a new game without the reset button.
- Add levels of play - make it get progressively more difficult (e.g., longer patterns, increasing tempo)
- Make it more interactive - give the user more choices on the style of the game (e.g., select a level)
- Add timing - Try to evaluate the user's rhythmic accuracy
- Change the nature of the game somehow