Menu

Software PWM Calculations

Help
Anonymous
2012-02-15
2013-05-30
  • Anonymous

    Anonymous - 2012-02-15

    Hi Gang,

    I hope I'm not missing something obvious here! If I apply the calculation shown in the Help file for the PWMOut command to the code below, I expect to see a pulse wave of about 26 kHz. But in fact, with the actual circuit I get one-hundredth of this, a waveform of about 263 Hz. Just what in the heck am I misinterpreting?

    By the way, the program and circuit work just fine; the LED dims as expected. But why the low frequency of the driving waveform?

    ;A program to fade a single LED on and off using software PWM

    ;********************* Configuration **********************
    #chip 16F88, 4             ;PIC16F88 running at 4 MHz
    #config mclr=off           ;reset handled internally
    #config osc=int            ;use internal clock

    ;*********************** * Constants ************************
    #define PWM_Out1 PortB.0   ;PWM output on B.0

    ;************************  Program *************************
    pgm:
      dir PWM_Out1 out         ;make this an output
     
      do   
        for width = 0 to 255   ;this spans 0% to 100%
          PWMOut(1, width, 1)  ;channel 1, duty cycle, cycles
        next
       
        for width = 255 to 0   ;now fade out
          PWMOut(1, width, 1) 
        next
      loop                     ;repeat in perpetuity
    end

    Thanks,

    Thomas Henry

     
  • Anonymous

    Anonymous - 2012-02-20

    Hello again,

    Well, I spent another night experimenting, measuring and observing, but this really has me stumped. Can someone please put me out of my misery and suggest what's going on?

    Here's the deal. Go to the help screen for PWMOut. In that panel you'll see a formula for computing the frequency (period, actually, but just take the reciprocal) of the PWM signal generated in software. The computation keeps coming up 100 times longer than what I'm seeing in the actual circuit. For my situation, the formula says the signal should be 26 kHz, but I only measure 260 Hz. Just what the heck am I misinterpreting?

    Not only that, but the PWMDelay factor doesn't work out either. Is that formula legit? If you're willing to help, let's keep it simple. Just tell me if you can make the computations work for you.

    Like I said earlier, the software PWM does in fact work, and I can certainly use it. I just want to understand what's going on.

    Many thanks,

    Thomas Henry

     
  • kent_twt4

    kent_twt4 - 2012-02-20

    If you look at the std.h file or the compiled.hex file, the PWMOUT sub loops thru the bit tests 255 times.  Working backwards from the (observed frequency)/255 you get an average of between 14 and 15 instuctions per loop.

    If you need a higher frequency PWM, then look to the HPWM command using the 16f88 CCP1.

     
  • Anonymous

    Anonymous - 2012-02-20

    Hi,

    Thanks for the comment. Yeah, I saw that earlier. But again, my question is: what is that formula for the cycle width in the Help screen trying to tell us? As mentioned, it seems to me to be off by a factor of 100.

    This is just a "matter of curiosity" thing for me. I have no trouble using either hardware or software PWM. I would just like to understand what the author was trying to get across in that formula.

    Any ideas?

    Thomas Henry

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.