Re: I DONT KNOW WHERE I AM WRONG .. USING PIC16F877a and HC-SR04.. XTAL_FREQ = 20MHz>
hello,
i didn't check all your code, but , some remarks :
maxi
width counter value is 32767
(better is to use unsigned int to avoid negative value! if over 32767)
witch is 3276710/5880=> 55 meters !
In reality, maximum possible will be <4 meters 4000 mm
5,88µSec/ milimeter
=> 4000 * 5,88=>
23520 µSec maxi
if resolution is 10µS => your width counter will get 2352 maxi
poor resolution
2352 will give 2352/58=40,55 decimeter !
so, don't use delay10µS in the loop
with a loop in C langage , accuracy is not enough.
or try to calibrate the loop for 1µS duration or a multiple like 5,8µS !!
Code:
while (RB2==1) //calculate the width of the echo received
{
Nop();;
Nop();
width++;
}
check asm result to calculate the duration of the loop..
if duration is 1µS you will get 23520 µS / 58 => 405 cm
or if you can adjuts loop duration to 5,8µS by adding some NOP() =0.25µS
you have direct value in mm width=4055
the best is to use Timer counter
at 20Mhz , 1 cycle is 4/20=0.25µS
and your resolution will better than 1 mm
BTW, how do check the result ? no display ?
Nota: you can not measure below 2 or 3cm !!!
if you want also to correct the measure by the ambiant temperature
check
**broken link removed**