View previous topic :: View next topic |
Author |
Message |
davidbue
Joined: 05 Nov 2007 Posts: 28 Location: Denmark
|
ADC Problem |
Posted: Mon Nov 05, 2007 11:05 am |
|
|
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
|
|
Posted: Mon Nov 05, 2007 11:17 am |
|
|
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
|
Hi Ronald |
Posted: Mon Nov 05, 2007 11:24 am |
|
|
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
|
|
Posted: Mon Nov 05, 2007 11:26 am |
|
|
By the way. It's a 18F2220 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 05, 2007 11:41 am |
|
|
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
|
|
Posted: Mon Nov 05, 2007 11:47 am |
|
|
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
|
Thanks JDM programmer |
Posted: Mon Nov 05, 2007 11:53 am |
|
|
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
|
|
Posted: Mon Nov 05, 2007 12:33 pm |
|
|
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
|
Sorry PCM programmer |
Posted: Mon Nov 05, 2007 12:42 pm |
|
|
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
|
Horray! |
Posted: Mon Nov 05, 2007 2:07 pm |
|
|
I've solved my problem.
I just had to declare my integer as signed. That did the trick.
Thank you everybody!
Regards
David |
|
|
|