Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 10, 2016, 09:43:52 09:43


Login with username, password and session length


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

Posts: 437

Thank You
-Given: 140
-Receive: 124



WWW
« on: June 25, 2010, 07:20:03 19: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: 840

Thank You
-Given: 312
-Receive: 387



WWW
« Reply #1 on: June 25, 2010, 08:12:50 20: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: 437

Thank You
-Given: 140
-Receive: 124



WWW
« Reply #2 on: June 25, 2010, 08:27:05 20: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, 08:53:51 20: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: 371

Thank You
-Given: 219
-Receive: 567


« Reply #3 on: June 25, 2010, 10:32:34 22: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: 840

Thank You
-Given: 312
-Receive: 387



WWW
« Reply #4 on: June 25, 2010, 10:35:06 22: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: 437

Thank You
-Given: 140
-Receive: 124



WWW
« Reply #5 on: June 25, 2010, 11:25:22 23: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: 325

Thank You
-Given: 38
-Receive: 1633


« Reply #6 on: June 25, 2010, 11:30:06 23: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: 267

Thank You
-Given: 223
-Receive: 553



« Reply #7 on: June 26, 2010, 02:49:48 02: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 26, 2010, 05:43:09 05: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, 05:36:20 17: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, 05:38:46 17: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