View previous topic :: View next topic |
Author |
Message |
davidbue
Joined: 05 Nov 2007 Posts: 28 Location: Denmark
|
adc return value handling |
Posted: Sat Mar 22, 2008 11:04 am |
|
|
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
|
|
Posted: Sat Mar 22, 2008 11:15 am |
|
|
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
|
|
Posted: Sat Mar 22, 2008 12:51 pm |
|
|
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: 1907
|
|
Posted: Sat Mar 22, 2008 2:15 pm |
|
|
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. |
|
|
|