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

GP2D12 sensor problem urgent need !!!!!!!!!

 
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

GP2D12 sensor problem urgent need !!!!!!!!!
PostPosted: Tue Apr 29, 2008 8:38 am     Reply with quote

iam using the following sample code that will read the voltage from sensor( this sensor gives analog output) then i want to display it on and LCD
but when i connect the circuit with the sensor it always gives reading with out moving any object toward the sensor ?(GP2D12) sensor
Code:
#include <16F877a.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#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();
   volts = (float)(adc_value * 5)/1024.0;   
   printf(lcd_putc, "\f%3.2f", volts);
   delay_ms(500);
  }
}
Matro
Guest







PostPosted: Tue Apr 29, 2008 8:41 am     Reply with quote

I'm not sure to really understand the problem. But from what I do, you complain that the ADC gives a result even if there is nothing moving close to the sensor.
That's normal, you ask the ADC for a value and it will give you one, whatever could be the sensor state.

Matro
eng.khlaif



Joined: 19 Apr 2008
Posts: 7

View user's profile Send private message

PostPosted: Tue Apr 29, 2008 1:36 pm     Reply with quote

Matro wrote:
I'm not sure to really understand the problem. But from what I do, you complain that the ADC gives a result even if there is nothing moving close to the sensor.
That's normal, you ask the ADC for a value and it will give you one, whatever could be the sensor state.

Matro

exactly it gives a reading without ask it
what i need is i want a reading just when the object is moving
so pls how can i modify the code so that it gives my reading based just on the sensor reading
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Tue Apr 29, 2008 1:43 pm     Reply with quote

eng.khlaif wrote:
...how can i modify the code so that it gives my reading based just on the sensor reading


Well, what voltage change does the sensor produce when something moves? You could compare the A/D reading with a previous reading and print the result only if the reading is changed enough.

Robert Scott
Real-Time Specialties
eng.khlaif



Joined: 19 Apr 2008
Posts: 7

View user's profile Send private message

PostPosted: Tue Apr 29, 2008 3:19 pm     Reply with quote

RLScott wrote:
eng.khlaif wrote:
...how can i modify the code so that it gives my reading based just on the sensor reading


Well, what voltage change does the sensor produce when something moves? You could compare the A/D reading with a previous reading and print the result only if the reading is changed enough.

Robert Scott
Real-Time Specialties

the sensor will read produce a value between 0.41-2.4 volts
what i want to say is from where does it display the reading while i am not moving any objects toward it
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Tue Apr 29, 2008 7:25 pm     Reply with quote

eng.khlaif wrote:

the sensor will read produce a value between 0.41-2.4 volts
what i want to say is from where does it display the reading while i am not moving any objects toward it

You question does not make any sense.

Robert Scott
Real-Time Specialties
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Apr 29, 2008 8:33 pm     Reply with quote

First of all, how do you have the sensor connected? Do you have any form of "smoothing" capacitor as illustrated in the example at the below link?

http://www.oopic.com/gp2d12.htm

The GP2D12 always puts out a voltage which is dependent on how close it is to an object. As RLScott said you need to decide how much change in distance (voltage) will trigger a display or you can simply display the distance reading constantly which is what you have now.

If you want a change to trigger the display you will have to do the following:

Define a constant value as the amount of voltage change trigger point (we will call it TRIG).

During start-up, get the adc_value and put it into a holding variable (we will call it OLD) to establish your initial baseline.

Now Enter the main loop.

Main Loop:
Get the current adc_value and compare it to OLD. If the difference exceeds TRIG then you save the current adc_value into OLD
and display OLD.

Delay however long you wish, blank the display at this point if you don't want the display to show constantly and go through the loop again.
Humberto



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

View user's profile Send private message

PostPosted: Tue Apr 29, 2008 10:17 pm     Reply with quote

Another tip that I would like to add is regarding how to setup accordingly the AD converter
of the PIC. Being the maximum expected output voltage very close to 1/2 Vcc, a good
improvement in the measurement would be achieved replacing:
setup_adc_ports(AN0); by
setup_adc_ports(AN0_VREF_VREF);

Where:
AN0 = Analog input
A2 = VREF- = VRefl = Gnd
A3 = VREF+ = VRefh = 2.55V

This give a resolution of 10mV per step using the ADC in 8 bits mode. Another point
to take care with this type of sensors is that its output do not follows a linear function,
hence you would need to determine what distance correspond to what voltages.
If these oddities are a problem in your application, they could be corrected using
a linearizing table.

Humberto
eng.khlaif



Joined: 19 Apr 2008
Posts: 7

View user's profile Send private message

PostPosted: Wed Apr 30, 2008 11:38 am     Reply with quote

dear all, thanks for your comments
now it seem that the sensor is working well with me and now i am to convert the voltage into equivalent distance and i modify my code as shown below
what i want to ask it now is how to display voltage on the first line on LCD and distance on the second line as below
V=voltage value
D=read distance

Code:
#include <16F877a.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <lcd.c>

//============================
void main()
{
float distance;
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();
   volts = (float)(adc_value * 5)/1023.0;

   if(volts >=0.41 && volts <0.53)
   {  distance=(148.3333 -volts *166.667);
      printf(lcd_putc, "\fV=%3.2f", volts);
      printf(lcd_putc, "\fD=%3.2f", distance);
    }
 else
    if(volts >=0.53 && volts <0.68)
   {  distance=(117.16933 -volts*107.867 );
      printf(lcd_putc, "\fV=%3.2f", volts);
      printf(lcd_putc, "\fD=%3.2f", distance);
    }

 else
    if(volts >=0.68 && volts <0.92)
   {  distance=(79.435 - volts*52.375  );
      printf(lcd_putc, "\fV=%3.2f", volts);
      printf(lcd_putc, "\fD=%3.2f", distance);
    }

 else
   if(volts >=0.92 && volts <1.17)
   {  distance=(58.1508 - volts*29.24);
      printf(lcd_putc, "\fV=%3.2f", volts);
      printf(lcd_putc, "\fD=%3.2f", distance);
    }

 else
   if(volts >=1.17 && volts <1.57)
   {  distance=(45.20475 - volts * 18.175);
      printf(lcd_putc, "\fV=%3.2f", volts);
      printf(lcd_putc, "\fD=%3.2f", distance);
    }
 else
   if(volts >=1.57 && volts <1.83)
   {  distance=(33.4569 - volts*10.6923);
      printf(lcd_putc, "\fV=%3.2f", volts);
      printf(lcd_putc, "\fD=%3.2f", distance);
    }
 else
     if(volts >=1.83 && volts <2.24)
   {  distance=(26.37895 - volts * 6.82456);
      printf(lcd_putc, "\fV=%3.2f", volts);
      printf(lcd_putc, "\fD=%3.2f", distance);
    }
 else
   lcd_putc("\f Out Of Range \n");
   //printf(lcd_putc, "\f%3.2f", volts);
   delay_ms(9500);

  }
  }
lasal



Joined: 25 Jan 2012
Posts: 13

View user's profile Send private message

use both lcd lines
PostPosted: Mon Oct 01, 2012 9:52 pm     Reply with quote

Code:

printf(lcd_putc, "\fV=%3.2f", volts);  // voltage print in first line
printf(lcd_putc, "\nD=%3.2f", distance);  // distance  print in second line
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Oct 02, 2012 4:47 am     Reply with quote

Please don't answer 4.5 year old questions.
It is a waste of your time as the original topic poster has long disappeared. Even worse, it is also wasting our time as this ancient topic is on top of the thread list again where everybody takes a look.
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