Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/30/05 16:05
Read: times


 
Msg Score: +1
 +1 Good Answer/Helpful
#90695 - Both frequency and amplitude? Yes!
Responding to: ???'s previous message
Hi Sushil,

Yes, it is possible to generate different frequencies and amplitude using PWM. I've got a circuit board on my desk right now that does it.

It's a bit of a pain to do, but it can be done. I'm not using the same chip as you, so I won't try to give specific register settings, just the general idea.

I selected a fast clock for the PWM counter and set its reload value so that I got a desired number of samples within a cycle of my lowest audio frequency, and then created a sine lookup table with appropriate compare values for this lowest frequency and with twice as many entries compared to the number of samples I set up with the PWM counter. (More entries allow more freedom to generate different frequencies.)

Then each timer interrupt I load in a new compare value, skipping every other entry in the sine lookup table. This PWM output when filtered gives a nice sine wave. I can scale the value of my lookup table to adjust the waveform's amplitude.

If instead of skipping every other entry in the table, I select every fourth entry, I'll end up with a sine wave that's twice the frequecy of the original (an octave up). If I select every third entry (possible because I put in twice as many as I needed initially) I can get a frequency halfway between these (a perfect fifth in music-speak). By changing the lookup table's index increment value, I can select a number of harmonics of the original frequency. (Fewer samples per cycle at the higher frequencies, but you can't have everything, unless you adjust the PWM frequency at the same time.)

A simple state machine handles the indexing of the lookup table, and I use another to control scaling to generate the desired waveform envelope for the particular sound I'm trying to create.

In your case, you could set the lookup table for the 250Hz low note using all entries, use every other entry to generate 500Hz, and use every fourth entry to generate 1000Hz.

If you really want to get carried away, you can play with your lookup table to generate waveforms other than a sine.

Then again, there are other ways besides PWM to generate sounds. For example, see http://centauri.ezy.net.au/~fastvid/picsound.htm which describes using a serial output to record and play back sound.

Dennis

List of 19 messages in thread
TopicAuthorDate
PWM generation using microcontroller            01/01/70 00:00      
   Whats your intention.            01/01/70 00:00      
      PWM generation using microcontroller            01/01/70 00:00      
         Freq. or duty cycle or both ?            01/01/70 00:00      
         PWM?            01/01/70 00:00      
            Amplitude?            01/01/70 00:00      
               Amplitude control by PWM            01/01/70 00:00      
                  I agree...            01/01/70 00:00      
                     Back to the question            01/01/70 00:00      
                     Remote hugging..            01/01/70 00:00      
                        Sorry guys...            01/01/70 00:00      
                           PWM generation using microcontroller            01/01/70 00:00      
                              Not at all!            01/01/70 00:00      
   Varying both frequency and duty cycle.            01/01/70 00:00      
   possibly this way            01/01/70 00:00      
      thanxs but..................            01/01/70 00:00      
         PWM and frequency            01/01/70 00:00      
            This thread is fuzzy because            01/01/70 00:00      
         Both frequency and amplitude? Yes!            01/01/70 00:00      

Back to Subject List