CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Function for variable scaling ad read pot values

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aaaaamartin



Joined: 17 Apr 2005
Posts: 39
Location: Germany Stuttgart

View user's profile Send private message

Function for variable scaling ad read pot values
PostPosted: Thu Jun 30, 2005 5:17 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Jun 30, 2005 8:00 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jul 01, 2005 4:18 am     Reply with quote

THX, works Idea Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group