S
S
SergeyGrigorev2013-10-24 11:27:58
Do it yourself
SergeyGrigorev, 2013-10-24 11:27:58

How can a microcontroller output sound through PWM and timers?

I still can’t figure out how to generate music sound with a microcontroller. I figured out the PWM and timers in the msp430, but I still can’t figure out the audio output, instead of the sound music , only some squeaks are output. There are ready-made examples in C on the Internet, but in most cases without the use of timers, and on AVR microcontrollers, and I have msp430. To port, you first need to figure out how it should work. Please tell us briefly how to calculate the correct division of the timer, control the volume, and output various notes. My microcontroller speed is within 1 MHz - 16 MHz, there are 2 16-bit timers

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Ocelot, 2013-10-24
@SergeyGrigorev

Let's say you need to generate some sample - a piece of a sound wave. It doesn't matter what it will be: a sine or something more complicated.
1) Divide this sample into samples . Each reading is an instantaneous value of the signal level (voltage on the speaker winding). The sample rate - the sampling rate - must be at least twice the maximum frequency of the reproduced sound. We need a voice (range from 300 to 3800 Hz) - we take a sampling frequency of 7.6 kHz. We need music - even higher.
Sample values ​​can be stored as arrays or generated on the fly.
2) Each count has its own PWM duty cycle. Accordingly, at a sampling rate of 7.6 kHz, you need to set a new duty cycle 7600 times per second.
3) What is the PWM frequency? Several carrier periods must fit between adjacent samples. More is better. How much depends on the required noise level, analog filters at the output, etc. (there is a rather severe matan). For simplicity, let's take the PWM frequency 10 times higher than the sampling frequency - 76 kHz.
4) What should be the PWM capacity? Depends on the required dynamic range. 8-bit PWM will give a ratio of loudest to quietest signal of 256:1 (~24 dB). 16-bit - 65536:1 (~48 dB).
5) What is the final clock frequency? F = (PWM frequency) * 2 (PWM capacity) . For our case with 8-bit PWM - almost 20 MHz. If you need phase-correct PWM, it's twice as high.

D
dlinyj, 2013-10-24
@dlinyj

The bottom line is that PWM is the equivalent of voltage. Those. if the duty cycle is zero, then the output voltage is zero, and if the duty cycle is 100%, then it is 3.3 (or 5 volts, depending on the controller logic). A high-pass filter (high-pass filter) is usually placed at the PWM output, cutting off the PWM signal itself, and as a result, a smoothed signal is obtained. To get a certain sound, for example, a sine, you need to change the duty cycle along the sinusoid (from zero to 100% and then back, you can use ready-made tables). The frequency of the sound depends on the rate of change along the sine. By combining sounds of different frequencies, you can get music.
image
Picture from Wikipedia ( read here )
Something like that.

V
Vladimir Chirkin, 2013-10-24
@shtirlitsus

There is a hardware PWM in the msp430. in order for them to generate a sine, for example, it is necessary to change the duty cycle of the PWM with the sampling frequency. those. achieve the right level at the right time. the level is determined by the duty cycle of the PWM. accordingly, the PWM carrier frequency must be much higher than the maximum frequency of the received sound.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question