unsigned long Over;
Unsigned int time;
float Distance;
while(1)
{
sendPulse();
T1CONbits.TMR1ON = 0;
PIR1bits.TMR1IF=0;
TMR1H= 0;
TMR1L= 0;
Over=0;
time=0;
while(Echo == 0);
T1CONbits.TMR1ON = 1;
while(Echo == 1)
{
if (PIR1bits.TMR1IF==1)
{
Over=Over+65536;
PIR1bits.TMR1IF=0;
}
}
T1CONbits.TMR1ON = 0;
time= TMR1H;
time= time<<8;
time= time|TMR1L;
printf("\r\nTime = %d",time);
printf("\r\nOver = %ul",Over);
Distance=(float) time + (float) Over;
printf("\r\nTravel duration %4.1f µS",Distance);
Distance=Distance*0.2/5.88; // counts * 0,2µS / 5.88 => result in mm
printf("\r\nDistance %4.1f mm",Distance);