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

adc return value handling

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



Joined: 05 Nov 2007
Posts: 28
Location: Denmark

View user's profile Send private message Visit poster's website

adc return value handling
PostPosted: Sat Mar 22, 2008 11:04 am     Reply with quote

Hi everybody.

Again an irritating newbie example!

I've got a pot-meter on my adc pin on my 18f2220. As far as I can see the read_adc() returns a signed integer, correct?

For some reason this won't work:

Code:


signed int adc_val;
int16 delay_time;

adc_val = read_adc();
delay_time = 2* adc_val;
delay_ms(delay_time);



I'd expect the delay to be from 0 to 2*255 = 510 ms, but as I turn my pot-meter, my delay goes from 0-255 and then jumps back to 0, and from here from 0-255.

I think that it is me that doesn't understand how to put an int16 = a simple calculation with a signed integer.

I know that you guys appreciate posting the full code, but unfortunately it's located at my school, and I have no access to it before on tuesday.

Can anybody help me please
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 22, 2008 11:15 am     Reply with quote

Quote:
As far as I can see the read_adc() returns a signed integer, correct?

No. It's unsigned.

From the CCS manual:
Quote:
Read_adc( )

Returns: Either a 8 or 16 bit int depending on #DEVICE ADC= directive.

Note: All types, except float, by default are unsigned.
davidbue



Joined: 05 Nov 2007
Posts: 28
Location: Denmark

View user's profile Send private message Visit poster's website

PostPosted: Sat Mar 22, 2008 12:51 pm     Reply with quote

Hi PCM programmer.

Well... Then I just don't understand...

If I declare my integer as unsigned, the return value from my read_adc() call (when I send it over hyperterminal with the printf() command) is a value between -127 and 127.

Best regards
David
newguy



Joined: 24 Jun 2004
Posts: 1903

View user's profile Send private message

PostPosted: Sat Mar 22, 2008 2:15 pm     Reply with quote

That's because you're telling printf to interpret the variable as a signed int. You want to use %u in your printf statement, not %d.
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