Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 26, 2024, 05:34:50 17:34


Login with username, password and session length


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

Posts: 442

Thank You
-Given: 167
-Receive: 149



« on: June 25, 2010, 06:20:03 18: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
Ichan
Hero Member
*****
Offline Offline

Posts: 833

Thank You
-Given: 312
-Receive: 392



WWW
« Reply #1 on: June 25, 2010, 07:12:50 19: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: 442

Thank You
-Given: 167
-Receive: 149



« Reply #2 on: June 25, 2010, 07:27:05 19: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, 07:53:51 19:53 by sohel » Logged
zuisti
Senior Member
****
Offline Offline

Posts: 409

Thank You
-Given: 242
-Receive: 780


« Reply #3 on: June 25, 2010, 09:32:34 21: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: 833

Thank You
-Given: 312
-Receive: 392



WWW
« Reply #4 on: June 25, 2010, 09:35:06 21: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: 442

Thank You
-Given: 167
-Receive: 149



« Reply #5 on: June 25, 2010, 10:25:22 22: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
Top
Senior Member
****
Offline Offline

Posts: 339

Thank You
-Given: 52
-Receive: 1719


« Reply #6 on: June 25, 2010, 10:30:06 22: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: 300

Thank You
-Given: 273
-Receive: 593



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