Reversed duty cycle calculation in %

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Speedy2k
Posts: 91
Joined: Mon Aug 06, 2012 5:04 am
Has thanked: 2 times

Reversed duty cycle calculation in %

Post 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!

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Reversed duty cycle calculation in %

Post 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
Martin

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times

Re: Reversed duty cycle calculation in %

Post 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
Enamul
University of Nottingham
enamul4mm@gmail.com

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Reversed duty cycle calculation in %

Post by medelec35 »

Hi Speedy2k, you can try:
print rev duty.png
(50.82 KiB) Downloaded 3580 times
Martin
Martin

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times

Re: Reversed duty cycle calculation in %

Post 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... :)
Enamul
University of Nottingham
enamul4mm@gmail.com

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Reversed duty cycle calculation in %

Post 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
Martin

Speedy2k
Posts: 91
Joined: Mon Aug 06, 2012 5:04 am
Has thanked: 2 times

Re: Reversed duty cycle calculation in %

Post by Speedy2k »

Thanx a lot once again!! IT as done the job!!

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Reversed duty cycle calculation in %

Post by medelec35 »

That's great!
Thanks for keeping us updated.

Martin
Martin

szamy
Posts: 10
Joined: Fri Sep 09, 2011 6:44 am

Re: Reversed duty cycle calculation in %

Post 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?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Reversed duty cycle calculation in %

Post 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

szamy
Posts: 10
Joined: Fri Sep 09, 2011 6:44 am

Re: Reversed duty cycle calculation in %

Post by szamy »

Thank You!
br. : Gabor

Post Reply