electronics forum

Rules | Recent posts | topic RSS | Search | Register  | Log in

Convert analogue voltage to distance value(GP2D12 sensor)??


Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers -> Convert analogue voltage to distance value(GP2D12 sensor)??
Author Message
hweontey



Joined: 10 Mar 2008
Posts: 8


Post09 Jun 2008 19:42   

Convert analogue voltage to distance value(GP2D12 sensor)??


I am new beginner in PIC microcontroller. I use PIC16F877A and Hi-Tech PICC LITE Compiler (C compiler). I already wrote C codes to get analogue voltage from GP2D12 distance sensor. But, i tried many times to convert it into distance value based on datasheet. I still can't get it. I NEED some help. I hope everyone can share experience and idea with me. The C codes as shown below. Thanks.

Added after 3 minutes:

My C codes:


//==================inckude=================================
#include<pic.h>

//===============configuration==============================
__CONFIG (0x3F32);

//===============define IO port=============================
#define lcd PORTB
#define RS RA2
#define E RA5
#define CHANNEL0 0b10000001 // AN0 sensor0

//==============FUNCTION PTOTOTYPE=========================
void e_pulse(void);//
void delay(unsigned short i);//
void send_char(unsigned char data);//
void send_config(unsigned char data);//
void lcd_goto(unsigned char data);//
void lcd_clr(void);//

void dis_num(unsigned long data);
void read_adc(void);
unsigned short read_temp(void);

//====================MAIN================================
unsigned short result;
unsigned short temp;

void main(void)
{
ADRESH=0; //clear A/D result
ADRESL=0; //clear A/D result

//setting ADCON1 Register
ADCON1=0b11000101; // A/D result right justified,
// configure RA2 and RA5 as digital I/O

TRISA=0b11011011; //configure PORTA I/O direction
TRISB=0b00000000; //configure PORTB as output
TRISC=0b00000000; //configure PORTC as output

PORTA=0;
PORTC=0;

while(1)
{
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //Lcd Return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //diplay on, cursor off and cursor blink off
send_config(0b00111000); //function set

lcd_goto(0); //cursor start from beginning

send_char('D');
send_char('I');
send_char('S');
send_char('T');
send_char('A');
send_char('N');
send_char('C');
send_char('E');
send_char('=');

while(1) //infinity loop
{

//sensor A
ADCON0=CHANNEL0; //CHANNEL0=0b10000001
lcd_goto(9);

read_adc();

temp=read_temp();//***Question: How to convert analogue voltage from sensor to distance in cm based on GP2D12 sensor??
dis_num(temp/10);
send_char('.');
dis_num(temp%10);
send_char('C');
send_char('M');

delay(2000);

}

}

}


//==================subroutine LCD setting ==========================

void send_config(unsigned char data)
{
RS=0;
lcd=data;
delay(500);
e_pulse();
}

void e_pulse(void)
{
E=1;
delay(500);
E=0;
delay(500);
}

void send_char(unsigned char data)
{
RS=1;
lcd=data;
delay(500);
e_pulse();
}

void lcd_goto(unsigned char data)
{
if(data<16)
{
send_config(0x80+data);
}
else
{
data=data-20;
send_config(0xc0+data);
}
}

void lcd_clr(void)
{
RS=0;
send_config(0x01);
delay(600);
}

void dis_num(unsigned long data)
{
unsigned char hundred_thousand;
unsigned char ten_thousand;
unsigned char thousand;
unsigned char hundred;
unsigned char tenth;

hundred_thousand = data/100000;
data = data % 100000;
ten_thousand = data/10000;
data = data % 10000;
thousand = data / 1000;
data = data % 1000;
hundred = data / 100;
data = data % 100;
tenth = data / 10;
data = data % 10;

if(hundred_thousand>0)
{
send_char(hundred_thousand + 0x30); //0x30 added to become ASCII code
send_char(ten_thousand + 0x30);
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}

else if(ten_thousand>0)
{
send_char(ten_thousand + 0x30); //0x30 added to become ASCII code
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(thousand>0)
{
send_char(thousand + 0x30); //0x30 added to become ASCII code
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(hundred>0)
{
send_char(hundred + 0x30); //0x30 added to become ASCII code
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(tenth>0)
{
send_char(tenth + 0x30); //0x30 added to become ASCII code
send_char(data + 0x30);
}
else send_char(data + 0x30); //0x30 added to become ASCII code
}

//==================subroutine ADC=========================

void read_adc(void)
{
unsigned short i;
unsigned long result_temp=0;
for(i=2000;i>0;i-=1) //looping 2000 times for getting average value
{
ADGO = 1; //ADGO is the bit 2 of the ADCON0 register
while(ADGO==1); //ADC start, ADGO=0 after finish ADC progress
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC

result_temp+=result;
}
result = result_temp/2000; //getting average value

}

unsigned short read_temp(void)
{
unsigned short temp;
temp=result;
return temp;
}

//==================subroutine DELAY==========================
void delay(unsigned short i)
{
for(;i>0;i--);
}
Back to top
wizpic



Joined: 23 May 2004
Posts: 380
Helped: 26


Post09 Jun 2008 22:21   

Re: Convert analogue voltage to distance value(GP2D12 sensor


I've add a quick look at the data sheet, I can see (I think) that it gives a 0-2.5V out

While I don't use C but the way I would go about doing it is set a distance and measure it with a ruler then see what what the ouput voltage would be at that distance then work out a routine by using floating point maths then set up the maximum distance then compare your reading's

Tell us some voltage reading your getting and I'll see if I can work it out in maths which you would have to convert it to C

wizpic
Back to top
rikie_rizza



Joined: 05 Aug 2006
Posts: 334
Helped: 20
Location: Bikini Bottom, between a rock and a pineapple


Post10 Jun 2008 2:09   

Convert analogue voltage to distance value(GP2D12 sensor)??


Do a calibration...never trust data sheet just like that.

DO A CALIBRATION FOR ANY INSTRUMENTATION DESIGN WORK!!!! NEVER TRUST DATA SHEET!!!

You know how to get data right? Do least square calculation...use MS Excel, is just that simple!
Back to top
Google
AdSense
Google Adsense




Post10 Jun 2008 2:09   

Ads




Back to top
hweontey



Joined: 10 Mar 2008
Posts: 8


Post10 Jun 2008 11:51   

Re: Convert analogue voltage to distance value(GP2D12 sensor


The reference website as shown below:
http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html

The result as shown below:



Sorry, but you need login in to view this attachment

Back to top
Arabic versionBulgarian versionCatalan versionCzech versionDanish versionGerman versionGreek versionEnglish versionSpanish versionFinnish versionFrench versionHindi versionCroatian versionIndonesian versionItalian versionHebrew versionJapanese versionKorean versionLithuanian versionLatvian versionDutch versionNorwegian versionPolish versionPortuguese versionRomanian versionRussian versionSlovak versionSlovenian versionSerbian versionSwedish versionTagalog versionUkrainian versionVietnamese versionChinese version
Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers -> Convert analogue voltage to distance value(GP2D12 sensor)??
Page 1 of 1 All times are GMT + 1 Hour
Similar topics:
IR Distance Sensor (11)
IR Distance sensor (3)
analogue voltage supplied to PIC.. Help! (4)
Sharp GP2Y0A21YK Distance Measuring Sensor (2)
Temperature sensor & max distance (3)
How to use: Sharp GP2Y0A21YK Distance Sensor (8)
light intensity and distance of color sensor (11)
Circuit - IR sensor to read at <3cm distance (+10points) (2)
controlling a DC Motor using pic16F877 and a distance sensor (3)
Circuit for IR sensor to read at <3cm distance (+10points (3)


Abuse || Administrator || Moderators || Support us || sitemap
topic RSS