# Sinusoidal PWM code for PIC18F452?

Discussion in 'Embedded Systems and Microcontrollers' started by Ahmed Waleed, Mar 10, 2009.

1. ### Ahmed Waleed Thread Starter Member

Mar 4, 2008
22
0
Dear All,
Where can I find a code for sinusoidal PWM for PIC18F452 with frequency 60 Hz?
and it must be all digital, no analog signals used (I mean i don't want to use ADC to compare sin wave with triangle wave) I want it fully digital, is it possible to find such a code?!

if someone has done it, maybe it helps me so much.

2. ### mik3 Senior Member

Feb 4, 2008
4,846
63
Use the sin(x) function of a PIC and plug values of (x) with as much resolution you want to get sinusoidal digital values.

3. ### russ_hensel Well-Known Member

Jan 11, 2009
818
47
Or for a more pure pwm approach google <magic sine wave>

4. ### Ahmed Waleed Thread Starter Member

Mar 4, 2008
22
0
this is a very good calculation method, thnx

5. ### Ahmed Waleed Thread Starter Member

Mar 4, 2008
22
0
you mean I use an array using C and put the values of the sin function in it, or there is a possibility to make a function sin(x) and vary the variable X on whole the period (2∏).

6. ### mik3 Senior Member

Feb 4, 2008
4,846
63
There a function sin(x) which calculates the sine value of any angle in radians.

7. ### jpanhalt AAC Fanatic!

Jan 18, 2008
5,550
835
How do you get a variable duty cycle from that?

John

8. ### thatoneguy AAC Fanatic!

Feb 19, 2009
6,357
718
I believe he was along these lines:

Multiply sin(x) with 1/2 max resolution of PWM, add result to 1/2 max resolution. Use result for duty cycle.

sin(180)=-1 - 0% duty cycle, sin (0) = 0 = 50% duty cycle, sin(90) =1 = 100% duty cycle. Change 0-360 degrees to 0-6.28 for radians.

Put result through lowpass filter, followed by decoupling cap to center on zero.