Generate sine wave modulated PWM with AVR microcontroller

This example will show how ease can sinewave modulated PWM modulated using AVR microcontroller and few lines of code. For this example I used Atmega8 MCU. All project is set up in VMLAB simulator.

To achieve this I saved sinewave lookup table in a Program memory (don’t forget to include interrupt.h header file):

const uint8_t sinewave[] PROGMEM= //256 values {0×80,0×83,0×86,…};

PWM is generated by using Phase and frequency correct PWM using 16 bit timer in Atmega8. Modulation is done by updating OCR1A value with one from sinewave table each time when compare matches.

Reading from flash program memory is done simply:

OCR1A=pgm_read_byte(&sinewave[i]);

The line above is placed inside the output compare interrupt for OCR1A service routine.

Resulting signal in scope simulator:

sinewave_PWM.PNG

Download project Sinewave modulated PWM source code if you want to try it by yourself.

This way you can modulate PWM with any signal shape that is stored in lookup table.

Blogsphere: TechnoratiFeedsterBloglines
Bookmark: Del.icio.usSpurlFurlSimpyBlinkDigg
RSS feed for comments on this post
 |  TrackBack URI for this post

New on WinAVR Tutorial
Running TX433 and RX433 RF modules with AVR microcontrollers,
Sometimes in embedded design you may want to go wireless. Might be you will want to log various readi …
Programming AVR ADC module with WinAVR,
Most of AVR microcontrollers have Analog to Digital Converter (ADC) integrated in to chip. Such solut …
New on WinARM Tutorial
What are differences between WinARM and WinAVR,
Everyone who is working with AVR microcontrollers knows this powerful tool – WinAVR (http://win …
LPC2000 watchdog timer,
As in all microcontrollers watchdog timers purpose isto reset microcontroller after reasonable amount …

Leave a Reply