Sine wave generator
Philldapill
Posts: 1,283
I'm trying to make a type of signal generator that produces a varying PWM signa. The duty cycle of this varying pulse train will be a function of the trig function sine. The following picture should show what I'm talking about.
http://en.wikipedia.org/wiki/Image:Pwm.png
In essence, if you were to overlay a sinewave, like the one shown, the high amplitude parts would coorespond to a longer pulse. When the sinewave is at it's absolute peak, the duty cycle would be 100%, and at it's lowest - 0%. What I've done is created a table in Excel that has all the computed values of the sinewave. I would use the ROM sine values, but in this application, I don't think that will work for various reasons. Now, the values in the excel sheet have been computed and divided up into·128 divisions. These values only represent 90 degrees of the sinewave, since, by definition, the function is symmetrical about the x axis and 90 degree points.
What I've been trying to do is code a PASM program that produces these pulses on TWO pins. One pin will do 180 degrees of the sinewave, then the other pin will repeat the exact same pulsetrain. These two pins will then drive a couple of high speed opto isolators, which will then drive a HIGH power H-bridge(yet to be designed). I am very new to PASM(1 week into it) and am not too experienced, yet, so forgive me for any lack of understanding.
My program flow goes something like this...
Now, this may seem easy enough at first glance, which I thought at first. However, if you start picking apart the flow, you might see a problem. Not only does it take a few clocks to execute the instruction, thus increasing the time the pin is off, but it also leaves very little room to execute the code in the first place when the duty cycle approaches 100% or 0%. If you turn the pin on, then make calculations, then·turn the pin off, you must do all the instructions during the on time. The same applies for the off time vs. on time. The problem comes in either way.
I'm looking for a way to get all the instructions done both ways. The duty cycle MUST approach 0% AND 100%.
My period will always be 2604 clocks. This gives a freq of 30.72kHz, which produces the sinewave at 60Hz(do the math if you want - 30.72kHz/512 periods). Now, if I split my code up into·4 nearly identical segments, each being from 0-45°, 45°-90°, 90°-135°, and 135°-180°, I think I could make it work.
In the first segment, I would be getting the next ON time value in the hub. In this segment, I would be doing the calculations during the off time, as I have more than 1302 clocks(actually anywhere from 2604 to 1302 since the duty will be from 0-50%). The next segment, again getting the NEXT value, but doing the calculations during the ON time since that time will always be greater than the off time. The same goes for the third, but it will be getting the PREVIOUS value, and the fourth, again the previous, but doing the calcuations during the OFF time. The cycle will then repeat on the other pin and again repeat on the other, etc.
Anyway, now that the explaining is done. I will be needing some advice, code wise, through this post. Please reply if you have ANY suggestions whatsoever. Thanks guys!
http://en.wikipedia.org/wiki/Image:Pwm.png
In essence, if you were to overlay a sinewave, like the one shown, the high amplitude parts would coorespond to a longer pulse. When the sinewave is at it's absolute peak, the duty cycle would be 100%, and at it's lowest - 0%. What I've done is created a table in Excel that has all the computed values of the sinewave. I would use the ROM sine values, but in this application, I don't think that will work for various reasons. Now, the values in the excel sheet have been computed and divided up into·128 divisions. These values only represent 90 degrees of the sinewave, since, by definition, the function is symmetrical about the x axis and 90 degree points.
What I've been trying to do is code a PASM program that produces these pulses on TWO pins. One pin will do 180 degrees of the sinewave, then the other pin will repeat the exact same pulsetrain. These two pins will then drive a couple of high speed opto isolators, which will then drive a HIGH power H-bridge(yet to be designed). I am very new to PASM(1 week into it) and am not too experienced, yet, so forgive me for any lack of understanding.
My program flow goes something like this...
Fetch the period of the PWM signal repeat Fetch the new ON time(duty cycle * period which is already stored as a table) Calculate the OFF time(period - ON time) Turn pin on for ON clock ticks Turn pin off for OFF clock ticks Flip the output pin(A to B, or B to A)
Now, this may seem easy enough at first glance, which I thought at first. However, if you start picking apart the flow, you might see a problem. Not only does it take a few clocks to execute the instruction, thus increasing the time the pin is off, but it also leaves very little room to execute the code in the first place when the duty cycle approaches 100% or 0%. If you turn the pin on, then make calculations, then·turn the pin off, you must do all the instructions during the on time. The same applies for the off time vs. on time. The problem comes in either way.
I'm looking for a way to get all the instructions done both ways. The duty cycle MUST approach 0% AND 100%.
My period will always be 2604 clocks. This gives a freq of 30.72kHz, which produces the sinewave at 60Hz(do the math if you want - 30.72kHz/512 periods). Now, if I split my code up into·4 nearly identical segments, each being from 0-45°, 45°-90°, 90°-135°, and 135°-180°, I think I could make it work.
In the first segment, I would be getting the next ON time value in the hub. In this segment, I would be doing the calculations during the off time, as I have more than 1302 clocks(actually anywhere from 2604 to 1302 since the duty will be from 0-50%). The next segment, again getting the NEXT value, but doing the calculations during the ON time since that time will always be greater than the off time. The same goes for the third, but it will be getting the PREVIOUS value, and the fourth, again the previous, but doing the calcuations during the OFF time. The cycle will then repeat on the other pin and again repeat on the other, etc.
Anyway, now that the explaining is done. I will be needing some advice, code wise, through this post. Please reply if you have ANY suggestions whatsoever. Thanks guys!
Comments
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Still some PropSTICK Kit bare PCBs left!
Buuuut, does it have the resolution needed for this? My table is filled with values ranging from 0 to 2604. Plus, this PWM train is at 30.72kHz. I suppose I could reduce my resolution to 15.36kHz, but thats a big difference in steps.
Leon
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Amateur radio callsign: G1HSM
Suzuki SV1000S motorcycle
The hardware will come later, once I get this darn thing to work right...
If this is for your DC-AC inverter, I doubt you'll need more than 8 bits of resolution, if that much. Can you scale the values in your table to fit?
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Still some PropSTICK Kit bare PCBs left!
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Still some PropSTICK Kit bare PCBs left!
A software DDS uses a phase accumulator etc. to generate waveforms:
www.myplace.nu/avr/minidds/index.htm
Leon
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Amateur radio callsign: G1HSM
Suzuki SV1000S motorcycle
I am following this thread with interest as I currently have a project where I have to generate a reasonably clean·sine wave of 40 to 80Hz in 0.1Hz steps.· The output voltage has to be variable up to 150 volts RMS.· Luckily the power required is only about 20 watts at full output.· The unit has to be as·light and efficient as possible so analogue techniques are not acceptable.
I was looking at a centre tapped primary for the transformer as the drive requirement is simpler however the efficiency is reduced.
I am not very familiar with whole harmonic part of this, but I have a feeling that I will have to eventually change the number of pulses to something with less harmonic generation. If anyone has some experience in this area, please, let me know. I just think that I will need an odd number of pulses in this, instead of even...
By the way, Validator, what are you using yours for? Mine is for a DC-AC inverter.
Post Edited (Mike Green) : 7/3/2008 6:07:21 PM GMT
http://forums.parallax.com/forums/default.aspx?f=25&p=1&m=174316
Given the hype, though, I have yet to see any real practical applications described with the magic sinewave approach either for motor drive or for power inverters.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
I have also been looking at doing it with a class-D amplifier but·all the·designs I have seen so far·require positive and negative supplies which would add to the cost.
I only need two units so designing transformers and·complex electronics·would not be viable.
Thanks for the links Tracey.· I will have a play with the information.· As you understand the program already can you explain please·how I might·adapt the code for·the range I need? (40 to 80Hz in 0.1Hz steps)
FRQx = 2^32 * Fout/CLKFREQ
So for example, 60 Hz is
FRQx = 2^32 * 60 / 80E6 = 3221
and the range of FRQx values is 2147 to 4295 for an output frequency of 40-80 Hz.
If you need to calculate that in real time, here is one way to do it in Spin,
Don't get too far into it though without trying it to see if you can get the required power output. I've heard from a respected correspondent that magic sines look good on paper but don't give the expected efficiency in practice.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
One advantage of the magic sines approach in theory is that filtering is needed only for high harmonics. Typically the first significant harmonic is the 23rd , and the amplitude of the harmonic is always less than the fundamental. Don has said in reference to motors that the motor inductance provides one pole and the intertia another pole and additional filtering may not be necessary. An inverter may or may not need additional filtering. Is this for something like a telescope drive?
The trouble is, I don't want to offer practical advice about the load. I came into this because I've always been intrigued with the math, ever since Don started writing about it, and it was clear that the Propeller is preeminently suited to meeting the tight timing requirements with cogs to spare. I haven't really built anything with it and am interested in your experiences if you do pursue it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com