Title: Frequency and Sound
1Chapter 8
- Frequency and Sound
- only covers through Twinkle Twinkle Little Star
- (opt)can skip to boe bot at this point
2Electric Beeps
- From your alarm clock to microwave to automobiles
and ATM machines your day is full of devices
sounding beeps to alert you or indicate actions
to be taken. - Microcontrollers produce sounds by sending
high/low signals very quickly to a speaker. The
rate at which the signal repeats is called
frequency and is measured in cycles per second or
Hertz (Hz) which produce the desired tone or
pitch.
3How conventional speakers work
- http//electronics.howstuffworks.com/speaker6.htm
- See function generator and speaker demonstration
4Piezoelectric Speaker
- The piezoelectric speaker is a common, small and
inexpensive speaker used in many devices though
it lacks in audio quality.
5How Piezoelectric Materials Work
- In a crystal microphone, air pressure deforms the
crystal enough to cause very small voltage
changes in the crystal. These voltage changes are
amplified and used to record or transmit sounds. - Piezoelectric materials also work the other way.
If you apply a voltage across the crystal, the
crystal will change shape. The change is very
slight in most cases, but it is enough to drive
small speakers.
6ACTIVITY 1 Building and Testing the Speaker
Build the circuit below
7Enter, Run, and Save the code below
- The FREQOUT command sends high/low signals to the
specified pin at the frequency and for the
duration defined.FREQOUT Pin, Duration, Freq1,
Freq2 - To play a note at 2000Hz which lasts 1.5 seconds
(1500ms)
8Your turnadjusting frequency and duration
- Save the program under a new name
- Try some different values for Duration and Freq1
- Notice as the frequency gets higher so does the
pitch observed
9Some interesting sounds can be done with for next
loops
- Run RAY GUN on the next slide
10Use the Reset button for your trigger
11Enter, Run, and Save Actiontones.bs2 on the next
slide (page 222 of your text)
12(No Transcript)
13- In ActionTones.bs2 a variety of tones are played.
Alarm and Robot Reply are a sequence of tones
sent to the speaker. - In Hyperspace, a nested loop is used where
FREQOUT cycles through durations from 15 to 1.
For each duration it cycles through frequencies
from 2000 to 2500 in increments of 20.
14Nested Loops
Inner Loop
Outer Loop
The inner loop is performed fully
everyrepetition of the outer loop.
15What follows are some sound effects that are
based on nested loops
16Enter, Run, and Save the code below (PROGRAM 3)
- Use your eyes and ears to verify that
- Duration changes each pass through the outer loop
- Frequency changes each pass through the inner loop
17Enter, Run, and Save the code below under the
name actiontonesyourturn.bs2
18Beats
- Produced by two slightly different frequencies
played at the same time - Beat Frequency difference between the
frequencies - http//www.walter-fendt.de/ph14e/beats.htm
19Two Frequencies at Once
- The FREQOUT command has an optional parameter
called Freq2. This allows playing 2 frequencies
simultaneously. - At times the frequencies will be in phase and at
other times be out of phase creating a beat
frequency. This rate of being in then out of
phase difference in frequencies - FREQOUT 9, 5000, 2000, 2005
20Enter, Run, and save mixingtones.bs2 below
(page 226 of your text)
- Note a beat frequency is the rate at which the
two frequencies fade in and out - Beat frequency difference in frequencies
- BF 2 Hz produced by mixing 2000 Hz with 2002 Hz
- BF 4 Hz produced by mixing 2000 Hz with 2004 Hz
21This code is inefficient and we can do better
- Modify the code, so the same task is done using a
word variable and a loop - MY SOLUTION IS ON THE NEXT SLIDE
22(No Transcript)
23ACTIVITY 3 Musical Notes and Simple Songs
- Each key on a piano is a specific frequency
corresponding to a note. There are 12 groups of
notes, each at a higher octave. An octave is a
doubling of frequency, so C7 is double the
frequency of C6. C8 2 x C7etc
24DO RE ME FA SO LA TI DO (C6) (D6) (E6) (F6) (G6) (
A6) (B6) (C7) THIS IS DONE BY PLAYING JUST THE
WHITE OR NATUARL KEYS
25- The black keys are called sharps or flats
- C6 D6b
- A6 B6b
26See if you can make your microcontroller play do
re me fa so la ti dowith a 500 ms pause between
each note. Save your code as doremefasolatido.bs2
- Use page 229 for help if you like
27Try some modifications
- Try Including the sharp and flat notes in your
song - Next play the song at the next octave up
- Freq2 might save you some time
28Storing and Retrieving Data
- The DATA command allows us to save musical or any
other type of data in the bs2 EEPROM. - The syntax is as follows
- Symbol DATA Word DataItem1,DataItem2,
- For exampleNotes DATA "C","C","G","G","A","A","G
"Stores the characters in EEPROM, with the 1st
location called Notes. Each subsequent address
is Notes an index value.0,1,2,3 etc
29Notes DATA "C","C","G","G","A","A","G"
- The first letter C is at notes 0
- The second letter C is at notes 1
- G is at notes 2
- etc
Note When storing letters, you must place
quotations around the letters. This is not
necessary for numbers
30Using memory map
- Each location on the memory map can store one
byte (8 bits) (8 digits) so up to a decimal
value of 255 - Two bytes would be a word value (256 to 65535)
and would take up two locations on the memory map - The memory map uses a grid in hex to mark the
location of each of these registers - Lets enter some numbers in the eeprom with the
data command. - Note that when entering word values, two
registers are taken instead of just one - See next slide
31- Notice only five registers in EEPROM are taken
up.starting with register 0
32- However, here 6 registers in EEPROM are taken
- NOTE you must tell eeprom if you plan on
storing a value greater than 255 by stating that
this data piece is a word
33Pulling the stored data from the EEPROM with
READ
- Lets say we want to pull the data out of eeprom
that we stored in it with - Notes DATA "C","C","G","G","A","A","G"
- SYNTAX
- READ Notes 6,noteletter
- This means find the data stored in the 6 th
register of EEPROM and store it under the
variable noteletter - If you like, you can now debug the value of
noteletter to see if it is correct - Try this71 is ascii for the letter G
34Reading words from EEPROM
- Remember, data stored as word variables take up
two registers in eeprom - Lets say we have
- Numbers DATA word 2000, word 3000
- Now lets display the second stored number in the
debug terminal - READ numbers 2,Word value
- DEBUG ?value
- ..2000 takes up registers 0 and 13000 takes up
registers 2 and 3..also notice the presence of
word in front of value!!!!
35Store a song in memory
- Use two Data commands to store
- The letters in doremefasolatido
- The frequencies in doremefasolatido
- Your program should
- Display the noteletters and the frequencies in
the debug terminal while the song is played
36Enter, Run, and Save the code on the next slide
(page 231 of your text)
37TwinkleTwinkle.bs2 Abbreviated version
When index 0
38- Verify that the notes sound like the song Twinkle
Twinkle Little Star - Use the Debug terminal to verify that it works as
expected by accessing and displaying values from
the three DATA directives
39- Now modify the song, so it plays the second part
of the tune - As in the first phrase the last note is held
twice as long as the others - You will need to expand each data directive
- You will need to change the FORNEXT loop so it
goes from 0 to 13 instead of 0 to 6. - TRY IT!!
40A HELPFUL WEB SITE FOR THE PROJECTS THAT FOLLOW
- THE ONLINE KEYBOARD
- http//www.pianoworld.com/fun/javapiano/javapiano.
htm