I'm using an AVR Mega 168 to generate a sine wave via pulse width modulation. My goal is to generate a sine wave with a frequency of approximately 50Hz50 Hz. I understand that the PWM frequency needs to be as fast possible so its easier to attenuate in the filter.
So far, my uCµC is running at 8MHz8 MHz using the internal RC oscillator (I plan on moving to a crystal later on - taking this one step a time). This provides me with a max.maximum PWM base frequency of 8M8x106/256 (fast PWM mode) = 31.25kHz25 kHz.
My program updates the duty cycle whenever timer0 overflows i.e. every 32uS32 µS (iI think... since 1/31250 = 32uS32 µS).
My question now is, how can I 'control' the frequency of the actual sine wave? I can easily slow down by prescaling but that would obviously make the base freq.frequency quite low.
I have included my code below: uint8_t i=0;
uint8_t i=0; int main(void) { int ipwm; DDRD = 0xFF; TCCR0A = 0b10000011; TCCR0B = (1 << WGM12); TIMSK0 |= (1 << TOIE0); TIFR1 |= (1 << OCF0A); TCCR0B = (1 << CS10); TCNT0 = 0; OCR0A = 128; sei(); for(;;) { } return 1; } ISR(TIMER0_OVF_vect) { OCR0A=pgm_read_byte(&sinewave[i]); // update duty cycle i++; }