Difference between revisions of "PID2008 Lab 1"

From CCRMA Wiki
Jump to: navigation, search
(Beep)
(Mac)
 
(32 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
<font size=5>AVR Programming</font><br>
 
<font size=5>AVR Programming</font><br>
Assigned: Monday, June 25
+
Assigned: Monday, July 7
  
 
The goal of this lab is to become familiar with the ins and outs of programming on the AVR mini.
 
The goal of this lab is to become familiar with the ins and outs of programming on the AVR mini.
Line 8: Line 8:
 
===Follow OS specific instructions for installing the [[AVR]] environment===
 
===Follow OS specific instructions for installing the [[AVR]] environment===
  
* In all cases, this entails downloading the avrlib to your home directory. At CCRMA, it is easiest to use subversion:
+
* In all cases, this entails downloading the avrlib to your home directory. It is easiest to use subversion.
  
     ~> cd
+
====CCRMA Linux====
     ~> svn checkout file:///usr/ccrma/group/pid/avr/svn/avrlib ./avrlib
+
* On a CCRMA Linux machine, open a terminal
 +
* From the terminal type
 +
     $ cd
 +
     $ svn co file:///usr/ccrma/group/pid/svn/avr/avrlib ./avrlib
  
===Create a pid2007 directory and copy the avrlib-demos to it===
+
====Mac====
 +
* On a Mac, you'll first need to make sure you have Subversion installed. You can get it here: [http://www.collab.net/downloads/community/ http://www.collab.net/downloads/community/]
  
            ~> mkdir pid2007
+
* Then, open a Terminal (the Terminal app is in /Applications/Utilities) and type:
            ~> cd ~/pid2007
+
    $ cd
     ~/pid2007> svn checkout file:///usr/ccrma/group/pid/avr/svn/avrlib-demos ./avrlib-demos
+
     $ svn co svn+ssh://ccrma-gate.stanford.edu/usr/ccrma/group/pid/svn/avr/avrlib ./avrlib  
  
===Make a directory for Lab 1 in your pid2007 directory===
+
* You'll be prompted for your CCRMA password (maybe twice)
  
     ~/pid2007> mkdir lab1
+
===Create a pid2008 directory (choose a meaningful name) and copy the avrlib-demos to it===
 +
 
 +
For example, if your name is Clarence, you might want to make a directory called clarence
 +
 
 +
     $ mkdir pid2008
 +
    $ cd ~/pid2008
 +
    $ svn co file:///usr/ccrma/group/pid/svn/avr/avrlib-demos ./avrlib-demos
 +
 
 +
As before, if you are using a Mac, replace file:// with svn+ssh://ccrma-gate.stanford.edu
 +
 
 +
===Make a directory for Lab 1 in your pid2008 directory===
 +
You can do this from the Linux file browser, the Mac Finder, or from the terminal:
 +
 
 +
    $ cd ~/pid2008
 +
    $ mkdir lab1
  
 
===Set up AVR connection to PC===
 
===Set up AVR connection to PC===
Line 31: Line 49:
 
*Make yourself a copy of the FLASH demo from avrlib-demos:
 
*Make yourself a copy of the FLASH demo from avrlib-demos:
  
                      ~> cd ~/pid2007/lab1
+
    $ cd ~/pid2008/lab1
        ~/pid2007/lab1> cp -r ../avrlib-demos/flash .
+
    $ cp -r ../avrlib-demos/flash .
        ~/pid2007/lab1> cd flash
+
    $ cd flash
  
*Compile the code type:
+
*Compile the code. Type:
  
        ~/pid2007/lab1/flash> make
+
    $ make
  
 
*Download the code onto the AVRmini. Press the RESET button on the AVRmini and within 2 seconds type:
 
*Download the code onto the AVRmini. Press the RESET button on the AVRmini and within 2 seconds type:
  
        ~/pid2007/lab1/flash> make load
+
    $ 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 PORTB header.
 
*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 PORTB header.
Line 57: Line 75:
 
* Copy the BUTTON program from the avrlib-demos to your Lab 1 folder:
 
* Copy the BUTTON program from the avrlib-demos to your Lab 1 folder:
  
                          ~> cd ~/pid2007/lab1
+
  $ cd ~/pid2008/lab1
            ~/pid2007/lab1> cp -r ../avrlib-demos/button .  
+
  $ cp -r ../avrlib-demos/button .  
            ~/pid2007/lab1> cd button  
+
  $ cd button  
  
 
* Load the program onto the AVR
 
* Load the program onto the AVR
  
      ~/pid2007/lab1/button> make load
+
  $ make load
  
* When you press SW4 ("Switch number 4"), LED0 ("Light Emitting Diode number 0") should light
+
* When you press SW3 ("Switch number 3"), LED0 ("Light Emitting Diode number 0") should light
* Look at the code in an editor:
+
* Look at the code in an editor. In Linux, you can use gedit:
  
      gedit button.c  
+
  $ gedit button.c &
  
 
* In particular, note that there's a comment near the top of the code that tells you
 
* In particular, note that there's a comment near the top of the code that tells you
Line 81: Line 99:
 
* Compile and load TOGGLE from the avrlib-demos
 
* Compile and load TOGGLE from the avrlib-demos
  
                      ~> cd ~/pid2007/lab2
+
  $ cd ~/pid2008/lab1
            ~/pid2007/lab2> cp -r ../avrlib-demos/toggle .  
+
  $ cp -r ../avrlib-demos/toggle .  
            ~/pid2007/lab2> cd toggle
+
  $ cd toggle
      ~/pid2007/lab2/toggle> make load
+
  $ make load
  
 
* Look at the code in an editor. Compare button.c and toggle.c. Why is there a debounce mechanism in toggle.c but not in button.c? How does the debounce mechanism work?
 
* Look at the code in an editor. Compare button.c and toggle.c. Why is there a debounce mechanism in toggle.c but not in button.c? How does the debounce mechanism work?
Line 90: Line 108:
 
* What happens if you change the debounce threshold to 30000?
 
* What happens if you change the debounce threshold to 30000?
 
* Can you change it to 100000? (hint - you need to change something else, too. DO NOT SPEND MORE THAN 5 MINUTES TRYING TO FIGURE THIS OUT!)
 
* Can you change it to 100000? (hint - you need to change something else, too. DO NOT SPEND MORE THAN 5 MINUTES TRYING TO FIGURE THIS OUT!)
 +
 
===Optional Section: Instruction Counting and Timing===
 
===Optional Section: Instruction Counting and Timing===
 
* Really, we mean it, don't waste time on this unless it fascinates you
 
* Really, we mean it, don't waste time on this unless it fascinates you
Line 96: Line 115:
 
** Look in the two sections <checkButton> and <main>. Each line corresponds to one instruction of the microprocessor. What debounce threshold value would give approximately 1 second of delay between the button press and the LED lighting up? (Hint: you will need to look at the Atmel Instruction Set documentation.)
 
** Look in the two sections <checkButton> and <main>. Each line corresponds to one instruction of the microprocessor. What debounce threshold value would give approximately 1 second of delay between the button press and the LED lighting up? (Hint: you will need to look at the Atmel Instruction Set documentation.)
 
** Why is using a very high debounce threshold a bad way to implement a 1 second delay? (There are at least three good reasons.)
 
** Why is using a very high debounce threshold a bad way to implement a 1 second delay? (There are at least three good reasons.)
* The best way to implement a 1 second delay is with the timer library in avrlib. Put the debounce threshold back to a reasonable value for debouncing and reintroduce the 1 second delay properly:
+
* The best way to implement a 1 second delay is with the timerx8 library in avrlib. Put the debounce threshold back to a reasonable value for debouncing and reintroduce the 1 second delay properly:
** Add <tt>#include "timer.h"</tt> at the top of toggle.c
+
** Add <tt>#include "timerx8.h"</tt> at the top of toggle.c
 
** Add <tt>timerInit();</tt> to the beginning of your <tt>main()</tt> function but after your varaible declariations..
 
** Add <tt>timerInit();</tt> to the beginning of your <tt>main()</tt> function but after your varaible declariations..
 
** Add a call to the <tt>timerPause()</tt> function in the right place in your program.
 
** Add a call to the <tt>timerPause()</tt> function in the right place in your program.
** In your makefile, add <tt>$(AVRLIB)/timer.c</tt> to the list of files in the definition of <tt>SRC</tt>
+
** In your makefile, add <tt>$(AVRLIB)/timerx8.c</tt> to the list of files in the definition of <tt>SRC</tt>
 
***The new makefile line should look like this  
 
***The new makefile line should look like this  
       SRC= $(TRG).c $(AVRLIB)/timer.c  
+
       SRC= $(TRG).c $(AVRLIB)/timerx8.c  
 
* Find the maximum delay time before you can notice a pause between the button press and the LED illuminating.
 
* Find the maximum delay time before you can notice a pause between the button press and the LED illuminating.
  
Line 115: Line 134:
 
* You should hear a beep when you press SW4.
 
* 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:
 
* 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 <tt>#include "timer.h"</tt> at the top of buttonbeep.c
+
** Add <tt>#include "timerx8.h"</tt> at the top of buttonbeep.c
 
** Add <tt>timerInit()</tt>; to the beginning of your <tt>main()</tt> function.
 
** Add <tt>timerInit()</tt>; to the beginning of your <tt>main()</tt> function.
 
** Add a call to <tt>timerPause()</tt> in the right place in your program.
 
** Add a call to <tt>timerPause()</tt> in the right place in your program.
** In your makefile, add <tt>$(AVRLIB)/timer.c</tt> to the list of files in the definition of <tt>SRC</tt>
+
** In your makefile, add <tt>$(AVRLIB)/timerx8.c</tt> to the list of files in the definition of <tt>SRC</tt>
 
** At what point does the delay become noticeable? What if the beep comes before the light?
 
** 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?
 
* Try finding the largest unnoticeable delay time as in #3 using the beep as feedback. Is it different?
Line 126: Line 145:
 
* Compile the code and program the AVR:
 
* Compile the code and program the AVR:
  
      cd ~/pid2007/lab2
+
  $ cd ~/pid2008/lab1
      cp -r ../avrlib-demos/avrsimon .  
+
  $ cp -r ../avrlib-demos/avrsimon .  
      cd avrsimon  
+
  $ cd avrsimon  
      make load
+
  $ 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.
 
* 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.
* The first sequence that is played should be random. Why isn't it? Fix it (without using a random number generator in C).
+
* The first sequence that is played should be random. Why isn't it? Fix it.
 +
** You need to use the srand() function.
 +
** You probably also need to start a timer -- look at avrlib-demos/xtra/reaction/reaction.c for an example
 
* Improve the game to make it more interesting and engaging. Here are some suggestions, but feel free to come up with others:
 
* 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.
 
** 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.
Line 140: Line 161:
 
** Add timing - Try to evaluate the user's rhythmic accuracy
 
** Add timing - Try to evaluate the user's rhythmic accuracy
 
** Change the nature of the game somehow
 
** Change the nature of the game somehow
 +
 +
<center>[[PID 2008]]</center>
 +
 +
 +
[[Category:PID_2008]][[Category:PID]]

Latest revision as of 13:28, 6 July 2008

AVR Programming
Assigned: Monday, July 7

The goal of this lab is to become familiar with the ins and outs of programming on the AVR mini.

Set up your environment to use the AVR

Follow OS specific instructions for installing the AVR environment

  • In all cases, this entails downloading the avrlib to your home directory. It is easiest to use subversion.

CCRMA Linux

  • On a CCRMA Linux machine, open a terminal
  • From the terminal type
   $ cd
   $ svn co file:///usr/ccrma/group/pid/svn/avr/avrlib ./avrlib

Mac

  • Then, open a Terminal (the Terminal app is in /Applications/Utilities) and type:
   $ cd
   $ svn co svn+ssh://ccrma-gate.stanford.edu/usr/ccrma/group/pid/svn/avr/avrlib ./avrlib   
  • You'll be prompted for your CCRMA password (maybe twice)

Create a pid2008 directory (choose a meaningful name) and copy the avrlib-demos to it

For example, if your name is Clarence, you might want to make a directory called clarence

   $ mkdir pid2008
   $ cd ~/pid2008
   $ svn co file:///usr/ccrma/group/pid/svn/avr/avrlib-demos ./avrlib-demos

As before, if you are using a Mac, replace file:// with svn+ssh://ccrma-gate.stanford.edu

Make a directory for Lab 1 in your pid2008 directory

You can do this from the Linux file browser, the Mac Finder, or from the terminal:

   $ cd ~/pid2008 
   $ mkdir lab1

Set up AVR connection to PC

  • Connect power cable, connect USB cable, and power-up the AVRmini

Test AVR setup with "flash" test code

  • Make yourself a copy of the FLASH demo from avrlib-demos:
   $ cd ~/pid2008/lab1
   $ cp -r ../avrlib-demos/flash .
   $ cd flash
  • Compile the code. Type:
   $ make
  • Download the code onto 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 PORTB header.
  • If everything is working, an LED on the AVRmini board should start flashing.

AVRMini Input/Output

Review architecture and programming, as necessary

Momentary pushbutton

  • Copy the BUTTON program from the avrlib-demos to your Lab 1 folder:
  $ cd ~/pid2008/lab1
  $ cp -r ../avrlib-demos/button . 
  $ cd button 
  • Load the program onto the AVR
  $ make load
  • When you press SW3 ("Switch number 3"), LED0 ("Light Emitting Diode number 0") should light
  • Look at the code in an editor. In Linux, you can use gedit:
  $ gedit 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.

Toggle Pushbutton Using Debounce

  • Compile and load TOGGLE from the avrlib-demos
  $ cd ~/pid2008/lab1
  $ cp -r ../avrlib-demos/toggle . 
  $ cd toggle
  $ make load
  • Look at the code in an editor. Compare button.c and toggle.c. Why is there a debounce mechanism in toggle.c but not in button.c? How does the debounce mechanism work?
  • What happens if you change the debounce threshold to 1? (hint - try toggling the LED 10 times)
  • What happens if you change the debounce threshold to 30000?
  • Can you change it to 100000? (hint - you need to change something else, too. DO NOT SPEND MORE THAN 5 MINUTES TRYING TO FIGURE THIS OUT!)

Optional Section: Instruction Counting and Timing

  • Really, we mean it, don't waste time on this unless it fascinates you
    • In the BUTTON project directory, create the file button.ss by typing make button.ss
    • Look at button.ss - these are the instructions that are actually written to the microprocessor.
    • Look in the two sections <checkButton> and <main>. Each line corresponds to one instruction of the microprocessor. What debounce threshold value would give approximately 1 second of delay between the button press and the LED lighting up? (Hint: you will need to look at the Atmel Instruction Set documentation.)
    • Why is using a very high debounce threshold a bad way to implement a 1 second delay? (There are at least three good reasons.)
  • The best way to implement a 1 second delay is with the timerx8 library in avrlib. Put the debounce threshold back to a reasonable value for debouncing and reintroduce the 1 second delay properly:
    • Add #include "timerx8.h" at the top of toggle.c
    • Add timerInit(); to the beginning of your main() function but after your varaible declariations..
    • Add a call to the timerPause() function in the right place in your program.
    • In your makefile, add $(AVRLIB)/timerx8.c to the list of files in the definition of SRC
      • The new makefile line should look like this
      SRC= $(TRG).c $(AVRLIB)/timerx8.c 
  • Find the maximum delay time before you can notice a pause between the button press and the LED illuminating.

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.

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 "timerx8.h" at the top of buttonbeep.c
    • Add timerInit(); to the beginning of your main() function.
    • Add a call to timerPause() in the right place in your program.
    • In your makefile, add $(AVRLIB)/timerx8.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?

Compile, Run and Play the AVRsimon

  • Compile the code and program the AVR:
  $ cd ~/pid2008/lab1
  $ 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.
  • The first sequence that is played should be random. Why isn't it? Fix it.
    • You need to use the srand() function.
    • You probably also need to start a timer -- look at avrlib-demos/xtra/reaction/reaction.c for an example
  • 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)
    • Add additional buttons and LEDs using the solderless breadboard
    • 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
PID 2008