Page 1 of 1

Reversed duty cycle calculation in %

Posted: Sat Aug 18, 2012 3:27 am
by Speedy2k
Hi, i currently have a device that run on a pic16f616, it is designed so the PWM duty cycle is revese. so 255 duty cycle on the pwm is 0% speed on the motor and 0 duty cycle on the PWM is 100% speed on the motor. I wouldl ike to show the current 0%-100% value of the current duty cycle on a LCD, is there any way of calculating the value reverse ? It may be an easy one, but i am lost rght now! thanx a lot!

Re: Reversed duty cycle calculation in %

Posted: Sat Aug 18, 2012 9:31 am
by medelec35
Hi Speedy2k, I Have the answer to that, but what version of Flowcode are you using?
V4 pro, V5 pro etc?

I just want to test it works on the same version you have.
Also this requires moving from General Programming, to correct section.

Martin

Re: Reversed duty cycle calculation in %

Posted: Sat Aug 18, 2012 10:56 am
by Enamul
Hi,
One simple way could be doing this in hardware without changing anything in software..for example, if you drive a transistor from pic where 0 duty cycle is 0 volt at output and 100% duty cycle means high in output; so if you use this as base drive of an NPN transistor and motor is driven from collector of that transistor where emitter is grounded..you will see opposite voltage i.e., 100% speed at 0 duty cycle and 0% speed at 100% duty cycle..

Even if you don't want to do that in hardware you can do that in software..let us know which one you prefer. If you are looking for soft one, please let us know why you are trying to get that..That will help us to make right one.
Enamul

Re: Reversed duty cycle calculation in %

Posted: Sat Aug 18, 2012 1:04 pm
by medelec35
Hi Speedy2k, you can try:
print rev duty.png
(50.82 KiB) Downloaded 3580 times
Martin

Re: Reversed duty cycle calculation in %

Posted: Sat Aug 18, 2012 2:03 pm
by Enamul
Hi,
After seeing Martin's post, I guess understand wrong about your post..you have reverse PWM already..you just want to print the duty cycle in LCD as it is...Martin's idea is nice..
for example, duty cycle=255 which you want to show 0%...(100-255*39/99)=(100-9945/99)=(100-100)=0...here 9945/99=100 because int has value only in int not possible to have 100.45 which we get in calculator.
similarly, dutycycle= 51 which you want to show 80%....(100-51*39/99)=(100-1989/99)=(100-20)=80
Thanks Martin... :)

Re: Reversed duty cycle calculation in %

Posted: Sat Aug 18, 2012 2:39 pm
by medelec35
Thanks Enamul,
Especially for those nice calculation examples :)
You are correct about int calculations.
Even if your result is 100.99999r
int will always be rounded down = 100
and not 101

Martin

Re: Reversed duty cycle calculation in %

Posted: Sun Aug 19, 2012 5:37 pm
by Speedy2k
Thanx a lot once again!! IT as done the job!!

Re: Reversed duty cycle calculation in %

Posted: Mon Aug 20, 2012 12:31 am
by medelec35
That's great!
Thanks for keeping us updated.

Martin

Re: Reversed duty cycle calculation in %

Posted: Tue Nov 13, 2012 10:57 am
by szamy
Hi,
Im beginner, and my english not really.
Im use pic12f683 , and some case i need to use reversed PWM signal.
I like to use an Variable (example "REV") ,
so:
REVbit = 1 ; pwm "normal" (HSA)
REVbit = 0 : pwm "revert" (LSA)

Please let me know, how can i do it?

Re: Reversed duty cycle calculation in %

Posted: Tue Nov 13, 2012 1:47 pm
by Benj
Hello Szamy,

It should be possible to do what you are intending by simply changing the value you are passing to the PWM duty cycle depending on if the REVbit variable is equal to 1.

decision: REVbit = 1
yes: PWM duty cycle = Duty
no: PWM duty cycle = 255 - Duty

Re: Reversed duty cycle calculation in %

Posted: Sat Nov 17, 2012 4:43 pm
by szamy
Thank You!
br. : Gabor