Add harmony to your system
Eugene O -- EDN, 7/20/2000
Have you ever wished you could distinguish one device's operating state from another's by the sounds they make or that error states would sound harsh while normal operations would sound harmonious? By combining the NCO technique from a previous Design Idea (Reference 1) with digital mixing, you can obtain musical chords or intervals with a minimal amount of hardware and software. Any µC system can thus produce a variety of sounds. In Figure 1, a piezoelectric speaker, Radio Shack Model 273-091, and two 270W resistors transduce a pulse stream from a µC. Differential drive to this transducer increases the volume by doubling the effective voltage. The capacitance of the piezoelectric speaker reacts with the 270W resistors to integrate and smooth the pulse stream. The software (Listing 1), a tight loop, comprises a square-wave generator and an NCO's (numerically controlled oscillator's) summing part. After it sets up some registers, the sound-generating loop establishes an output level for each of two or more square waves.The pseudocode example in Listing 1 demonstrates a two-note generator in which the output levels of two square waves are established in registers r_vol1 and r_vol2. The frequency of each square wave is a function of the values set for variables first_note and second_note and by the cycle rate of the loop. Note that a half-cycle of a square wave concludes when the corresponding counter register (r_cnt1 or r_cnt2) reaches zero. The summing part of the loop uses the NCO technique to generate an output level for the digital mixture of the square waves generated in the first part of the loop. The mixing of two square waves with frequencies of 880 Hz, the C above middle C, and 988 Hz, the second D above middle C, in the equal-tempered scale produces the oscilloscope waveform in Figure 2. An AVR AT90S Series µC runs through the sound-generating loop in 18 clock cycles. Thus, to produce an 880-Hz square wave with this type of µC running at 8 MHz, the first_note or second_note value is set at 253, which is equal to 8 MHz188802.
A note of caution is in order in calculating this value in an assembly program: Be careful of truncation issues; the musical intervals sound wrong if the frequencies are off. Also, when you use another type of µC, you must adjust the loop software to compensate for differences between the µC's instruction timing and the assumed instruction timing of the pseudocode. All instructions should complete in one clock cycle except for jump or branch executions, which take two clock cycles. You can create chords by expanding the software loop to include a third square-wave generator and modifying the mixer to add in the third note. A by-product of this sound-generation scheme is that it lets you control volume by changing the volume variable in Listing 1. The loudest possible volume occurs when the volume variable equals 80hex for a two-note generator. Setting the volume variable lower than 80hex reduces the voltage output from each square wave. With this scheme, you can produce 10 distinct volume levels, using hex values 80, 6A, 60, 58, 50, 48, 40, 38, 30, and 28. Click here to download Listing 1. (DI #2561)
REFERENCE
1. Ploss, Steve, "NCO technique helps µC produce clean analog signals," EDN, April 15, 1999, pg 129.
















