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

need help in GP2D12 analog sensor

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



Joined: 19 Apr 2008
Posts: 7

View user's profile Send private message

need help in GP2D12 analog sensor
PostPosted: Sat Apr 19, 2008 11:10 am     Reply with quote

dear all,
I am writing a code that will configure gp2d12 sharp sensor so that i can read the value from this sensor and then display it on an lcd as a distance
the idea is this sensor will give analog output between (0.4-2.4)volts
and then i need to convert this voltage into distance and display it on LCD
any one can help me
thanx for every one
Code:
#include <16F877.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#include <lcd.c>

//============================
void main()
{
int16 adc_value;
float volts;

lcd_init();

setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);

while(1)
  {
   adc_value = read_adc();            //  adc_value is digital
   volts = (float)(adc_value * 5)/1023.0;  // convert from digital to analog
   printf(lcd_putc, "\f%3.2f", volts);
   delay_ms(50);
  }
}

when i run the above code and after connecting the sensor to my circuit i always get a varying reading I am using pic16f877a
eng.khlaif



Joined: 19 Apr 2008
Posts: 7

View user's profile Send private message

PostPosted: Sun Apr 20, 2008 2:19 am     Reply with quote

dear all
more than 30 views and no one post reply is that very difficult
any way thanx for every thing
Ttelmah
Guest







PostPosted: Sun Apr 20, 2008 3:07 am     Reply with quote

Unfortunately, a lot of question will be needed...
Start with the stability of the signal itself from the sensor. Have you got the decoupling capacitor they specify?. How carefully is the signal line from the sensor laid out?. Can you verify that the output _does_ meet the impedance specifications for the PIC ADC input (they don't give the output impedance in the data sheet...).
Now, you appear to be using the PIC's Vdd as the Vref supply. How stable is this?. What smoothing is present close to the PIC?. What other sources of electrical noise are present on the board?. Given you are using this voltage, one step on the ADC, will be about 0.8cm at the far end of the output range. Given the sampling noise, the signal is _never_ going to be totally stable...

Start by supply the ADC, with an external 2.5v Vref, with a 270R resistor to 5v, and a 0.1uF ceramic capacitor to Vss, placed nice and close to the PIC.
Then ensure that the PIC circuit, and the sensor, have a good 'common' ground, and that nothing drawing any significant current draws it 'via' their rails.
Ensure you have good supply decoupling close to the PIC, and close to the sensor.
Then perform some simple averaging on the value read. Do this using integers to void wasting time in FP maths. So:
Code:

   int16 adc_value;
   float volts;
   int16 adc_sum=0;

   lcd_init();

   setup_adc_ports(AN0 | Vss_Vref);
   setup_adc(ADC_CLOCK_DIV_8);
   set_adc_channel(0);
   delay_us(20);

   while(1)  {
      adc_sum+= read_adc();            //  adc_value is digital
      adc_value=adc_sum/8;
      adc_sum-=adc_value;
      volts = ((adc_value * 2.5)/1024.0)+0.00122; 
      printf(lcd_putc, "\f%4.2f", volts);
      delay_ms(50);
   }

Expect some variation still (the reading from this type of sensor, _will_change a little as ambient light changes etc.), but it should be several orders of magnitude 'better'.
Look here for why the division should be by 1024, not 1023 (have posted a long 'reason' here only in the last couple of days..), and the offset added.

Best Wishes
eng.khlaif



Joined: 19 Apr 2008
Posts: 7

View user's profile Send private message

PostPosted: Mon Apr 21, 2008 2:11 pm     Reply with quote

dear
What i want to say that the ADC i used is inside the PIC itself and it is not external part.
So how can i supply it with Vref 2.5v and where to connect the capacitors
my circuit contains GP2D12 analog sensor. It has three terminal GND and VCC (5v) and the output and i connect it to pin A0 and i have developed the code shown before.
thanx
Ttelmah
Guest







PostPosted: Mon Apr 21, 2008 3:33 pm     Reply with quote

Read the chip's data sheet.
You can program AN3, to be used as the Vref+ for the ADC. The option 'Vss_Vref' enables this in the ADC setup.
Decouple the Vref, and supply lines to ground, close to the chips.
This is _essential_ for reliable operation of just about any chip.
Study something like a PC motherboard, and you will find literally hundreds of capacitors across the board. Remember they don't include _anything_ they can get away without....

Best Wishes
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