ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 1681

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Chapter 6: Recording Frequency Data · Page 111

The Output

Figure 6-5 shows the Stamp-O-Scope 2 display. You can adjust the potentiometers and change the frequency characteristics of the waveform. For example, pot B directly affects the amount of time the signal is low. It also indirectly affects the amount of time the signal is high. Try adjusting pot B until the low signal is 4 asterisks wide. Then, you can adjust pot A so that the pulse-width varies. Try adjusting A so that the pulse-width is also 5-asterisks wide. Toggle the Pause/Resume button on the Debug Terminal by clicking on it with your mouse to freeze and unfreeze the display.

Figure 6-5: Output Sample of 555 Timer Pulse Train

This is a time-varying display of the pulse train generated by the 555 timer as measured by pin P0. Again, the display has been turned sideways to match conventional displays and graphs.

Page 112 · Basic Analog and Digital

Now, try adjusting pot B and make the low signal two asterisks wide. What happened to the pulse width? It should also have gotten narrower too.

Next, try adjusting pot B as far as it will go to increase the frequency (making the pulse widths narrower). The piezoelectric speaker should be clicking pretty fast now. Carefully adjust pot A and you should be able to hear an audible tone out of the piezo speaker. It's not a pretty tone; it sounds about like a kazoo.

The Stamp-O-Scope 2 is probably not working very well any more, as shown in Figure 6- 6. We'll replace it with another program that lends itself to measuring audible sound.

Figure 6-6: Output Sample of Faster Pulse Train

Chapter 6: Recording Frequency Data · Page 113

The pulse train is now repeating itself several times as fast as the sampling rate. So the Stamp-O-Scope 2 display is no longer valid.

Now back way off on pot B so that all you get is a periodic clicking out of the piezoelectric speaker. We'll want to crank pot B back up in a little while to get that tone again, but first, let's consider what happened to the Stamp-O-Scope 2 display as the frequency of the 555 Timer was increased.

The display shown in Figure 6-6 still showed signal activity, even though it was erroneous. This is called aliasing. Aliasing happens when you don't sample something fast enough to get a true representation of what's going on. Aliasing can cause problems because sometimes the signal that gets displayed actually looks valid.

For the most part, the Debug Terminal is the limiting factor for sampling rate. So let's make a program that allows the BASIC Stamp take turns between working at full speed and sending messages to the Debug Terminal.

When is aliasing a concern? A recommended sampling rate depends on the characteristics of the signal being measured. Some signals are sampled just a few times per cycle while others are sampled thousands of times per cycle.

The absolute minimum sampling rate, in theory, that can be used to gather valid signal data is twice the frequency being sampled. When the sampling rate is less than twice the frequency of the signal being sampled, aliasing is guaranteed. This minimum frequency is called the Nyquist rate.

A BASIC Stamp I/O pin can be monitored to check the number of threshold voltage crossings per time interval. The BASIC Stamp can be programmed to track these changes once every two microseconds. The period of the sample is 2 microseconds.

From Chapter #5, we know that frequency is given by the inverse of the period:

f = 1 / T

So, the sampling rate is:

1 ÷ 2 x 10-6 seconds = 250 x 103 = 500 kHz

Since the sampling rate is 500 kHz, the theoretical maximum frequency we could sample is 250 kHz. The audible sound range is between 20 Hz and 20 kHz, and the sounds we will work with in this experiment will vary between 50 Hz and 3.5 kHz. Clearly, aliasing is not a concern.


Page 114 · Basic Analog and Digital

Program It

Let's make a program that counts how many times the pulse train repeats itself. In effect, we will make a program that determines the frequency of the pulse train. The BASIC Stamp has a built in feature for counting frequency, and the PBASIC command is the count command.

The BASIC Stamp increments the counter when the input voltage passes the 1.4 volt I/O pin threshold twice. This makes programming and interpreting the frequency data easy for periodic signals such as the pulse train, triangle wave and sine wave. Figure 6-7 shows why there are two threshold crossings per repetition of the waveform. When a waveform repeats itself, it's called a cycle. With a 5 volt amplitude and a 0 volt DC offset. Notice how the waveform crosses the threshold voltage twice per cycle.

1 full cycle

5

Volts

Figure 6-7

1.4

Volts

Full Cycle of

a Pulse Train

0

Volts

Two crossings of the threshold voltage

Enter Program Listing 6.2 into the BASIC Stamp Editor and save it as P6_2R0.bs2.

'Basic Analog and Digital - PL6_2R0.bs2

'Frequency Meter

'{$STAMP BS2}

'{$PBASIC 2.5}

f VAR Word

DO

COUNT 0, 1000, f

DEBUG HOME, "Frequency: ", DEC4 f, " Hz.", CR, CR

LOOP

When you run the program, and adjust pot B to its maximum, the output should be somewhere in the neighborhood of the frequency shown in Figure 6-8.

Chapter 6: Recording Frequency Data · Page 115

Figure 6-8

Debug Output for

Program Listing 6.2

Remember that you can turn the sound (noise, racket, whatever you want to call it by now) off by shorting pin 5 on the 555 timer to Vss. When you want the sound back on, just disconnect the wire from the Vss terminal again.

Modify the test circuit by replacing pot A with two 2 k resistors in series, and replace the 10 µF capacitor with the 0.1 µF capacitor from your parts kit. Figure 6-9 shows how the circuit should look when the changes to the circuit have been made.

Page 116 · Basic Analog and Digital

Vdd

2 kΩ

2 kΩ 7

8

4

100 kΩ

6

555 Timer

3

POT B

2

X

1

nc

0.1 µF

Vss

Figure 6-9

Astable Multivibrator

P0 Circuit

With fixed RA=4 k . RB is can still be adjusted with pot B.

Adjusting pot B, you should now be able to measure sound on a frequency range from about 60 Hz to about 3.5 kHz. You can log frequency data as you adjust the pot with the revised version of Program Listing 6.2 below.

'Basic Analog and Digital - PL6_2R1.bs2

'Frequency Meter

'{$STAMP BS2}

'{$PBASIC 2.5}

f

VAR

Word(10)

n

VAR

Nib

DO

DEBUG

CLS

FOR n

=

0

TO 9

COUNT

0, 1000, f(n)

DEBUG

HOME, "Frequency: ", DEC4 f, " Hz.", CR, CR

NEXT

PAUSE

1000

FOR n

=

0

TO 9

DEBUG

"Frequency at ", DEC1 n

DEBUG

"

seconds was ", DEC4 f(n), " Hz.", CR

NEXT

PAUSE

5000

LOOP


Chapter 6: Recording Frequency Data · Page 117

During the time this program records the frequency, the Debug Terminal display is similar to the previous revision of the program. When the program is done recording, it displays ten frequency samples. The Debug Terminal data shown in Figure 6-10 was generated by adjusting the pot back and forth to some random frequencies (tones).

Figure 6-10

Debug Output for

Program Listing 6.2,

Revision 1.

About the Code

Program Listing 6.2 Revision 2 introduces the concept of data storage using an array. We used the command:

f

VAR

Word(10)

to reserve 10 words of space in the BASIC Stamp's RAM. There's a word labeled f(0), another word labeled f(1), and so on through f(9). Each of these words is stored adjacent to each other in the BASIC Stamp's RAM.

Page 118 · Basic Analog and Digital

As the value of n is incremented in the FOR…NEXT loop, so is the index of the array. The first time through the array, a value is loaded into word f(0), the second time through the array, a value is loaded into word f(1), and so on through word f(9).

FOR n = 0 TO 9

COUNT 0, 1000, f(n)

DEBUG HOME, "Frequency: ", DEC4 f, " Hz.", CR, CR

NEXT

The same concept worked for printing the values back to the Debug Terminal.

FOR n = 0 TO 9

DEBUG "Frequency at ", DEC1 n

DEBUG " seconds was ", DEC4 f(n), " Hz.", CR

NEXT

The time varying waveforms we've looked at in this chapter and in Chapter #5 have been periodic. That is, they've repeated themselves. In the next two experiments, we'll take a look at waveforms that vary with time, but they aren't necessarily periodic.

Chapter 6: Recording Frequency Data · Page 119

What have I learned?

On the lines below, insert the appropriate words from the list on the left.

sampling

Sounds can be characterized by _____________ waveforms. The

frequency range for audible sound is between 20 and 20,000 Hz. When a

rate

time varying waveform such as a pulse train of sufficient frequency and

pulse train

amplitude is sent to a speaker, the speaker emits audible sound.

The 555 timer in an astable multivibrator circuit can be adjusted to

time-

generate a _____________ .

The frequency, pulse width, and

varying

_____________ can be adjusted by changing the values of the passive

amount

elements in the circuit, i.e. the capacitor, and the two resistors.

The pulse width is the _____________ of time a signal remains high, and

array

the duty cycle is the ratio of the pulse width to the _____________ of the

variable

waveform.

incremented

Aliasing is a phenomenon that occurs when the _____________ is less

period

than twice the frequency of the signal being sampled. This can cause

significant misinterpretations of the signal data.

duty cycle

Data can be conveniently stored and accessed in RAM using an

_____________ . The index of an array variable can be _____________

using a FOR…NEXT loop. When you specify an array of 10 bytes, they are

indexed as bytes 0 through 9.


Page 120 · Basic Analog and Digital

Questions

1.Given four different frequencies of sound, 3.5 Hz, 350 Hz, 3,500 Hz, 35,000 Hz, which ones can you hear and which ones can't you hear? Explain your answers. Also, compare the pitch of the sounds you can hear.

2.Explain aliasing. If a signal is sampled at 1 kHz, how low can the signal's frequency get before aliasing is guaranteed?

3.For the circuit in Figure 6-8, what resistance value does the pot have to be to make the astable multivibrator circuit generate a 2 kHz signal? This requires some algebra.

4.Construct a PBASIC command to declare an array of 5 nibbles. Make a command that sets the fist nibble in the array equal to the fifth nibble in the array.

Challenge!

1.In Program Listing 6.2 Revision 2, the frequency samples we took were once every 1-second because the count command counted for a second. Revise the program listing to count the frequency samples for ½ of a second. Make sure your output data is correct by comparing output of the revised program to output of the original for a known frequency. Fix any bugs

2.Design a program to record and play back tones. Make it so that the recorded tone is played on one speaker, and the playback tone is sent to a second speaker. Add a function to the program that shuts the 555 timer off by sending a low signal to pin-5 on the timer when you want to hear the playback signal. Hint: You can combine two of one of the program listings from Chapter #5 with a program listing from this experiment, and you'll be almost done. All you'll have to do is program the control signal that disables the 555 timer.

3.Design and build the circuit to implement the design from Challenge 2. You'll need to be creative about finding enough space on the breadboard.

4.Add two pushbuttons to the system developed in Challenge 2 and 3 so that you can control when to record and play back single sound samples. Now you'll need to be really creative about finding enough space on the breadboard. Use 10 k


Chapter 6: Recording Frequency Data · Page 121

resistors with the pushbuttons, and design them to send either high or low signals. Hint: Review Chapter #1 for circuit and program techniques for implementing the pushbuttons.

Why did I learn it?

Digital processing of audio signals is still expanding in the audio industry. New, more efficient ways of storing and transmitting sound are being developed so that you can transmit music more quickly over the internet and so that your voice sounds more like your voice to the person on the other end of the phone line.

Digital signal processing is also the crux of the telecommunications industry with cellular phones and normal telephones. When you talk on the phone, your voice signal is converted to 0s and 1s and sent via the telephone network in binary format. Your telephone still sends and receives analog voice signals, but your voice information is A/D and D/A converted at various points in the telephone network.

How can I apply this?

Frequency is an effective way to think about sound, but there is a myriad of other uses for frequency counting. For example, engine speed and car speed are determined by how fast certain moving parts on the car turn. When something rotates, each time it turns full circle it has repeated itself. If a shaft is rotating at 100 turns per second, its frequency of rotation is 100 Hz. Machine vibration can also be measured in terms of frequency, and so can many metabolic signals such as breathing, heart rate, and so on.

Chapter 7: Digital to Analog the Easy Way using PWM· Page 123

Chapter #7: Digital to Analog the Easy Way using PWM

A resistor, capacitor, BASIC Stamp, and a single line of PBASIC code. That's all it takes to build a D/A converter with 8-bit resolution. Sound too good to be true? In a sense it is, because a buffer is necessary for this circuit to maintain a given voltage. Even so, it's easy to build and offers a higher degree of accuracy than its resistive ladder counterpart. Figure 7-1 shows the circuit that's used - it's a simple RC circuit. The input receives a pulse width modulated (PWM) signal, and the output rises or falls to the desired voltage level.

PWM

D/A

Input

Output

P8

10 kΩ

1 µF

Vss

Figure 7-1

RC Circuit

Which can be connected to the BASIC Stamp for D/A conversion.

How can sending pulses of varied width and frequency control or set a voltage level at the D/A output? The answer is the RC circuit shown in Figure 7-1. This circuit behaves like a rechargeable battery. Figure 7-2 shows an example of the simplest kind of capacitor, the parallel plate capacitor. The charges migrate from the battery terminals and accumulate on the two metal plates until the voltage across the capacitor is the essentially the same as the battery voltage.

Page 124 · Basic Analog and Digital

+

_

Figure 7-2

Parallel Plate Capacitor Charged by a Battery

The opposed charges congregate at their respective plates. They want to get across the gap between the two metal plates, but there is an insulating material called a dielectric in-between the plates, so they can't get across.

You could also disconnect the battery from the capacitor, and it would maintain its voltage. Typically, there is a small amount of current that does make it through the dielectric material that separates the plates. It's called leakage current. Leakage current on a charged capacitor will cause the voltage to slowly dissipate.

The RC circuit shown can charge to within 1% of 5 volts very quickly. Assume that a single pulse is applied to the circuit in Figure 7-1 instead of the many pulses sent via a PWM signal. If the values of the components are exact, it would take about 6.93 milliseconds for the capacitor to charge up to 2.5 volts, and would take 46.1 milliseconds for the output to get all the way to 4.95 volts, which is 99% of the full 5 volts.

For advanced math enthusiasts, let's take a look at the proof. The modeling equation for the RC circuit from Figure 7-1 responding to a pulse that goes from 0 to 5 volts is:

V

= V

− t

1

− e R×C

Output

Input

The modeling equation is rearranged to isolate the exponential term:

1 −

VOutput

= e

− t

R×C

V

Input

The natural log of both sides is taken to remove the exponential term, and the terms are rearranged: