PDA

View Full Version : PWM generation with PIC16F628



Marin
- 15th July 2010, 18:41
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!

HankMcSpank
- 15th July 2010, 21:09
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)



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?!!! :D

mackrackit
- 15th July 2010, 21:37
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.


if (pot_sample >= 122) AND (pot_sample <= 132) then

if pot_sample > 132 then

if pot_sample < 122 then

Marin
- 16th July 2010, 09:10
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.

HankMcSpank
- 16th July 2010, 09:50
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.