sinusoidal PWM


Closed Thread
Page 1 of 3 123 LastLast
Results 1 to 40 of 84

Thread: sinusoidal PWM

  1. #1
    Join Date
    Feb 2011
    Posts
    37

    Default sinusoidal PWM

    Hi there,i'm new to this forum,m learning the PIC BASIC PRO programming and now i want to generate a sinusoidal pulse width modulated signal by using pic16f877 microcontroller,please i need some ideas on how to go about it and code example to start with....

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Hello and welcome to the forum.

    Microchip has an application note for sine wave with the 16F series chip. They show two ways to do it, one with PWM and the next with a resistor ladder. http://ww1.microchip.com/downloads/e...tes/00655a.pdf

    The PIC18F2431 and PIC18F4431 have some great features for multi pwm sine waves. So recently most of the examples use this chip.

    Here are a few examples: http://www.picbasic.co.uk/forum/cont...-DT-interrupts

    http://www.picbasic.co.uk/forum/showthread.php?t=14359

    You can also enter a search for sine and get some more.

  3. #3
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    thanx..m working on them

  4. #4
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Hi,i've read the Microchip appliction notes for sine wave with the 16F series chip..http://ww1.microchip.com/downloads/en/AppNotes/00655a.pdf together with the examples given in http://www.picbasic.co.uk/forum/content.php?r=229-Sine-wave-using-DT- interrupts and http://www.picbasic.co.uk/forum/showthread.php?t=14359.

    there are number of things which i've not understand from these readings and i'm asking for help from anyone who can help me to understand.these things are:
    1)how to generate a lookup table for the sine waveforms
    2)how to include DT_INTS-18.bas file so that when i compile,the compiler will be able to open it
    3)what is the purpose of this piece of code from example of http://www.picbasic.co.uk/forum/cont...-DT-interrupts

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

    thank you in advance.

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    1) You could use the 32 location array you pointed at in question 3. I used M.S. EXCEL.
    3) Get the data sheet for the PIC in PDF format and search for _OSC_HSPLL_1H or some piece of that. In the first of the 3 you can see these bits setup the oscillator and PLL. Most of the PICs have many modes for the oscillator. Internal/ external, divide by 32/ multiply by 4, etc.

    1)how to generate a lookup table for the sine waveforms
    2)how to include DT_INTS-18.bas file so that when i compile,the compiler will be able to open it
    3)what is the purpose of this piece of code from example of http://www.picbasic.co.uk/forum/cont...-DT-interrupts

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

    thank you in advance.[/QUOTE]

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    to bwaxing:

    My advice : read this great post with exemples, the sine is stored in an array :
    http://www.picbasic.co.uk/forum/cont...-DT-interrupts
    Last edited by Ioannis; - 8th May 2011 at 16:32.

  7. #7
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    thanks for the advice,i read examples from that post and I've not understand how to create the lookup table.From that post sine is stored in the array and i don't understand how the value for a particular step have been determined.for example i want to use 8-bit resolutions (256 steps),how will i determine the value of each step?is there a formula or something?
    thanks again.
    Last edited by Ioannis; - 8th May 2011 at 16:32.

  8. #8


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    sine[1]=0
    .
    .
    .
    sine[128]=255

    Excel can do that. The first number is the angle (1->128, like 0-360°), the second is the result of the sine (0->255, like 0-1).

    For each interruption, the command duty=sine[counter] will read the sine value located in the array and the position of this value in the array is determined by the "counter" variable. The result of this command is stored in the "duty" variable.

    A little example:

    Code:
    counter=128
     
    loop:
     
    duty=sine[counter]
     
    PWM pin,duty,cycle
     
    counter=counter-1
    if counter=0 then counter=128
     
    goto loop
    In each loop the counter value is decremented by one, and when the step value equals 0, the counter is reinizialized to 128.

    So in this case, to make a complete sine, it will take 128 loops. If you want a 1Hz sine, you have to repeat this loop exactly 128 times per second.
    Last edited by Ioannis; - 8th May 2011 at 16:33.

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    The max I see most people do is a 72 step lookup or array. (36 or 72 steps makes it easier to off set for multiple phases). The resistor capacitor filter smooths out the steps. It is still 8 bit resolution, even with 32 or 36 steps. If you wanted to have each value be more accurate, you could use 10 bits in your values for each step.
    Last edited by ScaleRobotics; - 2nd March 2011 at 18:37. Reason: 8 bit resolution

  10. #10
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Red face Re: sinusoidal PWM

    Hi,
    thanks for the tip,m trying to modify the example you gave in http://www.picbasic.co.uk/forum/cont...-DT-interrupts so that i can generate a 50Hz sinusoidal PWM signal to control a single phase inverter using pic16f877.If i understand u correctly,in the sine array you jumped four steps to get 128 steps of 8-bit resolution,but how you got the correspond value for each step is not clear to me,i want to learn how to create a lookup table the way you did 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''.

    When i copied the example and try to compile i got the warning 'unable to open include file DT_INTS-18.bas'..what should i do about this?

    Finally,is there any other modification i should do in that program that it could work better in pic16f877?
    Last edited by Ioannis; - 8th May 2011 at 16:33.

  11. #11


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    You have to download Darrel Taylor's interrupts system : http://darreltaylor.com/DT_INTS-14/intro.html

    And put the "DT_INTS-14.bas", etc. into the folder of your source files.
    Last edited by Ioannis; - 8th May 2011 at 16:34.

  12. #12
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Unhappy Re: sinusoidal PWM

    Hi,
    i have download DT_INTS-14.bas" and DT_INTS-18.bas files and put them in the folder of my source files,when i try to compile the program again the same warning appeared again "unable to open INCLUDE file DT_INTS-18.bas"!
    Last edited by Ioannis; - 8th May 2011 at 16:34.

  13. #13
    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,
    i have download DT_INTS-14.bas" and DT_INTS-18.bas files and put them in the folder of my source files,when i try to compile the program again the same warning appeared again "unable to open INCLUDE file DT_INTS-18.bas"!
    You would need to change the include statement, so that it is using DT_INTS-14 instead of DTS_INTS-18. 18 is for PIC18's, DT_INTS-14 is for lower chips, like PIC17,PIC16 and PIC12's.

    Then there are probably different registers that will give you errors, etc. You should be able to change it, (and also the configs), but it will be a little work.

  14. #14
    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 16:34.

  15. #15
    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: 7185
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 02:41. Reason: 109?? changed to 102 + 128

  16. #16
    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

  17. #17
    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.

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    I don't think your chip has a PLL. So its always best to go with the default configs when you try to change chips. So if you just erase this from the code:
    Code:
    asm
        __CONFIG    _CONFIG1H, _OSC_HSPLL_1H
        __CONFIG    _CONFIG2H, _WDTEN_OFF_2H & _WDPS_512_2H
        __CONFIG    _CONFIG4L, _LVP_OFF_4L
    endasm
    It will use the defaults located in PBP under your chipname.inc

    Then you will get less errors

  19. #19
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Quote Originally Posted by scalerobotics View Post
    I don't think your chip has a PLL. So its always best to go with the default configs when you try to change chips. So if you just erase this from the code:
    Code:
    asm
        __CONFIG    _CONFIG1H, _OSC_HSPLL_1H
        __CONFIG    _CONFIG2H, _WDTEN_OFF_2H & _WDPS_512_2H
        __CONFIG    _CONFIG4L, _LVP_OFF_4L
    endasm
    It will use the defaults located in PBP under your chipname.inc


    Then you will get less errors
    I've removed that part of the code together with other modifications but these two errors kip on coming,i dont how to make it work....

    m getting these two assembler errors.
    ERROR"INT_Handler"-Interrupt flag(PIR1,TMR1IF) not found.)
    ERROR"INT_ENABLE"-Interrupt flag(PIR1,TMR1IF) not found.)

    code.
    Code:
    ; i'm using pic16f877
    ;
    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
     
    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

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Something is not right. Are you compiling for the right chip, the PIC16F877? Because it does have those registers and bits. Are you using MPASM as your assembler, because the include file requires this. Can you send me your myprogram.lst file? That might tell me more....

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    In fact, your code compiles well over here.

  22. #22
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Hi, I've been trying to send you the .LST file,but it's bigger than 500000 characters,so i could not upload it on the forum,how should i send it?
    Last edited by Ioannis; - 8th May 2011 at 16:35.

  23. #23
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Quote Originally Posted by scalerobotics View Post
    Something is not right. Are you compiling for the right chip, the PIC16F877? Because it does have those registers and bits. Are you using MPASM as your assembler, because the include file requires this. Can you send me your myprogram.lst file? That might tell me more....
    Yes i'm compiling for the PIC16F877 using MPSAM as my assembler

  24. #24
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    I've converted the .lst file into pdf format so that i will be able to attach it,hope it will still serve the purpose.....
    Attached Images Attached Images

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Hmmm. The thing that jumps out at me a bit is that you are using Mpasm v2.30. I think that came out in 1999. Really! I think it is time to upgrade. See if that helps you. You can get the latest one by downloading MPLAB IDE here: http://www.microchip.com/stellent/id...&part=SW007002

  26. #26
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Thanks for the tip....yup,it's it working well now....
    i'm reading the pic16f877 data sheet page 63 about how to change the duty cycle of th pwm,it says the duty cycle is specified by writing to CCPR1L:CCPCON<5:4>. So in our case does this code below specify the duty cycle?if not where the duty cycle of pwm has been specified in the program?i want to know so that i may modify the duty cycle to make it equal to 90%.
    code:
    Code:
    sine:
        TMR1L = timerone.byte0
        TMR1H = timerone.byte1     
        CCPR1L = sineval[STEPCOUNT]>>1 
        stepcount = stepcount -1
        if stepcount = 0 then stepcount = 72
    Last edited by Ioannis; - 8th May 2011 at 16:36.

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Great!

    Yes, that is the duty cycle. Why not modify your array for the 90% instead of doing math here? We want to keep multiplication out of our assembly interrupt handler, otherwise, we will need to change it to a PBP type interrupt. And that would take a lot of time for each interrupt, as it needs to save and restore 70 registers each time it interrupts. If you need it to change dynamically, the beauty of an array rather than a lookup, is that you can change it on the fly (doing math in your main).
    Last edited by ScaleRobotics; - 5th March 2011 at 16:48.

  28. #28
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Thanks, i think i got it...let me experiment with the code i have and see what i got.....
    thank again..
    Last edited by Ioannis; - 8th May 2011 at 16:36.

  29. #29
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    hellow everyone,

    I'm simulating this code on proteus Isis but m not getting the expected waveform(sinusoidal wave form)....the code compiles successful...but the waveform i'm getting getting on the scope is not the one...i need help please..

    Code:
    ;
    define OSC 20
    
    ;*****************VARIABLE DECLARATION******************************************
    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
    STEPCOUNT var byte                      'Define stepcount as byte type variable
    stepcount = 256
    
    ;**************SETTING THE REGISTERS WITH APPROPIATE BIT DEFINITION************* 
    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
    
    ;****************A sine lookup 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
    
    ;********Define INT_Handler as ISR********************************************** 
    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    ; Prescaler = 8, TMR1ON          
    TMR1L = 255    
    TMR1H = 254
    @   INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts  
    timerone = $EE16           ;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
    simulation results attached
    Attached Files Attached Files

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    I don't see any kind of wave form on the scope???

  31. #31
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    sure,it is just a line and not a sine wave form as expected....the simulation log say there are attempts to read unimplemented memory locations being ignored..i dont know what does that mean and if it may be one of the cause...i need help.
    Last edited by Ioannis; - 8th May 2011 at 16:37.

  32. #32
    Join Date
    Feb 2011
    Posts
    4


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Maybe it is complaining that you have set your array to:
    sineval var byte[72]

    and then you go from 1 to 72.

    You either need to change it to 0 to 71, or change your array size to 73

  33. #33
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    i have changed the number of array size together with other modifications but there is no change in the simulation result,it is still a line....
    Code:
    define OSC 20
     
    ;*****************VARIABLE DECLARATION******************************************
    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
    STEPCOUNT var byte                      'Define stepcount as byte type variable
    stepcount = 256
     
    ;**************SETTING THE REGISTERS WITH APPROPIATE BIT DEFINITION************* 
    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
     
    ;****************A sine lookup table in an array********************************
    sineval var byte[73]
    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
     
    ;********Define INT_Handler as ISR********************************************** 
    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    ; Prescaler = 8, TMR1ON          
    TMR1L = 255    
    TMR1H = 254
    @   INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts  
    timerone = $EE16           ;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 = 1 then stepcount = 73
    @    INT_RETURN
    Last edited by Ioannis; - 8th May 2011 at 16:37.

  34. #34
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    First thing I would do is make sure the ISR is working. Make an unused pin an output, then toggle it in the ISR. hook a scope to that and make sure it toggles. Also you can do the same in your main with another pin.

    This way you will know some basic info like is my pic running, is my interrupt firing. do that and report back
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  35. #35
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    hi,
    i've managed to get the sinusoidal output sine wave although i'm getting frequency of 202Hz instead of 50Hz and peak voltage of 4.58v instead of 4.5v.Also the waveform is not smooth.Simulation results.docWhat should i do to acheave this parameters?
    code:
    Code:
    define OSC 4
    ;*****************VARIABLE DECLARATION******************************************
    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 
    ADCON0 = %00000000
    ADCON1 = %00000000
    trisb = %11111111
    trisc = %11111011
    trisa = %11111111
    TMR2 = 16
    PR2 = 15      ;set for 16Khz HPWM        
    CCP1CON.3 = 1        ' set to pwm mode
    CCP1CON.2 = 1
    T2CON.2=1
    T2CON.0=1
    STEPCOUNT var byte
    stepcount = 32
    ; 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
    sineval[6] = 213
    sineval[7] = 222
    sineval[8] = 228
    sineval[9] = 230
    sineval[10] = 228
    sineval[11] = 222
    sineval[12] = 213
    sineval[13] = 200
    sineval[14] = 185
    sineval[15] = 167
    sineval[16] = 148
    sineval[17] = 128
    sineval[18] = 108
    sineval[19] = 89
    sineval[20] = 71
    sineval[21] = 56
    sineval[22] = 43
    sineval[23] = 34
    sineval[24] = 28
    sineval[25] = 26
    sineval[26] = 28
    sineval[27] = 34
    sineval[28] = 43
    sineval[29] = 56
    sineval[30] = 71
    sineval[31] = 89
    sineval[32] = 108
     
     
    timerone var word 
     
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System
    ;include "ReEnterPBP-18.bas"  ;not needed for ASM type interrupt service routines
     
    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                 ; Prescaler = 8, TMR1ON
    TMR1L = 255
    TMR1H = 254
    @   INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts  
    timerone = 65504               ;gives about 50 htz sine 
    Main:
            pause 5
            'timerone = timerone - 1      'uncomment to vary 60hz sine
            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 = 32
     
    @    INT_RETURN

  36. #36
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Hi,
    You're still off on the array.
    Code:
    Sinval VAR BYTE[32]
    This creates an array of 32 elements numbered 0-31 but you assign values to it starting at 1 and ending at 32 - not very good. There is no SineVal[32], when you read/write to that variable you're actually accessing the "next" RAM location, something I'm sure you don't want to do here.

    You can adjust the peak value by "scaling" the value in your sine-table but really, you're not even 2% off target.

    Are you using a 4MHz x-tal like the code says or are you using something else?

    If I'm not mistaken a reload value of 65505 with a prescaler of 1:8 running at 4Mhz gives an interrupt frequency of 4032Hz, you have 32 "steps" to your cycle so you "should" get around 126Hz....

    For accuracy it's usually better to stop the timer and then ADD the preload value to the timers current value since this will take into account the interrupt latency etc. Stop it, copy it to a WORD, add the reload value, copy it back and restart the timer.

    Any specific reason you're using a TMR1 prescaler of 8? Using a lower prescaler ratio will give you better "resolution" on the interrupt frequency.

    /Henrik.

  37. #37
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    hellow Henrik,
    thanx for the tips, i've re-worked my sine table,also i've include the code for accuracy although i'm not sure if i've place it in a right place!
    For a 4MHz xtal and TMR1 prescaler of 1 i reload the T1 with the value 64980 to get 50Hz and it's what m getting on the scope.
    Also TMR2 with prescaler of 4 is loaded with 117 for the interrupt to be generated every 556us(256-(556/(4*0.25*4))
    But the problem is that the waveform i'm getting on the scope is not smooth at all....Simulation results..pdf

    code:
    Code:
    define OSC 4
     
    ;*****************VARIABLE DECLARATION******************************************
    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
    STEPCOUNT var byte                      'Define stepcount as byte type variable
    stepcount = 36
     
    ;**************SETTING THE REGISTERS WITH APPROPIATE BIT DEFINITION************* 
    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 = 117
    PR2 = 13          ;set for 18Khz HPWM(=36 steps*10 times*50hz)        
    CCP1CON = %000001100       ;set to pwm mode
    T2CON=%00000110           ;enable timer2 and set timer2 prescaler value of 1:4
     
    ;****************A sine lookup table in an array********************************
    sineval var byte[36]
    sineval[0] = 128
    sineval[1] = 148
    sineval[2] = 167
    sineval[3] = 185
    sineval[4] = 201
    sineval[5] = 215
    sineval[6] = 227
    sineval[7] = 235
    sineval[8] = 240
    sineval[9] = 242
    sineval[10] = 240
    sineval[11] = 235
    sineval[12] = 227
    sineval[13] = 215
    sineval[14] = 201
    sineval[15] = 185
    sineval[16] = 167
    sineval[17] = 148
    sineval[18] = 128
    sineval[19] = 108
    sineval[20] = 89
    sineval[21] = 71
    sineval[22] = 55
    sineval[23] = 41
    sineval[24] = 29
    sineval[25] = 21
    sineval[26] = 16
    sineval[27] = 14
    sineval[28] = 16
    sineval[29] = 21
    sineval[30] = 29
    sineval[31] = 41
    sineval[32] = 55
    sineval[33] = 71
    sineval[34] = 89
    sineval[35] = 108
     
    timerone var word 
     
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System
     
    ;********Define INT_Handler as ISR********************************************** 
    ASM
     
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,   _sine,   ASM,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
     
    T1CON = %000001    ; Prescaler = 1, TMR1ON          
    TMR1L = 255    
    TMR1H = 254
    @   INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts  
    timerone = 64980           ;gives about 50 hz sine 
    Main:
            pause 5
     
    GOTO Main
     
    '---[TMR1_INT - interrupt handler]------------------------------------------
     
    'accuracy thing
    TimerShadow VAR WORD
    T1CON.0 = 0  'Stop TMR1
    TimerShadow.HighByte = TMR1H
    TimerShadow.LowByte = TMR1L
    TimerShadow = TimerShadow + Timerone
    TMR1H = TimerShadow.HighByte
    TMR1L = TimerShadow.LowByte
    T1CON.0 = 1  'Restart TMR1
    sine:
        TMR1L = timerone.byte0
        TMR1H = timerone.byte1     
        CCPR1L = sineval[STEPCOUNT]>>1 
        stepcount = stepcount -1
        if stepcount = 0 then stepcount = 36
    @    INT_RETURN
    where i'm wrong in the code? are the calculations above correct?
    Last edited by Ioannis; - 8th May 2011 at 16:38.

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


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    What are you using for a filter? You should have at least half of the below example to smooth out the signal. Also, it will never look too good on simulation. Might look better on actual hardware. Might be some issues with running it at 4 mhz. Is there a reason you don't want 20 mhz? Even AN655 's example showed 20 mhz.

    Code:
    ;   Filter Diagram
    ;        
    ;               2.7k            2.7k    
    ;    RC2  ___/\  /\  /\______/\  /\  /\________ Analog Output
    ;              \/  \/    |     \/  \/     |  
    ;                        |                |
    ;                      ----- 0.1uF      ----- 0.1uF
    ;                      -----            -----
    ;                        |                |
    ;                       GND              GND
    ;
    ;

  39. #39
    Join Date
    Feb 2011
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    the 877 pic on the board i'm using have 4Mhz clock, and i'm using the same filter as above.are the calculations above correct?
    Last edited by Ioannis; - 8th May 2011 at 16:38.

  40. #40
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: sinusoidal PWM

    Hi,
    Several things.....
    TMR2 is the timer used for the PWM generation. The fact that you initially load it with 117 doesn't matter for either the PWM frequency or your interrupt frequency since you're using TMR1 to generate the interrupts.

    You have the prescaler for TMR2 set to 1:4 and PR2 set to 13 which indeed gives you ~18kHz PWM frequency but it'll only give you a give you 56 discrete dutycycles. Ie. the maximum value you can put in the dutycycle register is 56. The highest value in your sine table is 242 which you then shift right one time bofore putting in the CCP1L, so you're trying to put 121 in there when 56 is max.

    Changing the prescaler to 1:1 and putting 55 in PR2 gives you the same PWM frequency but allows dutycycle values of 0-222.

    TMR1 is now setup with a prescaler of 1:1 and you reload it with 64980 making it interrupt every 65536-64980=556us or at a frequency of 1798Hz, there's now 36 steps to your "cycle" so 1798/36=49.96Hz so it seems to be correct. I don't think you should expect to be able to run much faster than that at 4Mhz, the DT-ints takes quite afew cycles to save and restore all the system variables on each interrupt so you won't have much time left.

    The "accuracy thing" needs to be performed on each reload or it won't have much effect. It should be in your ISR instead of the reload code you have there now.

    /Henrik.

Members who have read this thread : 1

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