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

A/D Conversion problem...

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



Joined: 02 Dec 2005
Posts: 28
Location: Italy, Milan

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

A/D Conversion problem...
PostPosted: Thu Feb 09, 2006 3:08 pm     Reply with quote

Code:

#include <16F877A.h>
#device ADC=10

....

// Setup ADC Port
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL );

....

set_adc_channel(6);
lValue = read_adc();


Now, connecting AN6 to +5V I get a value of 2.53V (lValue = 0x208) or using a trimmpot I get values from 0 to 2,53V... any idea??


Alex
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 09, 2006 3:26 pm     Reply with quote

Sample A/D program:
http://www.ccsinfo.com/forum/viewtopic.php?t=23195

Discussion of how a partially blown input can affect A/D readings:
http://www.ccsinfo.com/forum/viewtopic.php?t=25692
[mAnNaRo]



Joined: 02 Dec 2005
Posts: 28
Location: Italy, Milan

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

PostPosted: Thu Feb 09, 2006 3:38 pm     Reply with quote

PCM I've read...
But... I've cutted the code from my long project and pasted in a new. It work fine! Sad because with the same hardware I get 3FE for +5V (on channel 6).
Now... In the long project I use also channels 0 and 1 and they work fine... but adc on channel 6 does't work. What kind of code can interfere with adc on this???

Alex
[mAnNaRo]



Joined: 02 Dec 2005
Posts: 28
Location: Italy, Milan

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

PostPosted: Thu Feb 09, 2006 3:45 pm     Reply with quote

Another experiment.

The simple code below...
Code:

void main() {
   int16 value;

   // Setup ADC Port
   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL );

   while(TRUE) {
           set_adc_channel(0);
      value = read_adc();
      printf("Value 0 = %LX\r\n", value);
      set_adc_channel(1);
      value = read_adc();
      printf("Value 1 = %LX\r\n", value);
      set_adc_channel(6);
      value = read_adc();
      printf("Value 6 = %LX\r\n", value);
   }
}


give me the same result as my long project!!! Channel 0 and 1 are correct but channel 6 for +5V give me 0x208! Confused any idea PCM?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 09, 2006 4:05 pm     Reply with quote

When you change the A/D channel, you should put in a small
delay after that, to allow time for the holding capacitor to charge up.
(This holding capacitor is inside the 16F877A chip).

Add the lines shown in bold below:
Quote:
while(TRUE) {
set_adc_channel(0);
delay_us(20);
value = read_adc();
printf("Value 0 = %LX\r\n", value);

set_adc_channel(1);
delay_us(20);
value = read_adc();
printf("Value 1 = %LX\r\n", value);

set_adc_channel(6);
delay_us(20);
value = read_adc();
printf("Value 6 = %LX\r\n", value);
}


Also, if one channel doesn't reach the proper value, it could be
that the impedance of the source is too high. What device is driving
channel 6 ? Do you have a series resistor between the device
and the PIC pin for channel 6 ? If so, what value is it ?
In section 11.1 of the 16F877A data sheet, it says:
Quote:
The maximum recommended
impedance for analog sources is 2.5 k.

Notice that this is quite a bit lower than for the 16F877, which
recommends 10K as the allowable maximum. So I suspect your
problem is due to excessive input impedance for that channel.
[mAnNaRo]



Joined: 02 Dec 2005
Posts: 28
Location: Italy, Milan

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

PostPosted: Thu Feb 09, 2006 4:16 pm     Reply with quote

Thanx PCM, the problem was the delay_us(20) after channel change! Smile

Alex
crushneck



Joined: 27 Nov 2005
Posts: 29

View user's profile Send private message MSN Messenger

PostPosted: Sat Feb 11, 2006 11:12 am     Reply with quote

Hello there. Dear PCM programmer. I have a similar project. Just wondering how can i edit it if i wanted to program it whenever a keypad is pressed, the selected output will display on a LCD?
JBM



Joined: 12 May 2004
Posts: 54
Location: edinburgh, Scotland

View user's profile Send private message

PostPosted: Sat Feb 11, 2006 12:19 pm     Reply with quote

Crushneck:

Have a look in the examples folder to see how to interface to keypads and LCDs. You could then call set_adc_channel(keypad_data), delay_us(20) and read_adc() to set and read the ADC channel. Then output the data to the LCD using the funcitons defined in the LCD library.

Have a poke around and see how you get on.
-JBM
crushneck



Joined: 27 Nov 2005
Posts: 29

View user's profile Send private message MSN Messenger

PostPosted: Sun Feb 12, 2006 9:45 pm     Reply with quote

Quote:
Have a look in the examples folder to see how to interface to keypads and LCDs. You could then call set_adc_channel(keypad_data), delay_us(20) and read_adc() to set and read the ADC channel. Then output the data to the LCD using the funcitons defined in the LCD library.


DEar JBM. how can i put the set_adc_channel(keypad_data); to my codes? I tried to put it but theres an error. Then i try to assume that u mean it this way. Can u check my code? Is this the right way of putting it?COs i did not get the output.

Code:

#include <lCD420.c>
#include <kbd.c>

void main() {
char k;
long int result;
long int result2;
float adcresult;
float adcresult2;
port_b_pullups(TRUE);

lcd_init();
kbd_init();

SETUP_ADC_PORTS(AN0_AN1_VSS_VREF);
setup_adc(ADC_CLOCK_DIV_8);

while (TRUE) {


      lcd_gotoxy(1,1);
      printf(lcd_putc, ">>ADC<<", adcresult);

      k=kbd_getc();

      set_adc_channel(k==1);//press '1' for a voltage output
      set_adc_channel(0);
      delay_us(20);
      result = read_adc();
      adcresult = (float)result * (5.0/1024.0);
      lcd_gotoxy(1,3);
      printf(lcd_putc, "ADC (V): %5.3fV", adcresult);


      set_adc_channel(k==2);//press '2' for a current output
      set_adc_channel(1);
      delay_us(20);
      result2 = read_adc();
      adcresult2 = (float)result2 * (3.0/1024.0);
      lcd_gotoxy(1,2);
      printf(lcd_putc, "ADC (I): %5.2fA", adcresult2);

   }
}
JBM



Joined: 12 May 2004
Posts: 54
Location: edinburgh, Scotland

View user's profile Send private message

PostPosted: Mon Feb 13, 2006 4:08 pm     Reply with quote

Ok, I see where you're going with your code, but there are a few "issues" with it.

At the moment, whenever you evaluate set_adc_channel(k==2), the (k==2) will evaluate to a boolean (true or false) answer. So if k is equal to 2, then that's true - it will pass a 1 to the set_adc_channel funtion, which really isn't what you're looking for. What you need to do is use 2 if statements which test if k is equal to 1 or 2:

Code:


while (TRUE) {

     lcd_gotoxy(1,1);                     //see note about this bit below
      printf(lcd_putc, ">>ADC<<", adcresult);

if(k==1){//press '1' for a voltage output
      set_adc_channel(0);
      delay_us(20);
      result = read_adc();
      adcresult = (float)result * (5.0/1024.0);
      lcd_gotoxy(1,3);
      printf(lcd_putc, "ADC (V): %5.3fV", adcresult);
}

if(k==2){//press '2' for a current output
      set_adc_channel(1);
      delay_us(20);
      result2 = read_adc();
      adcresult2 = (float)result2 * (3.0/1024.0);
      lcd_gotoxy(1,2);
      printf(lcd_putc, "ADC (I): %5.2fA", adcresult2);
}


}


Another thing is that the first time you run the loop, there will be no meaningful value in adcresult, simply because you havne't put anything in it yet. You'll probably want to have a look at that.

Let me know how it goes
-JBM
crushneck



Joined: 27 Nov 2005
Posts: 29

View user's profile Send private message MSN Messenger

PostPosted: Tue Feb 14, 2006 12:24 am     Reply with quote

dear JBM,i still cannot get the oputput i want after using ur given code. My lcd shows only the first line '>>ADC<<' . nothing happen when i press my keypad. Just for ur info, my keypad and LCD is working properly when i use the CCS example for keypad and lcd interface.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 14, 2006 12:38 am     Reply with quote

Quote:
if(k==1) { //press '1' for a voltage output

Look in the KBD.C driver file. It has a two-dimensional array
of values that it returns. What type of values does it return ?
What type of value are you comparing it to, in the statement
above ? What should you be comparing it to instead ?
Hint: The answer is in your comment.
2nd hint: Watch the details.
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