Announcement

Collapse
No announcement yet.

Inverter PWM with PIC calculated instead of table

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • richard
    replied
    duty=sin[(counter/100)] '


    That divided by 60 gives us 195 cicles per each 1/60 s. So, with this frequency I have to spread the sinus function through 195 cicles to make this function a sinus cycle in 1/60 seg. This gives me the STEP (not brads) to advance each program cycle (0.65), supposing initially that calculation time would be not relevant. Now, i have to know how much time this calculation takes. If it is about 10 uS, (considering about 100 machine cicles, but here I have not an exacly reference of the real time it takes) I would consider this time as part of the low time of signal and make some compensation
    [counter/100] is a step not a phase angle
    while you rethink the pwm command you need to rethink this too . your logic re needing 195 steps is ok but getting the sin of the step is not what you need ,you need the sin of the phase angle that the step represents. each step would represent 256/195 brads


    Leave a comment:


  • Luis Jim
    replied
    I was in a confusion: PWM is different than HPWM. PWM is a very slow software based picbasic instruction. HPWM is a pcbasic instruction that handles the hardware based PWM for PICs with this capability. So, table 9-3 of the PIC I have, works for HPWM instruction on CCP1 (RB3 port) and the times there stated are true under this instruction. Thank you mpgmike and Richard for your comments. I will reconfigure the program and reply it.

    Leave a comment:


  • richard
    replied

    PWM from the manual

    Outputs a pulse width modulated pulse train on Pin. Each cycle of PWM consists of 256 steps. The Duty cycle for each PWM cycle ranges from 0 (0%) to 255 (100%). This PWM cycle is repeated Cycle times. Pin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).
    PWM is a software-based command and does not require that the target device have PWM capability. The output pin may be set to any digital I/O pin, and may be different in different PWM commands within the same program.
    The Cycle time of PWM is dependent upon the oscillator frequency. If a 4MHz oscillator is used, each Cycle is about 5ms long. If a 20MHz oscillator is used, each Cycle is about 1ms in length. Defining an OSC value has no effect on PWM. The Cycle time always changes with the actual oscillator speed.
    If you want continuous PWM output and the PIC MCU has PWM hardware, HPWM may be used instead of PWM. See the section on for HPWM more information about it.
    Pin is made an output just prior to pulse generation and reverts to an input after generation stops.

    try it



    So, if I want 15 cycles with, for instance 50% duty, the instruction should be [ PWM pin, 127, 15 ]
    will give you 15 * 256 steps of 50% duty which will take 5x15 mS @ 4mhz or 1x15mS @20mhz ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,


    Table 9-3 of 16F628A
    what is that ? some vague reference to the pwm section of the datasheet for a pic16f628a .totally irrelevant to the PWM command
    Last edited by richard; 08-11-2019, 05:56 PM.

    Leave a comment:


  • mpgmike
    replied
    The trick is that you are targeting a different DC% each PWM period; first pulse at say 10%, second pulse at 23%, 3rd at 46%, and so on on the sinusoidal rise, then a reverse on the way back down. You might use a timer interrupt for your period and do a PULSOUT for the appropriate amount of time to get your effective duty cycle. The PULSOUT value would be from a look-up table that increments from 0 to max, then reverses from max back to 0 again. Using the ARRAYWRITE command you can create that table and save it with a variable type alias. To reference it, you use [brackets] to delineate your offset within the table;
    Code:
    ARRAYWRITE SineWave {10,23,46...}
    b0 VAR BYTE
    Dir VAR BIT
    
    Interrupt_Routine:
    PULSOUT SineWave[b0]
    IF Dir = 0 THEN
      b0 = b0 + 1
    ELSEIF Dir = 1 THEN
      b0 = b0 - 1
    ENDIF
    IF (b0 = [MAX SineWave Count])  AND (Dir = 0)THEN
      Dir = 1
    ELSEIF (b0 = 0) AND (Dir = 1) THEN
      Dir = 0
    ENDIF
    RETURN

    Leave a comment:


  • Luis Jim
    replied
    As I understood (I could be wrong), PWM cycle parameter states quantity of cicles to perform with a duty % of what is stated in duty parameter. So, if I want 15 cycles with, for instance 50% duty, the instruction should be [ PWM pin, 127, 15 ] and frequency is not defined here but depends on clock. Table 9-3 of 16F628A says that I would get 19530 Hz with a 20 MHz crystal, so if I have a 12 MHz the PWM frequency would be 11,718. That divided by 60 gives us 195 cicles per each 1/60 s. So, with this frequency I have to spread the sinus function through 195 cicles to make this function a sinus cycle in 1/60 seg. This gives me the STEP (not brads) to advance each program cycle (0.65), supposing initially that calculation time would be not relevant. Now, i have to know how much time this calculation takes. If it is about 10 uS, (considering about 100 machine cicles, but here I have not an exacly reference of the real time it takes) I would consider this time as part of the low time of signal and make some compensation. If this takes, as you say 1,000 uS, this algorithm is useless, but I'd thank you a lot if you help me documenting why you think that just one PWM instruction would take all that time. Another way is to ignore PWM function and simply make a clock calculation with interruption to measure the time I want the signal ON and by a simple substraction get the complement for the OFF time, making a while or for -next cycle for each half cycle. So, can you document the time you stated or suggest another thing? Thank you for your advice.

    Leave a comment:


  • richard
    replied
    pwm pin,duty,1 WILL GIVE 1mS of dutycycle duty @125khz with a fosc=20mhz
    PWM is a software generated signal that does not use the ccp/pwm module

    since sin takes values of 0 to 255 brads i cannot see how a value of sin 0 to 195 would ever represent a sine wave

    as it stands you have a 3Hz output signal when filtered , not much use for an inverter that i can see

    Leave a comment:


  • Luis Jim
    started a topic Inverter PWM with PIC calculated instead of table

    Inverter PWM with PIC calculated instead of table

    I have seen several posts about PWM in order to make a one phase inverter, (I want to do the pic based model over 16F628A and with a 12 MHz crystal)
    and most of them use a long values table calculated to get the sine wave, but what if it calculates the duty value each time?




    In the graph can be seen some details, Click image for larger version

Name:	Ciclo_PWM_628A.png
Views:	33
Size:	27.7 KB
ID:	7793
    There I see I will have to compensate about 1/7 of cicle time due to the calculation time.

    The core of program:

    ----------------- picbasic pro:
    counter=0
    loop:
    duty=sin[(counter/100)] ' to compensate decimal handle in above lines PWM
    pin,duty,1 ' PWM pin,duty,cycle at 11,718 Hz (the table 9-3 of this pic relates the PWM frequencies for a 20 MHz clock)
    counter=counter + n ' 127 cicles n=1, n= 127/195= 0.65 -> n =65 (since it doesn't handle decimals)
    if counter= 19500 then counter=0
    goto loop
    -----------------

    So, What do you think about it?
    Thank you in advance for your answer
    L.J.

Working...
X