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

GP2D120 Infrared Sensor

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



Joined: 21 Feb 2005
Posts: 5

View user's profile Send private message

GP2D120 Infrared Sensor
PostPosted: Mon Feb 21, 2005 1:21 pm     Reply with quote

hello

i have a sharp gp2d120 range sensor which has analog output. i will convert the output to digital with pic 16f877,and use it for data acquisition. i have code to read the adc but the output values are floating and am not sure what to do. Can help me out,as am having some problem about code?

thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 21, 2005 1:30 pm     Reply with quote

You need to post a small test program that shows the problem.
You should program this test program into your PIC and verify
that it works and then post it, along with a few samples of the output.
Basically, we want to look at your A/D setup code, and we want to
see your #fuses and #device statements, and we want to see your
variable declarations, etc. So post a complete (but small) test program.
For example, don't leave out the "#use delay()" statement, because
you think "they don't need to see that". The part that you leave out
may be the problem. You should be able to show us the problem
with a test program that has maybe 25 lines, max.
ee01ppa



Joined: 21 Feb 2005
Posts: 5

View user's profile Send private message

PostPosted: Mon Feb 21, 2005 2:11 pm     Reply with quote

This is my first adc program am using MPLAB to debug and program the pic on a picdem 2 plus development board. I have been watching the variable x in the debeg window but the value of x keeps floating between 0-255 and am unsure whether the problem is to do with my code.
Code:

#include <16F877.h>
#device ICD=TRUE
#device adc=10
#use delay(clock=4000000)
#fuses XT, NOPROTECT, BROWNOUT, NOLVP, NOWDT,DEBUG
#include<stdio.h>
#include<STDLIB.h>

#define STEP_CLOCK PIN_B0
#define CUTOFF 200           

//The range of the ADC of the PIC Development Board is 0 to 5V and the ADC conversion is 10bits. 

void main()
{
  int x, x1;
  int count = 10;

  while(1) 
  {
    setup_adc_ports(RA0_RA1_RA3_ANALOG); //set all ports for analog inputs
   setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(1); // prepare to read analog input from channel 1
    delay_us(20);
    x = read_adc();
    x1 = 5.00*x*100/1023.00;
    if(x>CUTOFF )
       {
        output_low(STEP_CLOCK);
       }
   output_high(STEP_CLOCK);
  }

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 21, 2005 2:33 pm     Reply with quote

The problem is due to your variable size. In CCS, an "int" is only 8-bits.
For a 10-bit A/D value, you need to use a "long", which is 16-bits in CCS.

CCS has some alternate variable names, which are much better to use,
because they remind you of the size. These are:

int8
int16
int32

Also, I'm not sure what you're trying to do with this line:
x1 = 5.00*x*100/1023.00;
It looks like you're trying to scale the result by a factor of roughly 0.5 ?
Is that really your intention ? If so, why not just divide by 2 ?
Also, you're mixing floating point and 8-bit integer data types.

So at a minimum, you need to redefine
int x, x1;
as
int16 x, x1;
Why not just keep it as an integer ?
ee01ppa



Joined: 21 Feb 2005
Posts: 5

View user's profile Send private message

PostPosted: Mon Feb 21, 2005 2:48 pm     Reply with quote

I have changed the variable to long int and i am now getting the right values. Thank very much!! You have been a great help.
Remco
Guest







PIC16f688 ADC problem
PostPosted: Fri Sep 09, 2005 4:19 am     Reply with quote

Hello,
I use a pic16f688 for reading an analog channel and send it trough rs232.
Now the function READ_ADC seems to deliver only 8 bits.
What goes wrong?
Can anyone help me?

Thanks
Remco

#include <16f688.h>
#device ADC=10
#fuses INTRC,NOWDT,NOPROTECT, NOMCLR
#use delay(clock=8000000)
#use rs232(baud=9600, bits=8, xmit=PIN_C1, rcv=PIN_C2)


void main() {

char string[30];
int16 value;

setup_adc_ports(PIN_A0 | VSS_VDD);
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel(PIN_A0);

delay_ms( 10 );
while (TRUE)
{
printf("\r\nTik maar een woord in, bedenk maar wat:\n\r");

gets(string);

printf("\n\rhet door jou ingetikte woord was: %s", string);
value = Read_ADC();
printf("\n\rde meetwaarde is:%4x",value);

}

}
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

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

Re: PIC16f688 ADC problem
PostPosted: Fri Sep 09, 2005 4:32 am     Reply with quote

Remco wrote:
Hello,
I use a pic16f688 for reading an analog channel and send it trough rs232.
Now the function READ_ADC seems to deliver only 8 bits.
What goes wrong?
Can anyone help me?


You have an error in the print statement. It should be:

Code:

printf("\n\rde meetwaarde is:%4lx",value);

_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Ttelmah
Guest







PostPosted: Fri Sep 09, 2005 9:18 am     Reply with quote

Ongoing to the comments about the arithmetic:
5.0*x*100/1023.0

It is worth being aware of several things. By forcing two of the values concerned to be 'float', these parts of the aritmetic will use floating point values, and will be _slow_, and bulky.
Though the compiler may optimise the two multiplications into one, the use of a float, and integer, might prevent this.
Multiplication is always faster than division, unless the division is by a simple 'binary' value (2,4,8 etc.).
So to generate this value more efficiently, use:

(float)x*0.488758

This gives eactly the same conversion as a single multiplication.
However then 'ongoing', it is always nicer if possible, to do things in integers if possible.

(x*62L)>>7

Might well be acceptable, and will be enormously faster.

Best Wishes
Kerron



Joined: 16 Nov 2010
Posts: 5

View user's profile Send private message

Sharp GP2D120 IR Range Sensor
PostPosted: Tue Nov 16, 2010 3:06 pm     Reply with quote

Convert To Voltage

5/(2^10) = .004883

read adc value then multiply by .004883 saved into a Float Voltage

Distance = -15.9703*(Log(Voltage)-1.00125);

this is from an exponential regression from 19 data points on the Voltage Vs Distance graph in the GP2D120 data sheet, then I solved for the Distance. You can simplify it even more to bits to distance and make sure to set the proper conditions when the sensor is giving a wrong reading, refer to the data sheet.

Voltage to Distance in cm.

Kerron Manwaring

Kerron.Manwaring@gmail.com
914-830-3542
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