Reversed duty cycle calculation in %
Reversed duty cycle calculation in %
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!
-
- 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 %
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
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
- 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 %
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
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
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- 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 %
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...
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...

-
- 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 %
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
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
-
- 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 %
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?
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?
- 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 %
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
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
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel