The Godfather talking
Share your stuff or I will make you regret it.
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2018, 12:06:27 00:06


Login with username, password and session length


Pages: [1]
Print
Author Topic: C code snippets for get percentage  (Read 3070 times)
0 Members and 1 Guest are viewing this topic.
sohel
Senior Member
****
Offline Offline

Posts: 438

Thank You
-Given: 148
-Receive: 132



WWW
« on: June 25, 2010, 12:20:03 12:20 »

please help me to get reduce code and time for simple percent = (value/256)*100


its work fine but i want to using pointer for divide and multiply.

thanks
Masud

Logged

A Thousand Miles Journey Start With a Single Step<br />http://www.youtube.com/user/masud58
Ichan
Hero Member
*****
Offline Offline

Posts: 837

Thank You
-Given: 312
-Receive: 387



WWW
« Reply #1 on: June 25, 2010, 01:12:50 13:12 »

What compiler? A compiler with good optimizing will handle it perfectly as long as the variables are integers.

I do not know about using pointer, but percent = (value * 100) >> 8 should give an efficient code. You may need to find out how is the compiler used handles the integral promotion.

-ichan
Logged

There is Gray, not only Black or White.
sohel
Senior Member
****
Offline Offline

Posts: 438

Thank You
-Given: 148
-Receive: 132



WWW
« Reply #2 on: June 25, 2010, 01:27:05 13:27 »

Thanks for faster answer.

I am using HI-TECH C for 16F

i want to show battery % without compiler builtin (*, /)
i want to use shifting bit or other tricks.

Read ad from battery. then convert it %.

like when ad = 217, its shows 100%

when ad = 156 its show 0%

that % value should be range 61.

thanks

« Last Edit: June 25, 2010, 01:53:51 13:53 by sohel » Logged

A Thousand Miles Journey Start With a Single Step<br />http://www.youtube.com/user/masud58
zuisti
Senior Member
****
Offline Offline

Posts: 388

Thank You
-Given: 227
-Receive: 680


« Reply #3 on: June 25, 2010, 03:32:34 15:32 »

i want to show battery % without compiler builtin (*, /)
Read ad from battery. then convert it %.
like when ad = 217, its shows 100%
when ad = 156 its show 0%
that % value should be range 61.

Hi sohel;
The simple math (using your example above):
ADx is your measured value (between 156 and 217)

BAT% = (ADx - 156) * 100/61  // using int math
or
BAT% = (ADx - 156) * 1.6393 // float 100/61 = 1.6393

At my mind it's impossible without compiler builtins.

Cheers
zuisti
Logged
Ichan
Hero Member
*****
Offline Offline

Posts: 837

Thank You
-Given: 312
-Receive: 387



WWW
« Reply #4 on: June 25, 2010, 03:35:06 15:35 »

Try this:

Code:
#include <htc.h>

#define VMIN 156
#define VMAX 217
#define VMUL ((100<<8)/(VMAX-VMIN))


unsigned char calc_percent(unsigned char val) {
val -= VMIN;
return ((val * VMUL) >> 8);
}

main() {
unsigned char adval, percent;
adval = 200;
percent = calc_percent(adval);
}

This code still use built-in multiplication and division but only took 49 bytes of program memory. Make sure that adval is in the range of VMIN to VMAX.

-ichan
Logged

There is Gray, not only Black or White.
sohel
Senior Member
****
Offline Offline

Posts: 438

Thank You
-Given: 148
-Receive: 132



WWW
« Reply #5 on: June 25, 2010, 04:25:22 16:25 »

You skip div rutine

but one problem is when it adval = 217 then it shows 99%, all value are ok.

many many
thanks
Logged

A Thousand Miles Journey Start With a Single Step<br />http://www.youtube.com/user/masud58
Top
Senior Member
****
Offline Offline

Posts: 333

Thank You
-Given: 44
-Receive: 1665


« Reply #6 on: June 25, 2010, 04:30:06 16:30 »

If you don't calculate it at run time then you have to use a lookup table.

It would be a very good idea to add range checking to this code.

Code:
#include  <htc.h>

#define VMIN    156
#define VMAX    217

const unsigned char p_table[62] = { 0,  2,  3,  5,  7,  8, 10, 11,
                                   13, 15, 16, 18, 20, 21, 23, 25,
                                   26, 28, 30, 31, 33, 34, 36, 38,
                                   39, 41, 43, 44, 46, 48, 49, 51,
                                   52, 54, 56, 57, 59, 61, 62, 64,
                                   66, 67, 69, 70, 72, 74, 75, 77,
                                   79, 80, 82, 84, 85, 87, 89, 90,
                                   92, 93, 95, 97, 98, 100};

main() {
  unsigned char adval, percent;
  adval = 200;
  percent = p_table[adval - VMIN];
}
Logged
LabVIEWguru
Senior Member
****
Offline Offline

Posts: 284

Thank You
-Given: 239
-Receive: 566



« Reply #7 on: June 25, 2010, 07:49:48 19:49 »

How about knowing ADC(counts) = .xx voltage
You already know MAX and MIN
do subsequent subtractions to get to MIN and count the subtractions of ADC(counts) to get there.

Like MAX = 100, MIN = 70, ADC(counts) (7)  = .1 volts
so: when you sample, you have 49 counts, 49 - 7 (7 times) = 0: 7 x .1 = .7 volts
Then subtract from max

I did this with a 6809 a long time ago
« Last Edit: June 25, 2010, 10:43:09 22:43 by LabVIEWguru » Logged
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #8 on: July 07, 2010, 10:36:20 10:36 »

I guess You only want to use "stdio.h". Too much overhead with "math.h"(or whatever Yr. compiler call it).
Right? Wink
« Last Edit: July 07, 2010, 10:38:46 10:38 by robban » Logged

Code Warrior
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC