# Lookup Table For Sinewave

## Recommended Posts

Hi,

I am trying to create a sine wave inverter using MOSFETs in H-bridge mode. DC supply 12V is switched by the MOSFETs to create a sine wave and this sine wave would be fed to a step up transformer giving 230V / 50 Hertz AC Sine wave. After I googled on how to develop this concept using a PIC, Most of the guys out there have suggested to use a sine wave PWM for the same and for generating sinewave PWM I have to use lookup table. Now I know fundamentally what is a lookup table. If I am right a LUT (lookup table) is a table which has precalculated values corresponding to the variables which are frequently used in the calculations. As in my case suppose there are MOSFETS A,B,C,D in the bridge, where A and D are diagonal to each other and B and C are diagonal and the transformer is connected at the Source-Drain Junction of A-C and B-D, in order to get an AC waveform, I would need to give a PWM input on A, Compliment of A on C, MOSFET D must be kept ON and B MOSFET would be OFF in order to get 1 half cycle. For the other half cycle, PWM input is given to B, Compliment of B on D, C is kept ON and MOFET A is OFF. This half cycle is exactly the same as the previous, but its direction is opposite to that of the previous (due to transformer). So I need a Sine PWM for 180 degress and for the next 180 deg, I will simply switch the other side of bridge with the same PWM. Now if we consider an O/P of 50Hz, half of it would be 100 Hz (Or 50Hz=20mS; 100Hz=10mS) and say I decide to take a sine sample at every 3 degrees for 180 degress (i.e. 10mS), I would have 60 samples of 3 degrees each. So my each sample would be (10mS/60 Samples = 167uS OR 6KHz). Hence the ON Time of my waveform per sample would be 167 X sine value at that particular instance. [This is what my understanding is. Please correct it if thats not the case]. So the sine lookup table should be sine values of 3, 6, 9, 12, 15....180 etc, which would all be floating points (except for 90 deg and 180 Deg). But I am puzzled on seeing none of the lookup tables shown on various forums have any float value, instead all of them had some decimal values ranging from 0 to 1024 or 0 to 255 or atleast 0 to 64. How are these values computed and in the above case what would these values be and in case I am taking a feedback of output voltage with an ADC in order to control the amplitude of the sinewave (O/P voltage) how will the values in this table get affected (The proposed 12V supply is a battery so as it discharges, the output voltage would also fall, hence there might be a need to increase the pulse width accordingly). Also how would this be implemented in the PIC? I am thinking of using 16F887.

I am trying to present the maximum facts that I understood, but I know that I can confuse the most precious brains with my stupid vocabulary. So please bear with me if anybody finds it confusing.

Thanks and Regards

Shree

##### Share on other sites

Hi

In not sure I could understand the all of it, but it think I can add something about the lookup tables.

You are right, lookup tables are used to avoid lenghty calculations in code.

Yes, they are tables of integer values. Or better, they are tables of binary values, you can interpret them as you wich.

The point here is to consider what you are going to store on those tables. If you are using PWM, you are not going to store a table of the Sin() function values.

Based on your description what you would need to store there are the PWM parameters and the I/O configurations for each fase of the cycle.

And these are all integer values.

You have two sets of information that can be stored in tables. One is the binary map of the static outputs (On/Off) at each phase of the cycle, the other the parameters for the PWM oscillator(s).

The status of the static outputs (On/Off) can be derived from the state variable of a controling state machine, so probably only the PWM parameters need to be tabled.

The PIC16F887 can read values directly from the Program flash, so you have two options for the implementation of these tables.

You can use the traditional "retlw" based table, limited to a miximum size of 255 entries of 8bit values (each table), or you can use the flash read option to have bigger tables of 14bit values.. (see section 10 off the datasheet).

About correcting the parameters for the output voltage level, that can be done in several ways, probably the fastest would be to have several tables and step from one to another based on the sampled output level.

Another way will be to have a base table of PWM values and then extra tables with offset values to shift the PWM duty cycle acording to the sensed output level.

By the way, take a look at the new PICs with configurable logic cells (CLC) they might provide you with the extra logic to generate the symetric PWM signal and to steer your signals to the intended inputs of the H bridge.

Just my 2 cents....

Best regards

Jorge

PS: Did you already found this one?

http://technology.niagarac.on.ca/people/mcsele/i2k.htm

Edited by JorgeF
##### Share on other sites

Hello Jorge,

Thanks alot for your reply. But I think I need to know more about the things you explained, because I couldnt grasp them. The link you forwarded was a inverter, but in PUSH PULL configuration, where as for more efficient output, I am thinking of going for a H-Bridge Configuration (as mentioned in earlier post). About the Sine wave look up table, to be more generic, the PWM pulses switching time is directly proportional to amplitude of the sinewave output signal, and since the output waveform is having a sine relation between its X and Y axis (At any given point of distance on X-axis in degrees/Radians, (0-360), the amplitude is Sine value of that distance in Degrees/Radians into the maximum amplitude), I think that there might be some application of sine factor in the PWM switching calculation. But ofcourse other parameters would add too and here is where I am stuck. I dont know how to calculate those values in the lookup table. My static values in one of the coloums of the LUT would correspond to 3,6,9,12,15...180 (3 degrees sampling rate). Hope I dont sound jumbling. But again thanks for your help.

Regards

Shree

##### Share on other sites

if angle is greater then 90 and less than or equal to 180 then sin(angle) = sin(180-angle)

if angle is > 180 then sin(angle) = -sin(angle-180)

So, you only need a table of 90 degrees if you are wiling to do a little work before and after the lookup.

I don't know how many hardware PWM units the device you mentioned has. If it does not have enough, you will need to implement it yourself in a little interrupt service routine. Either way, the input to the PWM (either software based or hardware based) is just an N-bit number where the pulse width is varied from 0 to full on. full on is when the input is at the max number. It sounds like you would like the vary the amplitued to regulate the voltage so you will need to scale the output of your table based on the current set point. To do this you will need to mulitply the output. Since the PICs do not natively do floating point math, you need to use fixed point math. You can put the binary point anywhere you want by simply shifting the final result by a fixed amount.

For example the binay number 00110100 can be thought of as 3.25 if you imagine the binary point between bits 3 and 4 (0011.0100). If you multiply this number by 4 you get 11010000 or 13.0. To convert this back to an integer that you can stick into the PWM. Simply shift it right by 4 to get 00001101 or the value 13.

I did this example with 8 bit math but you would rather do it with 16 bit math so that you have more bits of fraction to play with.

##### Share on other sites

= EDIT =

I should did a refresh on my browser before replying to what I was reading befiore dinner, I would have seen the post from Trossin before replying.

Anyway, I think my 'latin' can still be of some use.

Hi

That link ways mainly to get some sample code, that might worth some study. I noticed that its based on a Microchip PIC.

Getting back to generating a PWM signal with the PIC.

This is like using an ADC that gives you a binary integer value to represent the analog input, or a DAC where you use an integer binary to generate an analog signal. Your code is allways working with integer values.

Generating a PWM signal, either in code or using the PIC internal hardware (PIC16F887 has a CCP and a ECCP modules) is done by means of what is called a Numerically Controled Oscilator.

In its most basic form, you have two binary counters, one for the ON time and another for the OFF time. Values for ON and OFF time are choosen in a way that their sum is constant and represent the PWM signal period.

Efectivelly if we just calculate the points of the sine wave, we get floating point decimal numbers.

This numbers will relate to the TimeON / TimeOFF ratio of the PWM signal, in other words to the Duty Cycle, and not directly to one of these values.

So even with TON and TOFF beeing integer values their ratio can be decimal.

A few months ago I used PWM to control AC grid power power applied to heating resistors, and the PWM was adjusted to the input voltage level read by an ADC input. The values to control the PWM Duty Cycle where stored in tables and also the correcting offsets (like I sugested in a previous post).

The values on that table where obatined with the help of a spreadsheet where I did all the maths backwards (from target temperature to input volatge to PWM base values and correction offsets).

In a nutshell, I translate my analog values in a percentage of their maximum and them applied that percentage to my PWM resolution so obtaining a TimeON table (rounded to the PWM resolution). In the code a simple subtraction would give the TimeOFF value.

The same reasoning was used to obtain the correction offsets for the intended dynamic range (keeping a stable temperature along the acceptable AC input voltage variation).

In the end, I got tables of integer values that were stored in the famous "lookup tables".

Just my 2 cents....

Best regards

Jorge

Edited by JorgeF
##### Share on other sites

Hello Guys,

Yes! thats what I wanted to know. Thanks a million both of you Trossin and Jorge. LUT was a mystery for me for a long time, until I thought of asking about it here. Jorge you were apt and correct about what I wanted to know about them and explained it to the point and like many others, I am a huge fan of Trossin and have been learning about PIC and C programming with his projects right from the day one. Thanks again guys for all the valuable explaination.

Regards

Shree

## Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.