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
good song, we need to do it forseveral iterations, not just once.We need to listen, try, and listenagain.The hardware for playing thissong is still the circuit shown inFigure 2, but it is more flexible. Forexample, you can use 11.0592 MHzinstead of 12 MHz, and won’t get anyunpleasing result. You may also use abuzzer to replace the speaker if spaceis limited and sound quality can betolerated.Build Your ChipMusic LibrarySo far, we‘ve discussed chipmusic composing only on the 8051,but the principles outlined here canbe easily modified and applied toother micros such as AVRs or PICs, asthey all have a timer and a similarinterrupt scheme.Once you’ve created your musicfiles, you need a device programmerto “burn” it on to a micro. For 8051-like micros, there are numerousprogrammers available on themarket, including my 8x51 Flash/EPROM programmer.With the information presentedhere, not only you can complete thecomposing of the remaining portionof The Star-Spangled Banner, butalso do much more. For example,you can compose Beethoven’s Odeto Joy. With chip prices dropping solow, it’s much easier and cheaperthan ever for chip music composing.So don’t miss this chance tobuild your own chip music library asI did.So, happy chip music composing.NVAUTHOR BIO■ G.Y. Xu is an ElectricalDesigner specializing inmicroprocessor/microcontrollersystems design and development,both in hardware and software.He can be reached by emailat gyxu@cmpmail.comCALLTOLL-FREETO ORDER(800) 292-7711Electronic Science LabMaxitronix 500-in-1 Electronic Project LabModel MX-909Everything you need to build500 exciting projects!• Learn the basics of electronics. 500 differentelectronic experiments, special lighting effects,radio transmitter and receivers, sound effects,cool games and MORE!• Includes built-in breadboard and an LCD.• Explore amplifiers, analog and digitalcircuits plus how to read schematicdiagrams.• Includes 11 parts.• Lab-style manual included.• Requires 6 “AA” batteries.MX-908 - 300-in-1 Lab $69.95 MX-906 - 130-in-1 Lab $39.95MX-907 - 200-in-1 Lab $49.95 EP-50 - 50-in-1 Lab $18.95Elenco ® Digital MultimetersModel M-1750 Model LCM-1950$24. 95• 3 3/4 Digit LCD• Freq. to 10MHz• Cap. to 200μF• AC/DC Voltage• Current up to 10A• Audible Cont.• Diode Test• Transistor Test• Overload Prot.$169Guaranteed Lowest PricesUPS SHIPPING: 48 STATES 7%(Minimum $7.00)OTHERS CALL FOR DETAILSIL Residents add 8.75% Sales TaxSEE US ON THE WEBElenco ® Snap CircuitsElenco’s new Snap Circuits make learning electronics fun and easy. Just follow the colorful pictures inour manual and build exciting projects, such as: FM radios, digital voice recorders, AM radios, burglaralarms, doorbells, and much more! You can even play electronic games with your friends. All parts aremounted on plastic modules and snap together with ease. Enjoy hours of educational fun while learningabout electronics. No tools required. Uses “AA” batteries.6 versions available.Build up to 750 projects!As low as $29.95C&S SALESwww.cs-sales.com4 Educational Models AvailableSC-750 - Extreme Version, contains over 80 parts to build over 750 experiments. Includes everything from SC-500plus experiments in solar, electromagnetism, vibration switches, and 70 computer interfaced experiments.SC-500 - Pro Version, contains over 75 parts including voice recording IC, FM radio module, analog meter,transformer, relay, and 7-segment LED display. Build over 500 experiments.SC-300 -Deluxe Version, contains over 60 parts. Build over 300 experiments.SC-100 - Snap Circuits, Jr., contains over 30 parts. Build over 100 experiments.$59. 95• Large 1” 3 3/4Digit LCD• Autoranging Freq.to 4MHz• Cap. to 400μF• Inductance to 40H• Res. to 4,000MΩ• Logic Test• Diode &Transistor Test• Audible ContinuityTestQuantity Discounts AvailableElenco ® Educational KitsModel RCC-7KRadio Control Car KitModel AM-780KTwo IC Radio KitModel M-1006KDMM Kit$27. 95• 18 Ranges• 3 1/2 DigitLCD• TransistorTest• 7 Functions$9. 95• Diode Test• Transmitter includedNon-soldering Version AM/FM VersionAK-870 $27.95 AM/FM-108K $29.95$18. 95Visit our website www.cs-sales.com to see our assortment.Elenco ® Quad Power SupplyModel XP-5814 Fully Regulated Power Supplies in 1 Unit$754 DC Voltages: 3 fixed; +5V @ 3A, +12V@ 1A, 1 variable; 2.5 - 20V @ 2A • Fullyregulated & short protected • Voltage ¤t meters • All metal caseElenco ® Oscilloscopes*Special*$329S1325 30MHzProbes includedS1330 30MHz Delayed Sweep $459S1340 40MHz Dual Trace $469S1345 40MHz Delayed Sweep $549S1360 60MHz Delayed Sweep $749S1390 100MHz Delayed Sweep $999C&S SALES, INC.150 CARPENTER AVE • WHEELING, IL 60090(847) 541-0710 FAX: (847) 541-9904E-mail: sales@cs-sales.comwww.cs-sales.comCALL ORE-MAIL USFOR OURFREECATALOG!GreatGiftIdea!CreateYour OwnExcitingExperimentsUpgrades and replacementparts available at:www.cs-sales.com$99$119.95$89.95$59.95$29.95Elenco ® Test EquipmentElenco ® Frequency Counter1MHz-3GHz Model F-2800• 10 digit LCDdisplay• 16-segment RSSIbargraph• Resolution to 1Hz• Hi-speed(300MHz) directcount• Includes NiCdcharger andantennaElenco ® 5MHz SweepFunction Generatorwith built-in 60MHzFrequency CounterModel GF-8056$229Generates square, triangle, and sinewaveforms, and TTL, CMOS pulse.15 DAY MONEYBACK GUARANTEE2 YEAR FACTORYWARRANTYPRICES SUBJECTTO CHANGEWITHOUT NOTICEMay 2006 45