PDA

View Full Version : Displaying a percentage



Scampy
- 14th August 2016, 22:00
Hi,

I have a word variable that can hold a number from 0 to 4096. I want to display the value of that variable as a percentage on an LCD. EG when it's 4096 it's 100%, when 2048 it's 50% etc. but can't think how to do it. I know that simply dividing 4096 by 100 will give me the increment value for 1%, but can't seem to format it to work correctly. From a previous bit of code I came across this which is used in a menu option to set the value of CH1_Max between 0 and 4096 by using a word variable which has a value between 0 and 100



CH1_Max = maxbright */ 10484


Any pointers ?

HenrikOlsson
- 14th August 2016, 22:08
Percent = Value ** 1600 is one option but you're pissing away a lot of resolution.

Scampy
- 14th August 2016, 22:34
Thanks Henrik,

Is there a way to get better resolution ?

alec
- 15th August 2016, 00:34
X = 4096 * 100
Percent = div32 value
Lcdout (dec2 percent)

HenrikOlsson
- 15th August 2016, 06:03
Is there a way to get better resolution ?
To get .1% is easy, same aproach:


Percent = Value ** 16000 ' Percent is now 0-1000
LCDOUT $FE, 1, Percent / 10, ".", Percent // 10

Since you've got slightly over 4000 counts the smallest unit is around 0.025% but I don't know if you need, or even want, the display to show that?

Dave
- 15th August 2016, 13:14
Or you could do it this way as there is no need to do another math operation on the variable:

Percent = Value ** 16000 ' Percent is now 0-1000
LCDOUT $FE, 1, Percent / 10, ".", Percent DIG 0

Just format the variable....

Scampy
- 15th August 2016, 17:24
many thanks guys, I'll give it a go when I'm home from work

Scampy
- 15th August 2016, 20:48
Percent = Value ** 1600 is one option but you're pissing away a lot of resolution.



pc1 = CH1_PWM ** 1600
lcdout $FE,$80,"CH1"," ",dec PC1


This works nicely, apart from one small issue, in that it won't display 100. It reached 99 and that's it !

By resolution, I didn't realise that you were referring to the decimal resolution displayed, but was referring to the maths (I assume that whatever divisions were happening the increments for each 1% would be rounded down to an integer as PBP doesn't support FP maths).

Is there a way where I can display 100 for 4095 steps ?

HenrikOlsson
- 15th August 2016, 21:28
Well, to be fair you said your value ranged from 0 to 4096, that's 4097 "steps" ;-)

pc1 = CH1_PWM ** 1601

There WILL be rounding (or truncating rather) issues, yes.

pc1 = CH1_PWM ** 16004 is better and will give you the result in steps of 0.1% as show before.

Scampy
- 15th August 2016, 22:43
Well, to be fair you said your value ranged from 0 to 4096, that's 4097 "steps" ;-)

Yeah, sorry... the PCA chip has 4096 steps, so that's 0 to 4095. - brain fade - Must be my age :tongue:

Changing the value to 1601 works a treat - Can you explain how that works ?

HenrikOlsson
- 15th August 2016, 22:57
I feel I've written about the */ and ** so many times I've lost count, it's also described in the manual, but it's apparently one of those things...so here goes:

The ** multiplies the two values you give it. This results in an intermediate 32bit result. PBP then returns the top 16bits of those 32bits. This means it does an inherent divide by 65536 so you COULD think of the ** operator as a "multiply by units of 1/65536 command".

So x = value ** 1601 is PBP lingo for x = value * 0.02443 and what's 4095*0.02443? Correct, it's 100.

How did I find 1601? Well you want an "output value" of 100 for an "input value" of 4095 so, (100/4095)*65536=1600.39 but since you really wanted it to display 100 and not 99 we had to round that up to 1601. Same thing for the 0.1% resoultion, then we need to round it up to 16004.

I hope that makes sense.
/Henrik.

EDIT: Forgot, the */ and ** works slightly different depending on if you have LONGs enabled or not. The above describes how it works when DON'T have LONGs enabled.

Scampy
- 15th August 2016, 23:14
Many thanks, and sorry for making you have to repeat yourself ;-)