sinusoidal PWM


Closed Thread
Results 1 to 40 of 84

Thread: sinusoidal PWM

Hybrid View

  1. #1
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Smile Re: sinusoidal PWM

    hi everybody,

    i'm here again with the sinusoidal PWM, please bear with me,

    It seems there were some problems with my compiler but after re-installing it,things work pretty well,i've included the file DT_INTS-14.bas in the folder containing the source code and although there some errors,its a good start to me.
    there are few things which are not clear to me from the example ofhttp://www.picbasic.co.uk/forum/cont...-DT-interrupts
    How to obtain the correspond value for each step is not clear to me,i want to learn how to create a lookup table the way it is done in the example as i dont know how to create it using excel.what formula was used to get these values from that example?
    ; Set sine "table" in an array
    sineval var byte[32]
    sineval[1] = 128
    sineval[2] = 148
    sineval[3] = 167
    sineval[4] = 185
    sineval[5] = 200 etc.

    Also for 50Hz,PR2 will be loaded with 78 for 20MHz oscillator frequency of 16f877,but i dont how to arrive in determination of the value loaded for variable'' timerone''.

    Should i include DT_INTS-18.bas file or DT_INTS-14.bas file?it seems to me they perform different functions!which link will i get DT_INTS-18.bas file?

    Finally,is there any other modification i should do in that program that it could work better in pic16f877?

    Thank you in advance.
    Last edited by Ioannis; - 8th May 2011 at 15:34.

  2. #2
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Quote Originally Posted by bwaxing View Post
    hi everybody,

    i'm here again with the sinusoidal PWM, please bear with me,

    It seems there were some problems with my compiler but after re-installing it,things work pretty well,i've included the file DT_INTS-14.bas in the folder containing the source code and although there some errors,its a good start to me.
    there are few things which are not clear to me from the example ofhttp://www.picbasic.co.uk/forum/cont...-DT-interrupts
    How to obtain the correspond value for each step is not clear to me,i want to learn how to create a lookup table the way it is done in the example as i dont know how to create it using excel.what formula was used to get these values from that example?
    ; Set sine "table" in an array
    sineval var byte[32]
    sineval[1] = 128
    sineval[2] = 148
    sineval[3] = 167
    sineval[4] = 185
    sineval[5] = 200 etc.
    To be quite honest, I copied them from the application note I reference in the example I gave. It shows you a little more about it in the notes in the assembly code. If you wanted to make your own, you just need to figure out how many steps you are going to make. If you were doing 36 steps, each step would be 10 degrees. Sin(10 degrees) * 102 + 128 = number. In the Microchip example, they had the max = 90 percent. So 90 percent * 255 = 230. 230 - 128 (the midpoint) = 102. So it will swing + or - 102 from the midpoint (128).

    Then we can make a spreadsheet for our steps. I put the Microchip steps in on the left, then degrees for each step, then the formulated value on the right. As you can see, these pretty much match the Microchip values. You would have to do something similar for your 72 steps.

    Name:  steps.PNG
Views: 45150
Size:  22.2 KB
    Also for 50Hz,PR2 will be loaded with 78 for 20MHz oscillator frequency of 16f877,but i dont how to arrive in determination of the value loaded for variable'' timerone''.
    You will have to experiment with a scope to see what it comes out at. At least, that is how I did it. There are a lot of coming in and out of interrupts and such.
    Should i include DT_INTS-18.bas file or DT_INTS-14.bas file?it seems to me they perform different functions!which link will i get DT_INTS-18.bas file?
    Read my previous reply, I think I answered this already.
    Finally,is there any other modification i should do in that program that it could work better in pic16f877?
    Only change what won't work on a PIC16877 to what will work on a Pic16f877

    I have not checked to see all that is incompatible. You will have to do a little homework, checking the data sheets, and seeing if you get errors for registers that are incompatible. Seeing if the PWM is compatible, or if the way you address it needs to be changed. It's definately doable, as you can see from the Microchip application note which was written for a 16F device.
    Last edited by ScaleRobotics; - 4th March 2011 at 01:41. Reason: 109?? changed to 102 + 128

  3. #3
    Join Date
    Oct 2005
    Location
    Loveland CO USA
    Posts
    83


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Do you know Excel?
    Here is 5 minutes work. Left side is 0 to 255. Right side is 0 to +127 to 0 to -127 to 0.
    Graph right side!
    0 0.0 0.0000 0.0000 0
    1 1.4 0.0246 0.0246 3
    2 2.8 0.0493 0.0493 6
    3 4.2 0.0739 0.0739 9
    4 5.6 0.0986 0.0984 12
    5 7.1 0.1232 0.1229 16
    6 8.5 0.1478 0.1473 19
    7 9.9 0.1725 0.1716 22
    8 11.3 0.1971 0.1958 25
    9 12.7 0.2218 0.2199 28
    10 14.1 0.2464 0.2439 31
    11 15.5 0.2710 0.2677 34
    12 16.9 0.2957 0.2914 37
    13 18.4 0.3203 0.3149 40
    14 19.8 0.3450 0.3382 43
    15 21.2 0.3696 0.3612 46
    16 22.6 0.3942 0.3841 49
    17 24.0 0.4189 0.4067 52
    18 25.4 0.4435 0.4291 54
    19 26.8 0.4682 0.4512 57
    20 28.2 0.4928 0.4731 60
    21 29.6 0.5174 0.4947 63
    22 31.1 0.5421 0.5159 66
    23 32.5 0.5667 0.5369 68
    24 33.9 0.5914 0.5575 71
    25 35.3 0.6160 0.5778 73
    26 36.7 0.6406 0.5977 76
    27 38.1 0.6653 0.6173 78
    28 39.5 0.6899 0.6365 81
    29 40.9 0.7146 0.6553 83
    30 42.4 0.7392 0.6737 86
    31 43.8 0.7638 0.6917 88
    32 45.2 0.7885 0.7093 90
    33 46.6 0.8131 0.7264 92
    34 48.0 0.8378 0.7431 94
    35 49.4 0.8624 0.7594 96
    36 50.8 0.8870 0.7752 98
    37 52.2 0.9117 0.7905 100
    38 53.6 0.9363 0.8054 102
    39 55.1 0.9610 0.8197 104
    40 56.5 0.9856 0.8336 106
    41 57.9 1.0102 0.8470 108
    42 59.3 1.0349 0.8598 109
    43 60.7 1.0595 0.8721 111
    44 62.1 1.0842 0.8839 112
    45 63.5 1.1088 0.8952 114
    46 64.9 1.1334 0.9059 115
    47 66.4 1.1581 0.9160 116
    48 67.8 1.1827 0.9256 118
    49 69.2 1.2074 0.9347 119
    50 70.6 1.2320 0.9432 120
    51 72.0 1.2566 0.9511 121
    52 73.4 1.2813 0.9584 122
    53 74.8 1.3059 0.9651 123
    54 76.2 1.3306 0.9713 123
    55 77.6 1.3552 0.9768 124
    56 79.1 1.3798 0.9818 125
    57 80.5 1.4045 0.9862 125
    58 81.9 1.4291 0.9900 126
    59 83.3 1.4538 0.9932 126
    60 84.7 1.4784 0.9957 126
    61 86.1 1.5030 0.9977 127
    62 87.5 1.5277 0.9991 127
    63 88.9 1.5523 0.9998 127
    64 90.4 1.5770 1.0000 127
    65 91.8 1.6016 0.9995 127
    66 93.2 1.6262 0.9985 127
    67 94.6 1.6509 0.9968 127
    68 96.0 1.6755 0.9945 126
    69 97.4 1.7002 0.9916 126
    70 98.8 1.7248 0.9882 125
    71 100.2 1.7494 0.9841 125
    72 101.6 1.7741 0.9794 124
    73 103.1 1.7987 0.9741 124
    74 104.5 1.8234 0.9683 123
    75 105.9 1.8480 0.9618 122
    76 107.3 1.8726 0.9548 121
    77 108.7 1.8973 0.9472 120
    78 110.1 1.9219 0.9390 119
    79 111.5 1.9466 0.9302 118
    80 112.9 1.9712 0.9209 117
    81 114.4 1.9958 0.9110 116
    82 115.8 2.0205 0.9006 114
    83 117.2 2.0451 0.8896 113
    84 118.6 2.0698 0.8781 112
    85 120.0 2.0944 0.8660 110
    86 121.4 2.1190 0.8534 108
    87 122.8 2.1437 0.8403 107
    88 124.2 2.1683 0.8267 105
    89 125.6 2.1930 0.8126 103
    90 127.1 2.2176 0.7980 101
    91 128.5 2.2422 0.7829 99
    92 129.9 2.2669 0.7674 97
    93 131.3 2.2915 0.7513 95
    94 132.7 2.3162 0.7348 93
    95 134.1 2.3408 0.7179 91
    96 135.5 2.3654 0.7005 89
    97 136.9 2.3901 0.6827 87
    98 138.4 2.4147 0.6645 84
    99 139.8 2.4394 0.6459 82
    100 141.2 2.4640 0.6269 80
    101 142.6 2.4886 0.6075 77
    102 144.0 2.5133 0.5878 75
    103 145.4 2.5379 0.5677 72
    104 146.8 2.5626 0.5472 69
    105 148.2 2.5872 0.5264 67
    106 149.6 2.6118 0.5053 64
    107 151.1 2.6365 0.4839 61
    108 152.5 2.6611 0.4622 59
    109 153.9 2.6858 0.4402 56
    110 155.3 2.7104 0.4180 53
    111 156.7 2.7350 0.3955 50
    112 158.1 2.7597 0.3727 47
    113 159.5 2.7843 0.3497 44
    114 160.9 2.8090 0.3265 41
    115 162.4 2.8336 0.3032 39
    116 163.8 2.8582 0.2796 36
    117 165.2 2.8829 0.2558 32
    118 166.6 2.9075 0.2319 29
    119 168.0 2.9322 0.2079 26
    120 169.4 2.9568 0.1837 23
    121 170.8 2.9814 0.1595 20
    122 172.2 3.0061 0.1351 17
    123 173.6 3.0307 0.1107 14
    124 175.1 3.0554 0.0861 11
    125 176.5 3.0800 0.0616 8
    126 177.9 3.1046 0.0370 5
    127 179.3 3.1293 0.0123 2
    128 180.7 3.1539 -0.0123 -2
    129 182.1 3.1786 -0.0370 -5
    130 183.5 3.2032 -0.0616 -8
    131 184.9 3.2278 -0.0861 -11
    132 186.4 3.2525 -0.1107 -14
    133 187.8 3.2771 -0.1351 -17
    134 189.2 3.3018 -0.1595 -20
    135 190.6 3.3264 -0.1837 -23
    136 192.0 3.3510 -0.2079 -26
    137 193.4 3.3757 -0.2319 -29
    138 194.8 3.4003 -0.2558 -32
    139 196.2 3.4250 -0.2796 -36
    140 197.6 3.4496 -0.3032 -39
    141 199.1 3.4742 -0.3265 -41
    142 200.5 3.4989 -0.3497 -44
    143 201.9 3.5235 -0.3727 -47
    144 203.3 3.5482 -0.3955 -50
    145 204.7 3.5728 -0.4180 -53
    146 206.1 3.5974 -0.4402 -56
    147 207.5 3.6221 -0.4622 -59
    148 208.9 3.6467 -0.4839 -61
    149 210.4 3.6714 -0.5053 -64
    150 211.8 3.6960 -0.5264 -67
    151 213.2 3.7206 -0.5472 -69
    152 214.6 3.7453 -0.5677 -72
    153 216.0 3.7699 -0.5878 -75
    154 217.4 3.7946 -0.6075 -77
    155 218.8 3.8192 -0.6269 -80
    156 220.2 3.8438 -0.6459 -82
    157 221.6 3.8685 -0.6645 -84
    158 223.1 3.8931 -0.6827 -87
    159 224.5 3.9178 -0.7005 -89
    160 225.9 3.9424 -0.7179 -91
    161 227.3 3.9670 -0.7348 -93
    162 228.7 3.9917 -0.7513 -95
    163 230.1 4.0163 -0.7674 -97
    164 231.5 4.0410 -0.7829 -99
    165 232.9 4.0656 -0.7980 -101
    166 234.4 4.0902 -0.8126 -103
    167 235.8 4.1149 -0.8267 -105
    168 237.2 4.1395 -0.8403 -107
    169 238.6 4.1642 -0.8534 -108
    170 240.0 4.1888 -0.8660 -110
    171 241.4 4.2134 -0.8781 -112
    172 242.8 4.2381 -0.8896 -113
    173 244.2 4.2627 -0.9006 -114
    174 245.6 4.2873 -0.9110 -116
    175 247.1 4.3120 -0.9209 -117
    176 248.5 4.3366 -0.9302 -118
    177 249.9 4.3613 -0.9390 -119
    178 251.3 4.3859 -0.9472 -120
    179 252.7 4.4105 -0.9548 -121
    180 254.1 4.4352 -0.9618 -122
    181 255.5 4.4598 -0.9683 -123
    182 256.9 4.4845 -0.9741 -124
    183 258.4 4.5091 -0.9794 -124
    184 259.8 4.5337 -0.9841 -125
    185 261.2 4.5584 -0.9882 -125
    186 262.6 4.5830 -0.9916 -126
    187 264.0 4.6077 -0.9945 -126
    188 265.4 4.6323 -0.9968 -127
    189 266.8 4.6569 -0.9985 -127
    190 268.2 4.6816 -0.9995 -127
    191 269.6 4.7062 -1.0000 -127
    192 271.1 4.7309 -0.9998 -127
    193 272.5 4.7555 -0.9991 -127
    194 273.9 4.7801 -0.9977 -127
    195 275.3 4.8048 -0.9957 -126
    196 276.7 4.8294 -0.9932 -126
    197 278.1 4.8541 -0.9900 -126
    198 279.5 4.8787 -0.9862 -125
    199 280.9 4.9033 -0.9818 -125
    200 282.4 4.9280 -0.9768 -124
    201 283.8 4.9526 -0.9713 -123
    202 285.2 4.9773 -0.9651 -123
    203 286.6 5.0019 -0.9584 -122
    204 288.0 5.0265 -0.9511 -121
    205 289.4 5.0512 -0.9432 -120
    206 290.8 5.0758 -0.9347 -119
    207 292.2 5.1005 -0.9256 -118
    208 293.6 5.1251 -0.9160 -116
    209 295.1 5.1497 -0.9059 -115
    210 296.5 5.1744 -0.8952 -114
    211 297.9 5.1990 -0.8839 -112
    212 299.3 5.2237 -0.8721 -111
    213 300.7 5.2483 -0.8598 -109
    214 302.1 5.2729 -0.8470 -108
    215 303.5 5.2976 -0.8336 -106
    216 304.9 5.3222 -0.8197 -104
    217 306.4 5.3469 -0.8054 -102
    218 307.8 5.3715 -0.7905 -100
    219 309.2 5.3961 -0.7752 -98
    220 310.6 5.4208 -0.7594 -96
    221 312.0 5.4454 -0.7431 -94
    222 313.4 5.4701 -0.7264 -92
    223 314.8 5.4947 -0.7093 -90
    224 316.2 5.5193 -0.6917 -88
    225 317.6 5.5440 -0.6737 -86
    226 319.1 5.5686 -0.6553 -83
    227 320.5 5.5933 -0.6365 -81
    228 321.9 5.6179 -0.6173 -78
    229 323.3 5.6425 -0.5977 -76
    230 324.7 5.6672 -0.5778 -73
    231 326.1 5.6918 -0.5575 -71
    232 327.5 5.7165 -0.5369 -68
    233 328.9 5.7411 -0.5159 -66
    234 330.4 5.7657 -0.4947 -63
    235 331.8 5.7904 -0.4731 -60
    236 333.2 5.8150 -0.4512 -57
    237 334.6 5.8397 -0.4291 -54
    238 336.0 5.8643 -0.4067 -52
    239 337.4 5.8889 -0.3841 -49
    240 338.8 5.9136 -0.3612 -46
    241 340.2 5.9382 -0.3382 -43
    242 341.6 5.9629 -0.3149 -40
    243 343.1 5.9875 -0.2914 -37
    244 344.5 6.0121 -0.2677 -34
    245 345.9 6.0368 -0.2439 -31
    246 347.3 6.0614 -0.2199 -28
    247 348.7 6.0861 -0.1958 -25
    248 350.1 6.1107 -0.1716 -22
    249 351.5 6.1353 -0.1473 -19
    250 352.9 6.1600 -0.1229 -16
    251 354.4 6.1846 -0.0984 -12
    252 355.8 6.2093 -0.0739 -9
    253 357.2 6.2339 -0.0493 -6
    254 358.6 6.2585 -0.0246 -3
    255 360.0 6.2832 0.0000 0

  4. #4
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Smile Re: sinusoidal PWM

    Hi everyone.....

    First of all thanks to all of you who have been kind to assist me in this journey of learning PIC programming,this is important for me for my final project in my bachelor degree in electrical engineering...
    Back to business,i've been working on the suggestions and examples offered to me and things seems to go a little bit forward...however this code is not compiling successfully..


    define OSC 20
    ;wsave VAR BYTE $20 SYSTEM ' location for W if in bank0
    wsave VAR BYTE $70 SYSTEM ' alternate save location for W
    wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1
    wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2
    wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3

    asm
    __CONFIG _CONFIG1H, _OSC_HSPLL_1H
    __CONFIG _CONFIG2H, _WDTEN_OFF_2H & _WDPS_512_2H
    __CONFIG _CONFIG4L, _LVP_OFF_4L
    endasm

    ADCON0 = %00000000
    ADCON1 = %00000000 ;all anolog output
    trisb = %11111111 ;define porta as input
    trisc = %11111011 ;make ccp1/portc.2 an output pin
    trisa = %11111111 ;define porta as input
    TMR2 = 16
    PR2 = 34 ;set for 36Khz HPWM(=72 steps*10 times*50hz)
    CCP1CON = %000001100 ;set to pwm mode
    T2CON=%00000110 ;enable timer2 and set timer2 prescaler value of 1:4

    STEPCOUNT var byte
    stepcount = 256

    ; Set sine "table" in an array
    sineval var byte[72]
    sineval[1] = 128 ;0 degree
    sineval[2] = 137 ;5 degree
    sineval[3] = 146
    sineval[4] = 154
    sineval[5] = 163
    sineval[6] = 171
    sineval[7] = 179
    sineval[8] = 187
    sineval[9] = 194
    sineval[10] = 200
    sineval[11] = 206
    sineval[12] = 212
    sineval[13] = 216
    sineval[14] = 220
    sineval[15] = 224
    sineval[16] = 227
    sineval[17] = 228
    sineval[18] = 229
    sineval[19] = 230 ;90 degree
    sineval[20] = 229
    sineval[21] = 228
    sineval[22] = 227
    sineval[23] = 224
    sineval[24] = 220
    sineval[25] = 216
    sineval[26] = 212
    sineval[27] = 206
    sineval[28] = 200
    sineval[29] = 194
    sineval[30] = 187
    sineval[31] = 179
    sineval[32] = 171
    sineval[33] = 163
    sineval[34] = 154
    sineval[35] = 146
    sineval[36] = 137
    sineval[37] = 128 ;180 degree
    sineval[38] = 119
    sineval[39] = 110
    sineval[40] = 102
    sineval[41] = 93
    sineval[42] = 85
    sineval[43] = 77
    sineval[44] = 69
    sineval[45] = 62
    sineval[46] = 56
    sineval[47] = 50
    sineval[48] = 44
    sineval[49] = 40
    sineval[50] = 36
    sineval[51] = 32
    sineval[52] = 29
    sineval[53] = 28
    sineval[54] = 26
    sineval[55] = 26
    sineval[56] = 26
    sineval[57] = 28
    sineval[58] = 29
    sineval[59] = 32
    sineval[60] = 36
    sineval[61] = 40
    sineval[62] = 44
    sineval[63] = 50
    sineval[64] = 56
    sineval[65] = 62
    sineval[66] = 69
    sineval[67] = 77
    sineval[68] = 85
    sineval[69] = 93
    sineval[70] = 102
    sineval[71] = 110
    sineval[72] =119

    timerone var word

    INCLUDE "DT_INTS-14.bas" ; Base Interrupt System

    ASM

    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler TMR1_INT, _sine, ASM, yes
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM

    T1CON = $31
    TMR1L = 255 ; Prescaler = 8, TMR1ON
    TMR1H = 254
    @ INT_ENABLE TMR1_INT ; Enable Timer 1 Interrupts
    timerone = $FD00 ;gives about 50 hz sine
    Main:
    pause 5
    if timerone = $F9FF then timerone = $FF00

    GOTO Main

    '---[TMR1_INT - interrupt handler]------------------------------------------

    sine:
    TMR1L = timerone.byte0
    TMR1H = timerone.byte1
    CCPR1L = sineval[STEPCOUNT]>>1
    stepcount = stepcount -1
    if stepcount = 0 then stepcount = 72

    @ INT_RETURN

    one of the thing i'm messing up is this part of my code
    asm
    __CONFIG _CONFIG1H, _OSC_HSPLL_1H
    __CONFIG _CONFIG2H, _WDTEN_OFF_2H & _WDPS_512_2H
    __CONFIG _CONFIG4L, _LVP_OFF_4L
    endasm

    To be honest i've been working on this code for the whole day looking on the data sheets but cant find a way out to make it work... of course there are other errors..

    Thank you in advance.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts