PDA

View Full Version : Percentage



Scampy
- 1st July 2017, 21:58
Guys, I'm having a brain fart and for the life of me can't get an LCD to display the value of a word variable as a percentage.

The word variable is CH1_PWM and ranges from 0 to 4095

I tried this



If (CH1_PWM * 100)/4095 >= 100 then
lcdout $FE,$80,"CH1 ",$FE,$80+4,dec3 (CH1_PWM *100)/4095,"%"
endif

If (CH1_PWM * 100)/4095 <=0 or (CH1_PWM * 100)/4095 <10 then
lcdout $FE,$80,"CH1 ",$FE,$80+4,dec1 (CH1_PWM *100)/4095,"% "
endif

if (CH1_PWM * 100)/4095 >=10 and (CH1_PWM * 100)/4095 <100 then
lcdout $FE,$80,"CH1 ",$FE,$80+4,dec2 (CH1_PWM *10)/4095,"% "
endif



I know there must be a simple way of doing this, but it's been a long day and my brain just refuses to think straight !! I have a menu option that kind of works in reverse as it it uses a byte variable (maxbright) that ranges from 0 to 100 and it is used to set a word variable (in this case CH1_Max)


CH1_Max = maxbright */ 10484


But not sure how to compliment it the other way into the LCD display.

Malc

HenrikOlsson
- 2nd July 2017, 08:36
One way:

Percent = Ch1_PWM ** 1601

/Henrik.

Scampy
- 2nd July 2017, 11:51
Thanks Henrik, that works a treat.

Can you explain the maths behind the ** and 1601 ?

richard
- 2nd July 2017, 12:09
its the same as last time
http://www.picbasic.co.uk/forum/showthread.php?t=21204

HenrikOlsson
- 2nd July 2017, 13:21
The deja vu feeling is overwhelming, thanks Richard for digging up that thread :-)
The */ and ** operators have been covered many many times so yeah, read up on your own previous thread that Richard pointed to and I'll explain it there(post #11) :-)

/Henrik.

Scampy
- 2nd July 2017, 16:39
:o

Sorry guys....