PWM generation with PIC16F628


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2007
    Posts
    26

    Default PWM generation with PIC16F628

    I want to use 16F628 or 16F877 analog input which will get signal from potentiometer. Output should be 2 pins, one for each direction of PWM to drive H bridge. So, when we having potmeter in center both PWM pins don't output anything and motor is dead, when we turn potmeter in one side, one PWM output is starting egnerating PWM from 0 to 100%, and also same in revenrse, when we turn potmeter in other direction, than other PWM pin starts to output PWM from 0 to 100%.

    To get thing more ilustrated, it should look something like this:

    input on analog port 0:

    0V-------motor 100% reverse
    2,5V-----motor 0%
    5V-------motor 100% forward

    Real volatge levels from potmeter on porta.0 aren't need to be exact, only for ilustration, as here should be some dead time in middle and so.

    As, for one direction I now how to make code, just having ADCIN, x, and then using HPWM where x should be duty cycle variable.....only don't have any idea how to make it output on two pins depending on potmeter?

    thanks!

  2. #2
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    now I'm no programmer and I might be laying myself bare for everyone to laugh at, but this is how I'd likely approach it...

    (disclaimer...there maybe some syntax errors, but you get the methodology blah blah)

    Code:
    begin:
    
    ADCIN 1,pot_sample
    
    if pot_sample = 127 then
    fwd_pwm = 0 & rev_pwm = 0
    hpwm 1,fwd_pwm,20000
    hpwm 2,rev_pwm,20000
    goto begin
    endif
    
    if pot_sample >127 then
    rev_pwm = 0
    fwd_pwm =255-pot_sample x 2
    hpwm 2,rev_pwm,20000
    hpwm 1,fwd_pwm,20000
    goto begin
    endif
    
    
    if pot_sample<127 then
    fwd_pwm = 0
    rev_pwm =127-pot_sample x 2
    hpwm 1,fwd_pwm,20000
    hpwm 2,rev_pwm,20000
    goto begin
    endif
    
    pause 20
    goto begin
    I'm sure to those who really are programmers, then the above is inelegant, but I'm a kludger & therefore go with what I can grasp...not elegancy!

    the above assumes 8 bit AtoD which obviously gives 256 levels & therefore a level of 127 becomes your zero point & your duty cycle changes in steps of 2 up to 256 for each direction. But like I say....what do I know?!!!
    Last edited by HankMcSpank; - 15th July 2010 at 21:15.

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HankMcSpank View Post
    now I'm no programmer and I might be laying myself bare for everyone to laugh at, but this is how I'd likely approach it...
    That is my story too... And it does not stop me.

    127 exactly might be hard to get with a POT so maybe change the range some.

    Code:
    if (pot_sample >= 122) AND (pot_sample <= 132) then
    
    if pot_sample > 132 then
    
    if pot_sample < 122 then
    Last edited by mackrackit; - 15th July 2010 at 22:31. Reason: TYPO
    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    Aug 2007
    Posts
    26


    Did you find this post helpful? Yes | No

    Default

    Thanks for help guys!

    This laste if and is important, because of tolerance on POT and so, is better to ahve some more dead time between forward and reverse! Will try this and let you know how it works.

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Marin View Post
    Thanks for help guys!

    This laste if and is important, because of tolerance on POT and so, is better to ahve some more dead time between forward and reverse! Will try this and let you know how it works.
    I'd considered a 'dead window' in the middle, but didn't go there as you'd not asked & thought it better to keep it 'pure' so to speak - not sure how/the tolerance of the pot comes into play with respect to you last post - the tolerance refers to how far out the actual resistance is from the specified resistance - you don't really care whether say a 10k pot is 9.8k or 10.2k ...the middle point of your pot will be 127 (unless they've some whacky non linear way of making the pot's carbon track!)...you need the 'dead window' not for tolerance purposes, but to have a better chance of finding/hitting the point on the pot where both sides are at rest.

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