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

Confused - Analog to Digital

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







Confused - Analog to Digital
PostPosted: Fri Jun 08, 2007 2:51 pm     Reply with quote

I wrote code for a PIC to do Analog to Digital and to display the result on my LCD. I have provided my code below.

When I connect a potential meter to my AN0 input, and vary the voltage from 0 - 5V, it works perfectly fine. I even confirmed with my volt meter.

However the problem I have is with reading another set of analog values. These values come from a motorolla mpx10D pressure sensor which produces values in the mV range. I amplify these values with a 741N opamp so that the voltage entering the pic is in the 0.7 - 1.2V range.

I check the values entering the PIC AN0 with my volt meter and it reads them to be about 0.70V - 1.2V. But the values my analog to digital conver captures and displays on the LCD keep fluctuating by a lot. So for example, the volt meter will output 0.70, 0.71, 0.72, 0.70, where as my PIC will output to the LCD 0.42, 0.81, 0.54, 1.10, 0.40.

What could be the problem? Could the opamps be doing something, but then why is my volt meter reading a stable value? I could really use some help with this one. Thanks.


Code:

#include <16F877A>
#device ADC=10
#use delay(clock=20000000)

#include <flex_lcd.c>

void main ()
{
unsigned long value;
float voltage;
set_tris_b(0);
set_tris_a(1);
set_tris_d(0);

setup_adc_ports(00000011);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);   
delay_us(10);

lcd_init();

while(1)
{
value=read_adc();
//output_B(value);
voltage = (float)value*(5.0/1023.0);
delay_ms(1);
printf(lcd_putc,"\fVolt is %f",voltage); 
delay_ms(1000);
}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 08, 2007 3:07 pm     Reply with quote

Quote:
setup_adc_ports(00000011);

Don't use "magic numbers" for the CCS function parameters.
Use the constants that are given in the 16F877A.H file.
Also, your number above is interpreted as Octal format.
In decimal, it's equivalent to 9. Hardly anyone in the C world
uses Octal. We use either Hex or Decimal (or maybe binary).
gammla



Joined: 08 Jun 2007
Posts: 3

View user's profile Send private message

PostPosted: Fri Jun 08, 2007 5:55 pm     Reply with quote

Hello!
Try to lay a resistor around 10kOhm or higher between your output of the opamp and your adc input to ground.
This should solve your problem!

best regards,

gammla
ElectricalNoob



Joined: 17 May 2007
Posts: 15

View user's profile Send private message

THEPICUser
PostPosted: Fri Jun 08, 2007 10:36 pm     Reply with quote

Thank you PCM Programmer and gammla. I'm going to try the resister idea.

When it comes to practical applications, I am really new at this. I'm trying to learn as much as I can in the shortest amount of time. I'm still puzzled with little things like. When my AN0 analog input is in a continuous reading loop, and nothing is connected to the pin. The results of the analog to digital conversion will fluctuate. I don't understand the reason for this. But it seems like it might be similar to the reason why my pressure sensor has fluctuating problem.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 08, 2007 11:55 pm     Reply with quote

Quote:

When my AN0 analog input is in a continuous reading loop, and nothing is
connected to the pin, the results of the analog to digital conversion will
fluctuate. I don't understand the reason for this.

Connect a 47K pull-down resistor to the input capacitor of your op-amp
circuit. That way, when no signal is present, the input will be at 0 volts.
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