Go Down

Topic: Creating sine waves with an Arduino (Read 2 times) previous topic - next topic

irethedo

Thanks Grumpy Mike.

Not sure if  I understand you correctly, so your patience ia appreciated...

Would I create a PWM output with a 50% duty cycle and determine the
frequency by how often I turn the PWM output on and off?

thanks

Grumpy_Mike

Quote
Would I create a PWM output with a 50% duty cycle and determine the  
frequency by how often I turn the PWM output on and off?


No the PWM duty cycle is set by the value in the look up table. This in effect sets an analogue level after the PWM has been passed through a filter. At the next sample time you fetch the next entry in the table and set the PWM duty cycle with that.
Think of the PWM duty cycle as being an analogue output (which it is).
So set the sample time at your basic rate say every 100uS. This will give you a maximum frequency of 5KHz.
Then at each sample time you increment the pointer into the lookup table by some value. This value determines the final frequency. As you only have a look up table of 32 entries you only need 5 bits to address this. Therefore use the top 5 bits of your pointer to actually address the look up table and output (set the duty cycle) the value.
For a second frequency output use a different pointer a different increment, different PWM output but the same look up table.

irethedo

Quote
Quote
Would I create a PWM output with a 50% duty cycle and determine the  
frequency by how often I turn the PWM output on and off?


No the PWM duty cycle is set by the value in the look up table. This in effect sets an analogue level after the PWM has been passed through a filter. At the next sample time you fetch the next entry in the table and set the PWM duty cycle with that.
Think of the PWM duty cycle as being an analogue output (which it is).
So set the sample time at your basic rate say every 100uS. This will give you a maximum frequency of 5KHz.
Then at each sample time you increment the pointer into the lookup table by some value. This value determines the final frequency. As you only have a look up table of 32 entries you only need 5 bits to address this. Therefore use the top 5 bits of your pointer to actually address the look up table and output (set the duty cycle) the value.
For a second frequency output use a different pointer a different increment, different PWM output but the same look up table.


Thanks Grumpy Mike -

This is great information!

I will read up on how to do this.

irethedo

If I also wanted to have a variable frequency ttl level square wave output, couldn't I just
turn one of the output lines on and off and change the frequency & duty cycle
by how often that I change either of these states on that line?

I suspect that might be easy to do with a timer to control the timing of this?

Grumpy_Mike

Quote
couldn't I just  turn one of the output lines on and off and change the frequency & duty cycle


Yes you can but to get the accuracy you need you will probably have to go to an interrupt driven routine. If you are just interested in the audio then don't bother with the duty cycle changes as all it does is affect the harmonic contents of the sound.

Go Up