Microcontroller Timing

S. Mann

In this lab, we will learn to program an AVR to make some lights flash. In particular, we will flash some LEDs with specific timing considerations, to make a fun toy that helps children learn musical rhythm and tempo.

Connect 8 switches (or an 8 position DIP switch) to PORT B. These switches will select the kind of game to be played.

Connected 8 LEDs to PORT D. These 8 LEDs will count through musical measures, assuming that there are 8 or fewer measures per phrase (for phrases longer than 8 measures, you can use more LEDs, e.g. you could, for example, use 12 or 16 LEDs).

PART 1:

Write a program to flash the LEDs at a steady tempo as follows:
. . . . . . . X
. . . . . . . x
. . . . . . . x
X . . . . . . .
x . . . . . . .
x . . . . . . .
. X . . . . . .
. x . . . . . .
. x . . . . . .
. . X . . . . .
. . x . . . . .
. . x . . . . .
. . . X . . . .
. . . x . . . .
. . . x . . . .
. . . . X . . .
. . . . x . . .
. . . . x . . .
. . . . . X . .
. . . . . x . .
. . . . . x . .
. . . . . . X .
. . . . . . x .
. . . . . . x .
. . . . . . . X (same as first row, repeats, infinite loop, as per above)
Where "X" is a bright flash of light, and "x" is a dim flash of light.

Note that each flash of light should be brief enough that there is visible pulsation during two or more successive flashes of the same brightness.

This particular configuration will teach timing to popular children's songs that have 3 beats per measure, and 8 measures per phrase.

Try, for example, singing, or playing (on whatever musical instrument you might have) a 3-beat song, such as, for example, House of the Rising Sun (lyrics depicted below).

. . . . . . . x   there
X . . . . . . .   IS
x . . . . . . .   o
x . . . . . . .   a
. X . . . . . .   HOUSE
. x . . . . . .   o
. x . . . . . .   in
. . X . . . . .   NEW
. . x . . . . .   o
. . x . . . . .   or
. . . X . . . .   LEANS
. . . x . . . .   o
. . . x . . . .   they
. . . . X . . .   CALL
. . . . x . . .   o
. . . . x . . .   the
. . . . . X . .   RIS
. . . . . x . .   o
. . . . . x . .   ing
. . . . . . X .   SUN
. . . . . . x .   o
. . . . . . x .   o
. . . . . . . X   0
. . . . . . . x   o
. . . . . . . x   it's
X . . . . . . .   BEEN
x . . . . . . .   o
x . . . . . . .   the
. X . . . . . .   RU
. x . . . . . .   in
. x . . . . . .   of
. . X . . . . .   MAN
. . x . . . . .   y
. . x . . . . .   a
. . . X . . . .   GIRL
. . . x . . . .   o
. . . x . . . .   and
. . . . X . . .   GOD
. . . . x . . .   i
. . . . x . . .   kn
. . . . . X . .   OW
. . . . . x . .   o
. . . . . x . .   i'm
. . . . . . X .   ONE
. . . . . . x .   o
. . . . . . x .   o
. . . . . . . X   0
. . . . . . . x   o
Now implement some other timing patterns, such as for songs with 2, 4, 6, or 8 beats per measure.

Additional example:

Twinkle Twinkle Little Star: 2 beats per measure, 8 measures per phrase.
1  TWIN
   kle
2  TWIN
   kle
3  LIT
   tle
4  STAR
   o
5  HOW
   i
6  WON
   der
7  WHAT
   you
8  ARE
   o
1  UP
   a
2  BOVE
   the
3  WORLD
   so
4  HIGH
   o
5  LIKE
   a
6  DIA
   mond
7  IN
   the
8  SKY
   o
1  TWIN
   kle
2  TWIN
   kle
3  LIT
   tle
4  STAR
   o
5  HOW
   i
6  WON
   der
7  WHAT
   you
8  ARE
   o

PART 2:

Use a ninth output of the AVR to connect to an audio device such as a surplus loudspaker. Use short pulses for quiet clicks and longer pulses for loud clicks, so that there is audio to go with the flashing lights, i.e. whenever there is "x" there is a quiet click, and whenever there is "X" there is a loud click.

PART 3:

Implement a tempo adjustment using one of the analog inputs on the AVR.