ultrasonic reading problem

Thread Starter

walid el masry

Joined Mar 31, 2009
133
i wanna build a distance measure using ultrasonic and i have the module vm125 by vellemanusa on this link here

it suppose to measure distance between range: 0.66-8.20ft or 20.12-249.94cm

the module has an analog output which represent the distance and it has rating of voltage output 0-5v

am using pic microcontroller and in my case 16f876 but am confused i can't measure the right distance and i guess there is some thing wrong in my calculation


the adc has a resolution of 10bit and the analog output is 5v so the analog reading will represent the distance using this formula

distance = adc reading * (5/1024) but when i tested it gives me wrong result can any one help me here plz

i include her the code and photos of the circuit

Rich (BB code):
void initialize(){
 PORTA=0x00;
 TRISA=0x01;
 ADCON1=0b10001110;
 ADCON0=0b10000000;
 ADCON0.f0=1;

 Lcd_Init(&PORTB);
 Lcd_Cmd(Lcd_CLEAR);
 Lcd_Cmd(Lcd_CURSOR_OFF);
}

void ad_read(){
 ADCON0.f2=1;
 while(ADCON0.f2==1){}
   ad_res=0;
   ad_res=ADRESH;
   ad_res=ad_res<<8;
  ad_res=ad_res+ADRESL;
  distance=ad_res*(5/1024);
  Lcd_Cmd(Lcd_Clear);
  FloatToStr(distance, ad_res_txt);
  Lcd_Out_Cp(ad_res_txt);
  Lcd_Out_Cp("cm");
  Lcd_Cmd(LCD_SECOND_ROW);
  WordToStr(ad_res, ad_res_txt);
  Lcd_Out_Cp(ad_res_txt);
 }




 

Markd77

Joined Sep 7, 2009
2,806
try:
distance=ad_res*(250/1024);
instead of
distance=ad_res*(5/1024);

I think there is something else wrong because I would expect a value between 0-5 with your current calculation.
Maybe you have the ADC result in the left shifted instead of right shifted format.
 

Thread Starter

walid el masry

Joined Mar 31, 2009
133
the adc result is already fight justified since i wrote ADCON1=0b10001110; so the bit7 ADFM is equal 1 which is a right justified

and when i used you formula the result gives me zero !!!
 
Top