Skip to main content
Tweeted twitter.com/StackElectronix/status/1010670142848749568
Fixed code formatting (first line of code wasn't formatted), plus other fixes (space before units, µ symbol, typos)
Source Link

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++; } 

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 50Hz. I understand that the PWM frequency needs to be as fast possible so its easier to attenuate in the filter.

So far, my uC is running at 8MHz 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. PWM base frequency of 8M/256 (fast PWM mode) = 31.25kHz.

My program updates the duty cycle whenever timer0 overflows i.e. every 32uS (i think... since 1/31250 = 32uS). 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. quite low.

I have included my code below: 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++; } 

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 50 Hz. I understand that the PWM frequency needs to be as fast possible so its easier to attenuate in the filter.

So far, my µC is running at 8 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 maximum PWM base frequency of 8x106/256 (fast PWM mode) = 31.25 kHz.

My program updates the duty cycle whenever timer0 overflows i.e. every 32 µS (I think... since 1/31250 = 32 µ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 frequency quite low.

I have included my code below:

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++; } 
Source Link
Saad
  • 5.5k
  • 13
  • 67
  • 98

Sine wave frequency vs. PWM frequency (AVR)

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 50Hz. I understand that the PWM frequency needs to be as fast possible so its easier to attenuate in the filter.

So far, my uC is running at 8MHz 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. PWM base frequency of 8M/256 (fast PWM mode) = 31.25kHz.

My program updates the duty cycle whenever timer0 overflows i.e. every 32uS (i think... since 1/31250 = 32uS). 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. quite low.

I have included my code below: 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++; }