Maker Pro
Maker Pro

Sine PWM program

Sophie

Jul 17, 2013
11
Joined
Jul 17, 2013
Messages
11
Sir,
I am in need of a sine PWM program for AT89c51(8051), for H bridge inverter circuit.
Can anybody please tel me how to do this?

Regards
Sophie
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
You use a PWM frequency of N times the output frequency desired. N=16 or 32 is probably good enough. Then you change the duty cycle of the PWM on each period with the value of the sine at that point in the waveform. The sine values can be stored in a table. You would use half the wave and alternate between positive and negative outputs for each half-cycle.

Bob
 

Sophie

Jul 17, 2013
11
Joined
Jul 17, 2013
Messages
11
Thank you sir. Can u give me the corresponding program
I only know the basics of programming.

Sophie
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
I am not at Atmel user, so I can't help you with the actual program.

Bob
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Silly question, but why would you start designing something when you don't know how to handle one of the key components (the microcontroller)?

Or is this an assignment?
 

Raven Luni

Oct 15, 2011
798
Joined
Oct 15, 2011
Messages
798
Also bear in mind that depending on the speed of the processor, there will have to be a trade off between frequency and resolution. I was looking into doing exactly the same with a pic. I'm assuming the atmels work the same way because it only makes sense to do it this way, but basically it works like this:

The PWM module has a period and a duty cycle. These are the number of clock cycles used for 1 pulse and the number of those cycles for which the output is high. So, if you want a 16 bit resolution for your sine wave, you will need 65536 clock cycles multiplied by a sensible number - lets go with Bob's 32 - so thats 2097152 for a single wavelength. If your clock is running at 8MHz, that gives you a maximum sine wave frequency of less than 4 hertz.

As with the above, using a 4 bit sine resolution will yield a maximum frequency of 16kHz, which would be just enough for audio but 4 bits would not give very good quality at all.

Lights and servos - thats pretty much all this type of PWM is good for. For quality sine waves, stick with analogue.
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
Also bear in mind that depending on the speed of the processor, there will have to be a trade off between frequency and resolution. I was looking into doing exactly the same with a pic. I'm assuming the atmels work the same way because it only makes sense to do it this way, but basically it works like this:

The PWM module has a period and a duty cycle. These are the number of clock cycles used for 1 pulse and the number of those cycles for which the output is high. So, if you want a 16 bit resolution for your sine wave, you will need 65536 clock cycles multiplied by a sensible number - lets go with Bob's 32 - so thats 2097152 for a single wavelength. If your clock is running at 8MHz, that gives you a maximum sine wave frequency of less than 4 hertz.

As with the above, using a 4 bit sine resolution will yield a maximum frequency of 16kHz, which would be just enough for audio but 4 bits would not give very good quality at all.

Lights and servos - thats pretty much all this type of PWM is good for. For quality sine waves, stick with analogue.
I am not following your calculations.

To divide the half-cycle into 32 parts, you get a PWM frequency of 120 * 32 = 3840 Hz. With an 8 MHz clock, you get resolution of one part in 8M / 3840 = 2083 or about 11 bits. Which is actually more than the 10 bit resolution PICs have for their PWM modules, so you would get the full 10-bit resolution, which is more than adequate for an inverter.

Bob
 

Raven Luni

Oct 15, 2011
798
Joined
Oct 15, 2011
Messages
798
Hmm - not sure I'm following that either :p

I missed that it was for an inverter - that would work :)

Anyway, the way I understand it, the 32 has nothing to do with division - its for oversampling / stability. A low pass filter is what turns the pwm signal into a sine wave thats why you need a comfortable margin (you could probably go as low as 8).

If you're using the full 10 bits of a standard module, that means a timer generates an interrupt every 1024 clock cycles. The output will be held high for a number of those cycles and then go low (set in the duty cycle register). When the interrupt fires, your code sets the appropriate duty cycle according to a table of sine wave values and the process repeats (following from the above mentioned, you would do this every 8-32 interrupts) .

So with 10 bits and 8x repeat, thats 8192 clock cycles per sample, not per wavelength - my bad - so with an 8MHz clock, thats a maximum sample rate of (less than) 1kHz, and a nyquist frequency of half that.

Further limits include having enough time for all your instructions to execute before the next interrupt fires, also according to the pic datasheet, theres a 4x multiplication of the timing (need to read that bit again)
 

Raven Luni

Oct 15, 2011
798
Joined
Oct 15, 2011
Messages
798
Actually screw that - you dont need the oversampling stuff - the difference is the interrupt frequency over the output frequency :p
 
Top