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 CCS Technical Support

ADC Problem

 
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 Problem
PostPosted: Mon Nov 05, 2007 11:05 am     Reply with quote

Hi everybody.

Here's my problem. I've got a potentiometer on my ADC pin.

1. I read from my ADC pin
2. I'd like this reading to be a 'normal' integer value
3. I'd like to manipulate with the integer value (for example adding 300)
4. I'd like to output the manipulated value on my display.


When I read directly from my ADC pin and print the value on my LCD display (when turning the potentiometer all the way from the one end to the other) it changes like this:

-1 moving towards -128 and then the value jumps to 127 and moves towards 0.

Why dosen't I just get a value from 0-255? (that's what I want)

When I try to subtract or add a constant to my ADC reading, it acts even stranger.


For example if i add the line:

pot_value = pot_value +300

-just after reading the ADC value, the behaviour is like this:

44 moving towards -128 and then the value jumps to 127 and moves towards 43.



Let's say I override my ADC reading. Instead of the line:

pot_value = read_adc();

I type:

pot_value = 200;

-Then the I get a proper reading on my display, and when I add 300:

pot_value = pot_value +300

- I get a reading of 500.


What am I doing wrong?

Code:



#include "C:\pickit2_kode\lcd1_test\take1.h"
#include "C:\pickit2_kode\lcd1_test\LCD.C"

void main()
{
 
int pot_value = 0;

   setup_oscillator(OSC_8MHZ|OSC_INTRC);
   setup_adc_ports(AN0|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);

   lcd_init();

lcd_gotoxy(1,1); //pos 1 row 1
lcd_putc("ADC Test take1");



delay_ms(2000);



while(1){
lcd_gotoxy(1,1);



pot_value = read_adc();


printf(LCD_PUTC, "value=%d    ",pot_value);
delay_ms(100);
}

}


rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Mon Nov 05, 2007 11:17 am     Reply with quote

Make sure your variable is an unsigned int16 so it has the full 10bit range.

Look in the spec. sheet regarding justification of the ADC result registers. It takes two registers to make the 10bit value. It can be left or right justified. This will make a huge diference in your results. You might need to use make16() to combine the two.

Ronald
davidbue



Joined: 05 Nov 2007
Posts: 28
Location: Denmark

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

Hi Ronald
PostPosted: Mon Nov 05, 2007 11:24 am     Reply with quote

I don't think I understand what you're explaining me.

Can you write a quick mock-up code example?

Best regards
David
davidbue



Joined: 05 Nov 2007
Posts: 28
Location: Denmark

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

PostPosted: Mon Nov 05, 2007 11:26 am     Reply with quote

By the way. It's a 18F2220
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 05, 2007 11:41 am     Reply with quote

Quote:
1 moving towards -128 and then the value jumps to 127 and moves towards 0.
Why dosen't I just get a value from 0-255? (that's what I want)

printf(LCD_PUTC, "value=%d ",pot_value);

Download the CCS manual:
http://www.ccsinfo.com/downloads/CReferenceManual.pdf
Look in the printf section at the list of format specifiers that are
used to display the various data types. Then look at what you're
using in the printf statement above.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Nov 05, 2007 11:47 am     Reply with quote

Quote:

2. I'd like this reading to be a 'normal' integer value

As Ronald told you, make sure your variable is an unsigned int16 so it has the full 10bit
range, according to your readings it seems like you are dealing with an 8 bit signed integers.

Quote:

3. I'd like to manipulate with the integer value (for example adding 300)

You can do it defining the variable -that hold the converted result- as an unsigned long

Quote:

Why dosen't I just get a value from 0-255? (that's what I want)

In CCS:
- a signed int goes from -127 to +127
- an unsigned int goes from 0 to 255

In the CCS Manual you will find all this info. Read the Data Definitions chapter.


Humberto
davidbue



Joined: 05 Nov 2007
Posts: 28
Location: Denmark

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

Thanks JDM programmer
PostPosted: Mon Nov 05, 2007 11:53 am     Reply with quote

I'm using #device adc=8

-But what if I want to convert my ADC reading into a new variable being an Unsigned int?

Is that possible - how do I do that.

I'm sorry. I'm really a newbie.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 05, 2007 12:33 pm     Reply with quote

Read Humberto's post. In CCS an 'int' is an unsigned 8-bit variable.
Also, it's not "JDM" programmer.
davidbue



Joined: 05 Nov 2007
Posts: 28
Location: Denmark

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

Sorry PCM programmer
PostPosted: Mon Nov 05, 2007 12:42 pm     Reply with quote

Sorry PCM programmer.!

Just had JDM programmer in my head.... My fault. Thank you for the reply.
davidbue



Joined: 05 Nov 2007
Posts: 28
Location: Denmark

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

Horray!
PostPosted: Mon Nov 05, 2007 2:07 pm     Reply with quote

I've solved my problem.

I just had to declare my integer as signed. That did the trick.

Thank you everybody!

Regards
David
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