sine wave table for offline UPS AN1279Ahttps://www.microchip.com/forums/(c) Microchip30Re: sine wave table for offline UPS AN1279A (Gort2015)btw, This is an 8bit forum, you have a 16bit mpu.<br/>https://www.microchip.com/forums/FindPost/1054419Mon, 04 Jun 2018 23:07:35 GMTRe: sine wave table for offline UPS AN1279A (PStechPaul)I had a hard time searching for the meaning of Q1.5. Today I found:<br/> <br/><a href="http://www.academia.edu/13157605/Qn.m_Format" target="_blank" rel="nofollow">http://www.academia.edu/13157605/Qn.m_Format</a><br/> <br/>As for look-up tables that span more than one quadrant, I could not find anything. But the following is interesting and perhaps useful:<br/> <br/><a href="https://stackoverflow.com/questions/1382322/calculating-vs-lookup-tables-for-sine-value-performance" target="_blank" rel="nofollow" title="https://stackoverflow.com/questions/1382322/calculating-vs-lookup-tables-for-sine-value-performance">https://stackoverflow.com...sine-value-performance</a><br/> <br/><a href="https://jfdube.wordpress.com/2011/12/06/trigonometric-look-up-tables-revisited/" target="_blank" rel="nofollow" title="https://jfdube.wordpress.com/2011/12/06/trigonometric-look-up-tables-revisited/">https://jfdube.wordpress....k-up-tables-revisited/</a><br/> <br/><a href="https://gist.github.com/wrl/9913185" target="_blank" rel="nofollow">https://gist.github.com/wrl/9913185</a><br/> <br/><a href="http://www.cypress.com/file/144746/download" target="_blank" rel="nofollow">http://www.cypress.com/file/144746/download</a><br/> <br/><a href="https://www.eevblog.com/forum/microcontrollers/sine-wave-lookup-table/" target="_blank" rel="nofollow" title="https://www.eevblog.com/forum/microcontrollers/sine-wave-lookup-table/">https://www.eevblog.com/f...ine-wave-lookup-table/</a><br/>https://www.microchip.com/forums/FindPost/1054393Mon, 04 Jun 2018 14:11:00 GMTRe: sine wave table for offline UPS AN1279A (Gort2015)Q1.5 is a fixed point number format.<br/> <br/>You would do this in assembly and it would set the 40bit accumulator A.<br/>mpy arg_Angle * Q15, A<br/> <br/>https://www.microchip.com/forums/FindPost/1054335Mon, 04 Jun 2018 05:24:43 GMTRe: sine wave table for offline UPS AN1279A (66pvoarvp)Hi PStechPaul,<br/>it was typo or mis typed.<br/>In many application notes & other codes, the LUT values are taken for 135 degrees, not for just 90 degrees.<br/>They could take for only quarter cycle, but they took more, what is the reason.<br/> <br/>With Thanks<br/> <br/>https://www.microchip.com/forums/FindPost/1054310Mon, 04 Jun 2018 02:39:04 GMTRe: sine wave table for offline UPS AN1279A (PStechPaul)What is <b>Q1.5</b> cycle? AFAIK, a sine function is based on a vector rotation in four quadrants, where I would assume <b>Q1</b> is <b>0 to 90</b> degrees. Sine (as well as cosine) values can be easily computed for any rotation in any of other three quadrants.<br/> <br/>Also, if space is limited for a look-up table, linear interpolation can be done without much expense of computing resources. For instance, the sine value for <b>6</b> degrees is <b>3425</b>, so a linear interpolation for <b>3</b> degrees would be <b>1712</b>. The actual value is <b>1715</b>. Even using <b>10</b> degree steps would result in a value of 5690/2=<b>2845</b> for <b>5</b> degrees, with the actual value being <b>2856</b>. Using the same method for <b>85</b> degrees results in an interpolated value of (32768+32270)/2 = <b>32519</b> as compared to <b>32643</b>, which is an error of only <b>0.4%</b>. The maximum error would be expected at <b>45</b> degrees, where the interpolation gives an error of <b>0.8%</b>.<br/> <br/>By the way, I had to use the "full version" to post this, and the forum title has reverted to the <b>APG vNext Trial </b><br/>https://www.microchip.com/forums/FindPost/1054306Mon, 04 Jun 2018 02:21:37 GMTRe: sine wave table for offline UPS AN1279A (66pvoarvp)Hi DarioG, Gort2015, NorthGuy, Mysil, Bob White and PStechPaul<br/> <br/>I whole heartedly thank all of you, for the inputs and technical analysis put forth by you.<br/>The inputs are very much useful for us to move forward.<br/> <br/>One lame doubt i have is: In many LUTs, the values are for Q1.5 cycle, why it so, instead of Q1 cycle?<br/> <br/>With Thanks<br/>https://www.microchip.com/forums/FindPost/1054291Sun, 03 Jun 2018 22:28:54 GMTRe: sine wave table for offline UPS AN1279A (PStechPaul)The accuracy is irrelevant when using the sine value to span a discrete segment of the waveform. If you are using PWM with steps of <b>1 degree</b>, the value will be wrong at <b>4.9</b> degrees and <b>5.1</b> degrees. If the PWM value changes as the time reaches the sample point, it will be <b>2286</b> at <b>4.9</b> (which is the value for 4 degrees), then jump to <b>2856</b> at <b>5.0</b> degrees (where it will briefly be correct), and then it will be too low until the <b>6.0</b> degree point. The error will be from <b>0</b> to <b>1.75%</b> from <b>5.0</b> to <b>5.9</b> degrees, so the average error will be about <b>0.9%</b>. An 8 bit PWM will have an error of about <b>0.4%</b>, so more bits will not reduce the digitizing error.<br/> <br/>Doubling the sample rate will cut the maximum error to half, so the usual <b>10 bit</b> PWM with samples every <b>0.25</b> degrees will have a maximum error of about <b>0.5%</b>, even though the accuracy of the pulse width is now about <b>0.1%</b>. But now the sampling rate (PWM carrier frequency) will be <b>86.4 kHz</b> for 60 Hz. This may be well within the capability of the PIC, but it may cause much higher switching losses, especially for IGBTs.<br/> <br/>Most of the digitizing error occurs at the lowest amplitude of the sine wave, so it contributes much less to the <b>RMS</b> value of the waveform. This is why even the "modified sine wave" inverters, having only <b>one bit PWM</b>, can work satisfactorily for most purposes.<br/> <br/>Actually, a UPS for most computer equipment could work just as well, and perhaps even better, by supplying DC to the equipment, which usually uses switching power supplies that rectify the incoming AC to DC before anything else.<br/>https://www.microchip.com/forums/FindPost/1054268Sun, 03 Jun 2018 13:44:39 GMTRe: sine wave table for offline UPS AN1279A (Gort2015)There is a slight bit of rounding<br/> <br/>Angle = 5<br/>x = sin(radian(Angle))<br/>x = 0.08715574274765817355806427083747<br/> <br/>Q1.5 = x * (2 ^ 15)<br/>Q1.5 = 2855.9193783552630311506500268023<br/>Q1.5 Rounded to 2856<br/> <br/>To get more accuracy each angle could be stored as a 24 or 32bit number.<br/> <br/>; 0 - 89 +cos -sin<br>; 90 - 179 +cos +sin<br>;180 - 269 -cos +sin<br>;270 - 359 -cos -sin<br/>https://www.microchip.com/forums/FindPost/1054213Sun, 03 Jun 2018 04:56:27 GMTRe: sine wave table for offline UPS AN1279A (PStechPaul)For a 50/60 Hz UPS it is really not necessary to use a very high carrier frequency, or a high resolution (bits). The output voltage is regulated to a single amplitude, so loss of resolution and resulting distortion at low levels is not a factor. And sampling frequency (carrier) of 10 or 20 times the fundamental frequency is likely quite good enough to achieve better than 1% THD, which is much better than actually needed for a UPS application.<br/> <br/>That said, the main reason for using a high carrier frequency is audible noise, so something above 16 or 20 kHz would be advised. At 60 Hz, a 20 kHz carrier is 333 samples per cycle, or 83 samples per 1/4 wave, or 90 degrees. It may be simplest to use 1 sample per degree, which is 21.6 kHz for 60 Hz, or 18.0 kHz for 50 Hz.<br/> <br/>Given the resolution in term of time, looking at the sine table posted above, it is clear that the greatest error occurs near the zero crossing:<br/><pre class="prettyprint">sincostable:<br />.word 0 ;0<br />.word 572 ; 1.74%<br />.word 1144 ; 1.74%<br />.word 1715 ; 1.74%<br />.word 2286<br />.word 2856 ;5<br />.word 3425<br />.word 3993<br />.word 4560<br />.word 5126<br />.word 5690 ;10 1.72%<br>...<br>.word 32270 ;80<br />.word 32365 ; 0.29%<br />.word 32449<br />.word 32524<br />.word 32588<br />.word 32643 ;85<br />.word 32688<br />.word 32723<br />.word 32748 ; 0.08%<br />.word 32763 ;89<br />sincostable_end:<br></pre><br/>With maximum value of 32768, the relative errors are as I have added above. This shows that the error due to the carrier frequency, or time between samples, is dominant over the first 10 degrees, and is still better than 2%, so an error due to pulse width resolution is insignificant. From 80 to 90 degrees, the error is less than 0.3%. From this, I conclude that 8 bit PWM would be just about as good as 10, 12, or 16 bit. Even 5 bit (1/32) would probably be satisfactory, with THD of perhaps 3% or so.<br/> <br/>Here is an interesting article:<br/><a href="http://www.wseas.us/e-library/conferences/2010/Catania/MINO/MINO-06.pdf" target="_blank" rel="nofollow" title="http://www.wseas.us/e-library/conferences/2010/Catania/MINO/MINO-06.pdf">http://www.wseas.us/e-lib...tania/MINO/MINO-06.pdf</a><br/> <br/> <br/>https://www.microchip.com/forums/FindPost/1054196Sat, 02 Jun 2018 15:07:59 GMTRe: sine wave table for offline UPS AN1279A (Bob White)To get a good sine wave one does not want to go to 100% duty cycle. Typically the maximum duty cycle is about 90% (look up "modulation factor").<br/>https://www.microchip.com/forums/FindPost/1054182Sat, 02 Jun 2018 10:57:32 GMTRe: sine wave table for offline UPS AN1279A (Mysil)Hi,<br/>Table in attachment in message #1 seem to be tailored to PWM with 14 bit resolution.<br/>and limit the duty cycle to about 83 % <br/>Edit, message above:<br/>Or a PWM period counter of 13 696 or higher, maybe 13750.<br/>Aiming for a PWM period frequency of 16 kHz, this will still require a PWM clock signal of 220 MHz.<br/> <br/>Depending on circuits and components in inverter power stages,<br/>driving switches to 100 % duty cycle may not be desirable.<br/> <br/>Depending on what kind of FET or IGBT drivers are used, switch off time may be longer than switch on time.<br/>This may increase the effective duty cycle of switching transistors, from values listed in the table.<br/> <br/>Also some types of power FET drivers use a charge pump and a capacitor to provide power to the High side gate drive for N-channel power switches.<br/>If the capacitor is small, there should not be too long time without PWM pulses when the High-side switch is on.<br/> <br/>I agree with the suggestion above to use a spreadsheet, and calculate your own table.<br/> <br/>If you are making a inverter for a fixed output frequency, and have defined a PWM frequency,<br/>it may be possible to define a modulation table for the purpose.<br/>E.g.: PWM base frequency 16 kHz and inverter output frequency 50 Hz:<br/> 16000 / 50 / 4 = 80 PWM pulses in each quarter of the output waveform.<br/> a sine table with 80 entries may be used directly.<br/>or the other way around, for 50 Hz fixed frequency output, and modulation table with 128 entries,<br/>a PWM base frequency of 25600 Hz may be used with direct table lookup, without interpolation in time and phase angle.<br/>But for full 16 bit PWM resolution, this would imply a PWM clock frequency of 1.677 GHz<br/> <br/>For a variable frequency drive, it is different, then scalling in time and phase angle is needed.<br/>A modulation table with 128 entries still make sense, it may make scaling in time more efficient by avoiding division, and instead using multiplication and shift in time scaling expression.<br/> <br/>You select PWM resolution according to what clock frequency is available, and what PWM period is wanted.<br/>Then to determine what 'trim' the modulation table may need, you will need to make measurements on the actual hardware, both unloaded and with full load.<br/> <br/> Mysil<br/>https://www.microchip.com/forums/FindPost/1054172Sat, 02 Jun 2018 07:49:29 GMTRe: sine wave table for offline UPS AN1279A (NorthGuy)The maximum value would depend on the timer period, not on the size of the register. The details depend on the specific OC or PWM module you use.<br/>https://www.microchip.com/forums/FindPost/1054167Sat, 02 Jun 2018 06:36:04 GMTRe: sine wave table for offline UPS AN1279A (DarioG)ah yep, indeed having 128 steps is not so useful - 90 are usually enough :)<br/>https://www.microchip.com/forums/FindPost/1054163Sat, 02 Jun 2018 06:01:54 GMTRe: sine wave table for offline UPS AN1279A (Gort2015)Here is the sin/cos table that I created. I use it to draw ellipses, circles, arcs, sectors and segments. For more accurate values quadrupal the table into steps of 0.25<br/>;----------------------------------------------------<br/>.section sincosdata,code ;constants<br>;----------------------------------------------------<br>sincostable:<br>.word 0 ;0<br>.word 572<br>.word 1144<br>.word 1715<br>.word 2286<br>.word 2856 ;5<br>.word 3425<br>.word 3993<br>.word 4560<br>.word 5126<br>.word 5690 ;10<br>.word 6252<br>.word 6813<br>.word 7371<br>.word 7927<br>.word 8481 ;15<br>.word 9032<br>.word 9580<br>.word 10126<br>.word 10668<br>.word 11207 ;20<br>.word 11743<br>.word 12275<br>.word 12803<br>.word 13328<br>.word 13848 ;25<br>.word 14365<br>.word 14876<br>.word 15384<br>.word 15886<br>.word 16384 ;30<br>.word 16877<br>.word 17364<br>.word 17847<br>.word 18324<br>.word 18795 ;35<br>.word 19261<br>.word 19720<br>.word 20174<br>.word 20622<br>.word 21063 ;40<br>.word 21498<br>.word 21926<br>.word 22348<br>.word 22763<br>.word 23170 ;45<br>.word 23571<br>.word 23965<br>.word 24351<br>.word 24730<br>.word 25102 ;50<br>.word 25466<br>.word 25822<br>.word 26170<br>.word 26510<br>.word 26842 ;55<br>.word 27166<br>.word 27482<br>.word 27789<br>.word 28088<br>.word 28378 ;60<br>.word 28660<br>.word 28932<br>.word 29197<br>.word 29452<br>.word 29698 ;65<br>.word 29935<br>.word 30163<br>.word 30382<br>.word 30592<br>.word 30792 ;70<br>.word 30983<br>.word 31164<br>.word 31336<br>.word 31499<br>.word 31651 ;75<br>.word 31795<br>.word 31928<br>.word 32052<br>.word 32166<br>.word 32270 ;80<br>.word 32365<br>.word 32449<br>.word 32524<br>.word 32588<br>.word 32643 ;85<br>.word 32688<br>.word 32723<br>.word 32748<br>.word 32763 ;89<br>sincostable_end:<br>;----------------------------------------------------<br><br><br/>https://www.microchip.com/forums/FindPost/1054157Sat, 02 Jun 2018 05:13:09 GMTRe: sine wave table for offline UPS AN1279A (Gort2015)You only need the sin angles 0 - 89 or 1 - 90.<br/> <br/>Use a spreedsheet with angles 1 - 90, convert to radians then caluculate Q1.5 value.<br/>All 4 (sin, cos) quadrants can then be calculated.<br/> <br/>The program only needs to do multiply<br/>https://www.microchip.com/forums/FindPost/1054156Sat, 02 Jun 2018 05:02:38 GMTRe: sine wave table for offline UPS AN1279A (66pvoarvp)prepare full table, scale it dynamically as needed. It is a good idea.<br/>Thank you<br/>https://www.microchip.com/forums/FindPost/1054155Sat, 02 Jun 2018 04:40:50 GMTRe: sine wave table for offline UPS AN1279A (DarioG)Oh well, I've got no clue <img src="https://www.microchip.com/forums/upfiles/smiley/209.gif" alt="Smile" data-smiley="<img src="https://www.microchip.com/forums/upfiles/smiley/209.gif" alt="Smile" data-smiley="[:)]" />" /><br/> <br/>If you're using a schematic and code fully provided by someone else (microchip or whoever), stick to those value. If you're designing your own thing, recalculate the table for full scale - I usually use 16bits and scale them down if needed at run time (right shift, quite fast)<br/>https://www.microchip.com/forums/FindPost/1054154Sat, 02 Jun 2018 04:24:20 GMTRe: sine wave table for offline UPS AN1279A (66pvoarvp)Hi DarioG,<br/>Thanks for sharing the info.<br/>How do we decide that trimming value?<br/> <br/>Thanks<br/>https://www.microchip.com/forums/FindPost/1054152Sat, 02 Jun 2018 03:32:47 GMTRe: sine wave table for offline UPS AN1279A (DarioG)indeed it looks like they wanted to "trim" the max value a little bit.<br/>https://www.microchip.com/forums/FindPost/1054148Sat, 02 Jun 2018 02:25:59 GMTsine wave table for offline UPS AN1279A (66pvoarvp)Hi,<br/>we are trying to design 1KVA inverter using dsPIC33 family MCU. For this we have started with AN1279 application note by MICROCHIP. For PWM generation, sine wave table with 128 values is used in that application note. sine table file attached below.<br/> <br/>First entry in the table is 0-> we took that theta=0.<br/>128th entry =13695 -> we took theta=90.<br/> <br/>for dsPIC mcu, pwm Duty reg length=16bits, then considering shifting of sine wave,<br/>the maximum value ~2^16=65535<br/>Half of this value is 32768<br/> <br/>if pwm resolution is taken as 15bits, then max value=32767, half value=16384<br/>But in the table, max value=13695<br/> <br/>I read related forum posts about sine table calculations, the calculated vaues are different from those in the table.<br/>did they take some lower value intentionally, due to some reason. or am i wrong in understanding it.<br/> <br/>Thanks<br/> <br/>https://www.microchip.com/forums/FindPost/1054143Sat, 02 Jun 2018 02:00:54 GMT