i want to write a c code that configure and gp2d12 IR sensor that will be used to detect the distance of the moving object and then convert the analog voltage into digital and then into distance
iam using the MPLAB and C language
when i use the folowing code the reading always gives
voltage=0;
i dont know why
pls ny one can help me
#include <pic.h>
#include "lcd.h"
#include "delay.h"
#include<stdio.h>
//setup the configuration word for use with ICD2
__CONFIG(DEBUGEN & WDTDIS & LVPDIS ) ;
void init_a2d(void){
ADCON0=0; // select Fosc/2
ADCON1=0; // select left justify result. A/D port configuration 0
ADON=1; // turn on the A2D conversion module
}
/* Return an 8 bit result */
unsigned char read_a2d(unsigned char channel){
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(ADRESH); // return 8 MSB of the result
}
void main (void)
{
unsigned char outString[20];
float voltage;
unsigned char x;
init_a2d(); // initialise the A2D module
GIE=0;
TRISE = 0 ;
TRISD = 0 ;
TRISB = 0 ;
TRISA = 0 ;
ADCON1 = 7 ;
RE2 = 0 ;
RA0=1;
DelayMs(500) ;
lcd_init() ;
while(1)
{
lcd_clear() ;
lcd_goto(0) ;
lcd_puts("welcome " ) ;
x=read_a2d(1);
voltage=(x/255.0)*(5.0);
sprintf(outString,"A2D = %d.%d volts",voltage);
lcd_puts(outString);
//lcd_puts("Real time ") ;
DelayMs(1500);
}
}
iam using the MPLAB and C language
when i use the folowing code the reading always gives
voltage=0;
i dont know why
pls ny one can help me
#include <pic.h>
#include "lcd.h"
#include "delay.h"
#include<stdio.h>
//setup the configuration word for use with ICD2
__CONFIG(DEBUGEN & WDTDIS & LVPDIS ) ;
void init_a2d(void){
ADCON0=0; // select Fosc/2
ADCON1=0; // select left justify result. A/D port configuration 0
ADON=1; // turn on the A2D conversion module
}
/* Return an 8 bit result */
unsigned char read_a2d(unsigned char channel){
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(ADRESH); // return 8 MSB of the result
}
void main (void)
{
unsigned char outString[20];
float voltage;
unsigned char x;
init_a2d(); // initialise the A2D module
GIE=0;
TRISE = 0 ;
TRISD = 0 ;
TRISB = 0 ;
TRISA = 0 ;
ADCON1 = 7 ;
RE2 = 0 ;
RA0=1;
DelayMs(500) ;
lcd_init() ;
while(1)
{
lcd_clear() ;
lcd_goto(0) ;
lcd_puts("welcome " ) ;
x=read_a2d(1);
voltage=(x/255.0)*(5.0);
sprintf(outString,"A2D = %d.%d volts",voltage);
lcd_puts(outString);
//lcd_puts("Real time ") ;
DelayMs(1500);
}
}