13.07.2015 Views

Nuts & Volts

Nuts & Volts

Nuts & Volts

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

REFERENCES■ [1] John Backus: The AcousticalFoundations of Music, 1977.■ [2] Scientific American’sReading Series: The Physics ofMusic, 1978.■ [3] G.Y. Xu: 8x51 Flash/EPROMMicrocontroller Programmer,Circuit Cellar Magazine, April1998.■ [4] G.Y. Xu: Play the AVRHyperTerm, <strong>Nuts</strong> & <strong>Volts</strong>Magazine, February 2005.[3], I’ve found that its tone matchesmany fine-tuned Steinway andYamaha pianos. The frequencymeter or oscilloscope measurementshows 440.0 Hz. This circuit canbe built and mounted in a smallbox and act like a “Tuning fork” formusicians.Now, Let’s ComposeJust as the words saying “Whenyou know the notes to sing, you cansing (al)most anything.” Chip musiccomposing is no difference. Simplyput, we need to create the subroutinesfor each note, then call theseroutines to make a song. Listing 2 isan example showing how to composethe beginning melody of TheStar-Spangled Banner (Listing 2 isalso available on the <strong>Nuts</strong> & <strong>Volts</strong>website).It utilizes only four differentnotes, but we’ve created eight noteroutines for your conveniencefor future use. Each note routinedeals with two parameters: thefrequency and the duration of thenote.Using 8051’s Timer0 interrupt isthe main reason for creating eachnote routine. As we see, the 8051works in mode 3, where Timer0 acts astwo separate eight-bit counters TL0and TH0. If the register TL0 is loadedwith number 0 to start, it will count upone each microsecond, and overflowafter 256 μS.The necessary steps to enableTimer0 interrupt and start it areshown at the beginning part of themain program. After that, an infiniteloop is entered to generate the beginningmelody of The Star-SpangledBanner.The principle of frequencygeneration is the same as on “A”Tone, but the technique is different.Here we deal with a number ofdifferent frequencies, not just onelike 440 Hz or T/2 = 1,136 μS, and weneed to keep the Timer0 interruptservice routine the same for allthese frequencies.A simple solution is to set upthe Timer0 so that it alwaysoverflows every 8 μS, then calculatehow many timer Ticks are needed forT/2 of any frequency we are dealingwith.When counting elapsed timebetween timer ticks, the timeit takes to execute the interruptservice routine, that is, the InterruptExecution Time (IET) must be takeninto account. As calculated inListing 2, IET=7 μS/INT, so theelapsed time between two ticks isfixed 8+7=15 μS.Under this scheme the number ofticks for some frequencies may not bean integer and need to be rounded, insuch case the calculation can only beapproximate. But 8 μS is very smallcompared to any T/2 we can have,so the created note would still besatisfactory.Now, let’s look at an examplefrom the note subroutine: Howmany ticks are needed to generatethe 523 Hz (T/2=956 μS) tone.Since 956/15 = 63.73, we round itup to 64. But in the notesubroutine the tick starts from 0, soit should take 64-1 = 63 as therequired ticks. By the way, we haveused the note name “Doe” inparallel with “C5” for it; this is helpfulin composing.As for the second parameter, theduration of the note is decided by thenumber of repeat times Rp for asquare wave. Roughly speaking, wecan simply assign a fixed numbersuch as Rp = 250 to every noteroutine. It works, and I did it in mybeginning compositions. But this waycan’t achieve equal duration for everynote. The result is the lower thefrequency (with larger T/2), the longerthe note duration.A better way to achieve equalnote duration is to start from thehighest frequency (shortest T/2),assign the largest Rp=255, thencalculate the note duration. Forinstance, in the “C6” notesubroutine, the highest frequency is1,047 Hz, T/2 = 478 μS, if Rp=255is assigned to it, then the noteduration will beRp * T = 255 * 478 * 2 = 243780 μS,or roughly 1/4 second.After that, we use this formula toget the required Rp for other lowerfrequencies. For instance, in the “C5”note subroutine we getRp = 243780 / T = 243780 / (2*956) =127.5 => 128By doing so for all other notesubroutines, we keep each noteduration almost equal to 1/4 second.And we can think of each subroutinecall as a “quarter note.” This is veryhelpful when composing; you canestimate the required number ofcalls for the notes you are going toplay.Last, but not least, we needsome delay routines for REST notecomposing. For example, we alreadyprovide 10 milliseconds (ms) and100 ms delay routines. From there,you can create the “quarter rest”note routine, if needed. Just remember:“half time of all music issilence.”Now that we’ve created notesubroutines, composing The Star-Spangled Banner is just a matterof calling the required notes intothe main program to constructthe melody, as shown in Listing 2.Of course, in order to make a44 May 2006

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!