SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By dumais
#113306
Hi, I know that this has been covered a lot, but I just can't seem to understand.

I am trying to generate phone-quality(8bit 8khz sampling rate) sound using an atmega32 running at 16mhz.

I was tinking about using Timer0 (8bit) for that.
Technically, All I would need to do is setup the timer correctly and write to OCR0 on every TIMER0_OVF interupt right?

I am using the Fast PWM mode and If I am using a prescaller of 8, my frequency will be 16MHZ/8/TOP=7812hz. Which is not 8KHZ
I have seen somewhere that I could use a prescaler of 8 and set TOP to 250. But how do I set TOP=250?

Another place suggested to use the OCR0 to limit TOP, but that is not possible when in fast PWM mode since OCR0 is used to drive the OC0 pin.
User avatar
By itikhonov
#113335
dumais wrote:8bit 8khz sampling rate
The question not in how to get 8khz, question how you are going to get 8bit (256 different voltage levels on output pin).
By tecoist
#113341
The ATmega fast PWM mode will run at a fixed frequency (your example calculation is quite correct). There's no way to fine-tune this frequency. CTC mode lets you generate other frequencies (that, I think, is what the "other place" meant about using OCR0 to limit TOP), but it doesn't let you PWM the results.

You could upsample your data to 62.5k samples/second, digitally low-pass filter it (to remove alias bands), and then PWM at 62.5ks/s in fast PWM mode, if you have enough processor bandwidth to do that. Or you could buy a cheap external DAC and run it at 8ks/s. Or you could really really cheat and PWM at 7.8ks/s, and drop a sample every now and then: 8ks/s and 8 bits is pretty atrocious sound quality so you may well decide that Cheap is Better and live with the dropped samples (the cheater's down-sample filter).