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 problem

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



Joined: 28 Jul 2005
Posts: 23

View user's profile Send private message

ADC problem
PostPosted: Wed Jan 18, 2006 1:24 am     Reply with quote

Hi,
I am reading my 0-5v voltage, after conversion from the ADC. The problem is the voltage i get as a result of conversion and the actual input voltage is not the same. e.g. if the actual voltage is 1.5v the program below shows 3v. similarly if actual is 3v, adc converted is 4.8v. Ive connected a 1k pot to vary it. I dont see anything wrong in the code or in the circuitry. Plz suggests possible solutions


Code:

#include <16F877A.h>
#device *=16 ADC=8
#fuses XT,NOWDT,NOPROTECT,NOLVP
#include<stdlib.h>
#use delay(clock=4000000)
#use rs232(baud=4800,xmit=PIN_A0,rcv=PIN_B0,stream=PC,parity=N)

float factor = 0.019296875;  // 4.94/256
long value;

void main()
{   
    setup_port_a(ALL_ANALOG); //
    setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(0);
    while(1)
    {
        value = read_adc();
        fprintf(PC,"\n\rvoltage= %f", factor * (float)value);
    }
}
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Wed Jan 18, 2006 4:04 am     Reply with quote

What's the value of your pot? A/D cnverter on the PIC wants to see the impedance under 2.5k. So, either the low side of your pot should be always less then 2.5k, or your pot should be buffered with an op-amp buffer.
TIMT



Joined: 02 Sep 2005
Posts: 49
Location: Nottingham, UK

View user's profile Send private message

PostPosted: Wed Jan 18, 2006 5:09 am     Reply with quote

shouldn't :-

setup_port_a(all_analog)

be :-

setup_adc_ports(all_analog)

I could be wrong, but it may be worth trying

Regards, Tim
_________________
Tim
Humberto



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

View user's profile Send private message

PostPosted: Wed Jan 18, 2006 7:52 am     Reply with quote

I don't see any wrong in your code except that you define ADC=8 then the variable
value as a long integer, wich is inconsistent because value would never be longer than 256.
You can test the AD converter with a 10K pot without problems while keeping the
wires as short as possible.
First, I would check if the conversion is done properly with:
Code:

int value;

fprintf(PC,"\n\rADC= %d", value);
delay_ms(100);


I would add a delay in the loop to print the result, then I would apply and play the math convertion stuff.

Humberto


Last edited by Humberto on Wed Jan 18, 2006 8:02 am; edited 1 time in total
Ttelmah
Guest







PostPosted: Wed Jan 18, 2006 8:01 am     Reply with quote

One other thing I would add, is to turn off the comparators. These have a habit on some CCS versions, of being enabled at boot, and interfering with pins. setup_port_a, is an 'alias' for setup_adc_ports, and though it should work, I'd suggest switching to the standard format.
Ar you actually checking the voltage with a meter?. Otherwise, I'd be suspicious of a wiring problem somewhere.

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Jan 18, 2006 9:30 am     Reply with quote

Make sure that the result, in registers ADRESH & ADRESL, are right or left justified correctly. ADCON1 contains bit ADFM which controls this. It defaults to right justified, using bits 6-7(ADRESL) & 0-7(ADRESH) which will really mess things up if you are expecting the data to be contained in bits 0-7(ADRESL) & 0-1(ADRESH). Make sure ADFM is set to a '1' if you want to use the lower bits of the result register.

Ronald
EmbdFreak



Joined: 28 Jul 2005
Posts: 23

View user's profile Send private message

PostPosted: Thu Jan 19, 2006 1:38 am     Reply with quote

yes, im actually measuring the voltage with meter while checking my AtoD's output value. What ive noticed is, as my input voltage reaches 3v the AtoD's output reaches 255(max value for 8 bit) and the program does not respond as i higher the input voltage further (3v to 5v ). Similarly the program starts responding once i lower the value again below 3V. The lower value after which the program stops printing is about 0.7V(input voltage). Hence the program shows its output for 0.7v-3v only
Ive used 1k, 10k pots and my compiler version is 3.203

Code:

#include <16F877A.h>
#device *=16 ADC=8 // check this again
#fuses XT,NOWDT,NOPROTECT,NOLVP
#include<stdlib.h>
#use delay(clock=4000000)
#use rs232(baud=4800,xmit=PIN_A0,rcv=PIN_B0,stream=PC,parity=N)

double factor = 0.0048046875;
float factor8 = 0.019296875 ;
unsigned int value;
float res;
void main()
{     
    //setup_port_a(ALL_ANALOG); 
    SETUP_ADC_PORTS(ALL_ANALOG);
 //   setup_comparator(NC_NC_NC_NC);
    setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(0);
     while(1)
    {
        value = read_adc();
       // res = value/256;
       // res = res * 5 ;
        fprintf(PC,"\n\rvalue= %U",value);
     }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 19, 2006 2:39 am     Reply with quote

I tested your program with a 16F877A on a PicDem2-Plus board, with
PCM vs. 3.242 and 3.203. I only changed the #use rs232() statement
to use pins C6 and C7, because the board is wired for that.

It worked.

The PicDem2-Plus has a trimpot on pin RA0, and the board runs at +5v,
and I got 255 when the pot was pegged in one direction and 0 when it
was turned all the way in the other direction.

So I think you have a hardware problem:

1. The RA0 input may be partially blown. Try another channel.

2. You may have some circuit that is interfering with the operation
of the A/D. This could be a large series resistor between your
trimpot and the PIC pin. There could also be some resistor which
causes a voltage divider to exist on that pin. Measure the pin
with a voltmeter. Does it go to +5v when you peg the trimpot ?

3. You could have a drooping power supply. What is your power
supply ?
Ttelmah
Guest







PostPosted: Thu Jan 19, 2006 6:02 am     Reply with quote

One thing that could give this behaviour, is a bad Vdd connection to the processor itself. If it was only running off effectively 3v, it would give this behaviour, with the pot actually 'pulling up' the supply through the internal protection, once the voltage rises about 3.6v. Though the 'F', (as opposed to the 'LF'), is not specified to work below 4v, many will. Remember that Vdd, must be fed to both pin 11, and pin 32.

Best Wishes
EmbdFreak



Joined: 28 Jul 2005
Posts: 23

View user's profile Send private message

PostPosted: Thu Jan 19, 2006 8:52 am     Reply with quote

PCM Programmer, it has WORKED !!!
I changed the channel, so ur solution no. 1 worked right away.
Thank u all for all ur replies !!!
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