• AVR Freaks

sine wave table for offline UPS AN1279A

Author
66pvoarvp
New Member
  • Total Posts : 28
  • Reward points : 0
  • Joined: 2016/12/05 18:29:36
  • Location: 0
  • Status: offline
2018/06/02 02:00:54 (permalink)
0

sine wave table for offline UPS AN1279A

Hi,
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.
 
First entry in the table is 0-> we took that theta=0.
128th entry =13695 -> we took theta=90.
 
for dsPIC mcu, pwm Duty reg length=16bits, then considering shifting of sine wave,
the maximum value ~2^16=65535
Half of this value is 32768
 
if pwm resolution is taken as 15bits, then max value=32767, half value=16384
But in the table, max value=13695
 
I read related forum posts about sine table calculations,  the calculated vaues are different from those in the table.
did they take some lower value intentionally, due to some reason. or am i wrong in understanding it.
 
Thanks
 
#1

19 Replies Related Threads

    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/02 02:25:59 (permalink)
    0
    indeed it looks like they wanted to "trim" the max value a little bit.

    GENOVA :D :D ! GODO
    #2
    66pvoarvp
    New Member
    • Total Posts : 28
    • Reward points : 0
    • Joined: 2016/12/05 18:29:36
    • Location: 0
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/02 03:32:47 (permalink)
    0
    Hi DarioG,
    Thanks for sharing the info.
    How do we decide that trimming value?
     
    Thanks
    #3
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/02 04:24:20 (permalink)
    0
    Oh well, I've got no clue Smile
     
    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)

    GENOVA :D :D ! GODO
    #4
    66pvoarvp
    New Member
    • Total Posts : 28
    • Reward points : 0
    • Joined: 2016/12/05 18:29:36
    • Location: 0
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/02 04:40:50 (permalink)
    0
    prepare full table, scale it dynamically as needed. It is a good idea.
    Thank you
    #5
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3231
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/02 05:02:38 (permalink)
    +1 (1)
    You only need the sin angles 0 - 89 or 1 - 90.
     
    Use a spreedsheet with angles 1 - 90, convert to radians then caluculate Q1.5 value.
    All 4 (sin, cos) quadrants can then be calculated.
     
    The program only needs to do multiply

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #6
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3231
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/02 05:13:09 (permalink)
    0
    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
    ;----------------------------------------------------
    .section sincosdata,code   ;constants
    ;----------------------------------------------------
    sincostable:
    .word    0    ;0
    .word 572
    .word 1144
    .word 1715
    .word 2286
    .word 2856  ;5
    .word 3425
    .word 3993
    .word 4560
    .word 5126
    .word 5690  ;10
    .word 6252
    .word 6813
    .word 7371
    .word 7927
    .word 8481  ;15
    .word 9032
    .word 9580
    .word 10126
    .word 10668
    .word 11207 ;20
    .word 11743
    .word 12275
    .word 12803
    .word 13328
    .word 13848 ;25
    .word 14365
    .word 14876
    .word 15384
    .word 15886
    .word 16384 ;30
    .word 16877
    .word 17364
    .word 17847
    .word 18324
    .word 18795 ;35
    .word 19261
    .word 19720
    .word 20174
    .word 20622
    .word 21063 ;40
    .word 21498
    .word 21926
    .word 22348
    .word 22763
    .word 23170 ;45
    .word 23571
    .word 23965
    .word 24351
    .word 24730
    .word 25102 ;50
    .word 25466
    .word 25822
    .word 26170
    .word 26510
    .word 26842 ;55
    .word 27166
    .word 27482
    .word 27789
    .word 28088
    .word 28378 ;60
    .word 28660
    .word 28932
    .word 29197
    .word 29452
    .word 29698 ;65
    .word 29935
    .word 30163
    .word 30382
    .word 30592
    .word 30792 ;70
    .word 30983
    .word 31164
    .word 31336
    .word 31499
    .word 31651 ;75
    .word 31795
    .word 31928
    .word 32052
    .word 32166
    .word 32270 ;80
    .word 32365
    .word 32449
    .word 32524
    .word 32588
    .word 32643 ;85
    .word 32688
    .word 32723
    .word 32748
    .word 32763 ;89
    sincostable_end:
    ;----------------------------------------------------



    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #7
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/02 06:01:54 (permalink)
    0
    ah yep, indeed having 128 steps is not so useful - 90 are usually enough :)

    GENOVA :D :D ! GODO
    #8
    NorthGuy
    Super Member
    • Total Posts : 5572
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/02 06:36:04 (permalink)
    0
    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.
    #9
    Mysil
    Super Member
    • Total Posts : 3330
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/02 07:49:29 (permalink)
    0
    Hi,
    Table in attachment in message #1 seem to be tailored to PWM with 14 bit resolution.
    and limit the duty cycle to about 83 %
    Edit, message above:
    Or a PWM period counter of 13 696 or higher, maybe 13750.
    Aiming for a PWM period frequency of 16 kHz, this will still require a PWM clock signal of 220 MHz.
     
    Depending on circuits and components in inverter power stages,
    driving switches to 100 % duty cycle may not be desirable.
     
    Depending on what kind of FET or IGBT drivers are used, switch off time may be longer than switch on time.
    This may increase the effective duty cycle of switching transistors, from values listed in the table.
     
    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.
    If the capacitor is small, there should not be too long time without PWM pulses when the High-side switch is on.
     
    I agree with the suggestion above to use a spreadsheet, and calculate your own table.
     
    If you are making a inverter for a fixed output frequency, and have defined a PWM frequency,
    it may be possible to define a modulation table for the purpose.
    E.g.:  PWM base frequency 16 kHz and inverter output frequency 50 Hz:
          16000 / 50 / 4 = 80 PWM pulses in each quarter of the output waveform.
          a sine table with 80 entries may be used directly.
    or the other way around, for 50 Hz fixed frequency output, and modulation table with 128 entries,
    a PWM base frequency of 25600 Hz may be used with direct table lookup, without interpolation in time and phase angle.
    But for full 16 bit PWM resolution, this would imply a PWM clock frequency of 1.677 GHz
     
    For a variable frequency drive,  it is different, then scalling in time and phase angle is needed.
    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.
     
    You select PWM resolution according to what clock frequency is available, and what PWM period is wanted.
    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.
     
       Mysil
    post edited by Mysil - 2018/06/02 09:01:16
    #10
    Bob White
    Super Member
    • Total Posts : 261
    • Reward points : 0
    • Joined: 2010/11/06 19:52:38
    • Location: Denver, Colorado
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/02 10:57:32 (permalink)
    0
    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").
    #11
    PStechPaul
    Super Member
    • Total Posts : 2375
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/02 15:07:59 (permalink)
    0
    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.
     
    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.
     
    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:
    sincostable:
    .word    0    ;0
    .word 572  ; 1.74%
    .word 1144  ; 1.74%
    .word 1715  ; 1.74%
    .word 2286
    .word 2856  ;5
    .word 3425
    .word 3993
    .word 4560
    .word 5126
    .word 5690  ;10 1.72%
    ...
    .word 32270 ;80
    .word 32365  ; 0.29%
    .word 32449
    .word 32524
    .word 32588
    .word 32643 ;85
    .word 32688
    .word 32723
    .word 32748 ; 0.08%
    .word 32763 ;89
    sincostable_end:

    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.
     
    Here is an interesting article:
    http://www.wseas.us/e-lib...tania/MINO/MINO-06.pdf
     
     

     
    #12
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3231
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/03 04:56:27 (permalink)
    0
    There is a slight bit of rounding
     
    Angle = 5
    x = sin(radian(Angle))
    x = 0.08715574274765817355806427083747
     
    Q1.5 = x * (2 ^ 15)
    Q1.5 = 2855.9193783552630311506500268023
    Q1.5 Rounded to 2856
     
    To get more accuracy each angle could be stored as a 24 or 32bit number.
     
    ;    0 -  89  +cos    -sin
    ;  90 - 179  +cos    +sin
    ;180 - 269  -cos    +sin
    ;270 - 359  -cos    -sin

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #13
    PStechPaul
    Super Member
    • Total Posts : 2375
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/03 13:44:39 (permalink)
    +1 (1)
    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 1 degree, the value will be wrong at 4.9 degrees and 5.1 degrees. If the PWM value changes as the time reaches the sample point, it will be 2286 at 4.9 (which is the value for 4 degrees), then jump to 2856 at 5.0 degrees (where it will briefly be correct), and then it will be too low until the 6.0 degree point. The error will be from 0 to 1.75% from 5.0 to 5.9 degrees, so the average error will be about 0.9%. An 8 bit PWM will have an error of about 0.4%, so more bits will not reduce the digitizing error.
     
    Doubling the sample rate will cut the maximum error to half, so the usual 10 bit PWM with samples every 0.25 degrees will have a maximum error of about 0.5%, even though the accuracy of the pulse width is now about 0.1%. But now the sampling rate (PWM carrier frequency) will be 86.4 kHz for 60 Hz. This may be well within the capability of the PIC, but it may cause much higher switching losses, especially for IGBTs.
     
    Most of the digitizing error occurs at the lowest amplitude of the sine wave, so it contributes much less to the RMS value of the waveform. This is why even the "modified sine wave" inverters, having only one bit PWM, can work satisfactorily for most purposes.
     
    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.

     
    #14
    66pvoarvp
    New Member
    • Total Posts : 28
    • Reward points : 0
    • Joined: 2016/12/05 18:29:36
    • Location: 0
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/03 22:28:54 (permalink)
    0
    Hi DarioG, Gort2015, NorthGuy, Mysil, Bob White and PStechPaul
     
    I whole heartedly thank all of you, for the inputs and technical analysis put forth by you.
    The inputs are very much useful for us to move forward.
     
    One lame doubt i have is: In many LUTs, the values are for Q1.5 cycle, why it so, instead of Q1 cycle?
     
    With Thanks
    #15
    PStechPaul
    Super Member
    • Total Posts : 2375
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/04 02:21:37 (permalink)
    0
    What is Q1.5 cycle? AFAIK, a sine function is based on a vector rotation in four quadrants, where I would assume Q1 is 0 to 90 degrees. Sine (as well as cosine) values can be easily computed for any rotation in any of other three quadrants.
     
    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 6 degrees is 3425, so a linear interpolation for 3 degrees would be 1712. The actual value is 1715. Even using 10 degree steps would result in a value of 5690/2=2845 for 5 degrees, with the actual value being 2856. Using the same method for 85 degrees results in an interpolated value of (32768+32270)/2 = 32519 as compared to 32643, which is an error of only 0.4%. The maximum error would be expected at 45 degrees, where the interpolation gives an error of 0.8%.
     
    By the way, I had to use the "full version" to post this, and the forum title has reverted to the APG vNext Trial

     
    #16
    66pvoarvp
    New Member
    • Total Posts : 28
    • Reward points : 0
    • Joined: 2016/12/05 18:29:36
    • Location: 0
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/04 02:39:04 (permalink)
    0
    Hi PStechPaul,
    it was typo or mis typed.
    In many application notes & other codes, the LUT values are taken for 135 degrees, not for just 90 degrees.
    They could take for only quarter cycle, but they took more, what is the reason.
     
    With Thanks
     
    #17
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3231
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/04 05:24:43 (permalink)
    0
    Q1.5 is a fixed point number format.
     
    You would do this in assembly and it would set the 40bit accumulator A.
    mpy   arg_Angle * Q15, A
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #18
    PStechPaul
    Super Member
    • Total Posts : 2375
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/04 14:11:00 (permalink)
    0
    I had a hard time searching for the meaning of Q1.5. Today I found:
     
    http://www.academia.edu/13157605/Qn.m_Format
     
    As for look-up tables that span more than one quadrant, I could not find anything. But the following is interesting and perhaps useful:
     
    https://stackoverflow.com...sine-value-performance
     
    https://jfdube.wordpress....k-up-tables-revisited/
     
    https://gist.github.com/wrl/9913185
     
    http://www.cypress.com/file/144746/download
     
    https://www.eevblog.com/f...ine-wave-lookup-table/

     
    #19
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3231
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: sine wave table for offline UPS AN1279A 2018/06/04 23:07:35 (permalink)
    0
    btw, This is an 8bit forum, you have a 16bit mpu.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #20
    Jump to:
    © 2019 APG vNext Commercial Version 4.5