View previous topic :: View next topic |
Author |
Message |
aaaaamartin
Joined: 17 Apr 2005 Posts: 39 Location: Germany Stuttgart
|
Function for variable scaling ad read pot values |
Posted: Thu Jun 30, 2005 5:17 pm |
|
|
Dear fellows,
working on 16F877 // Compiler:3.219
I'm doing 8-Bit A/D Conversion and have trouble getting a scale function to work. Due to integer division it returns zero all the time.
Here's the code
Code: |
int p_read (int pnum, int psca)
{
int pret=0;
int pmes=0;
set_adc_channel(pnum);
delay_us(20);
pmes=read_adc();
delay_us(20);
pret=( pmes * ( (psca-1) / 255) );
return pret;
} |
I need to have results given ranging from 0 to (scale-1).
Scale can be anything in between 5 to 256, expect 0 of course.
Later on I need to extend the function, to be given a varible start and end value, resulting from calibration, that is done beforehand.
These two values derive from mechanic limitation of the pots rotation angle. i.e. never going down to zero or up to 255.
So far I haven't found what I'm looking, neither by doing it on my own
nor by looking up other posts.
Let's do the math, thanks , Martin |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Jun 30, 2005 8:00 pm |
|
|
Code: |
int
p_read (int pnum, int psca)
{
int pret=0;
int16 pmes=0;
set_adc_channel(pnum);
delay_us(20);
pmes=read_adc();
delay_us(20);
pmes *= (psca-1);
pret = pmes / 255;
return pret;
}
|
|
|
|
aaaaamartin
Joined: 17 Apr 2005 Posts: 39 Location: Germany Stuttgart
|
|
Posted: Fri Jul 01, 2005 4:18 am |
|
|
THX, works |
|
|
|