1

Click here to load reader

Ultrasonic Distance Sensor

Embed Size (px)

DESCRIPTION

A simple MikroC implementation of the Ultrasonic Distance Sensor.

Citation preview

Page 1: Ultrasonic Distance Sensor

Ultrasonic Distance Sensor: MikroC Implementation REV 0.1 By: Bangon Kali ([email protected], Click this link to get the most updated copy)

Disclaimer: Ako-ako ra ni, dli official, not very accurate, needs to be calibrated by the user. Measuring quality depends

on the instruction cycle frequency of the PIC which is Fosc/4.

Note: I have a more accurate method but the code is just too complex and you need CCP Module for the PIC. Just try the

method in this document, and if it doesn’t work on your PIC then I will post another method using the CCP Module.

Device: http://www.e-gizmo.com/KIT/sonar.htm

Datasheet: https://docs.google.com/file/d/0BxdLxDCD6HidSloyN0ZPYVA4QjA/edit

Timing Diagram

Implementation

Source Code … PORTD.B1 = 1; // Trigger

Delay_us(20); // Wait >=5uS

PORTD.B1 = 0; // Low

while (PORTD.B0 == 0) // Wait

count_distance = 0; // Initialize Counter

while (PORTD.B0 == 1) // Time HIGH state in Echo

{ // Recommended: make this part light for awesome

count_distance++; // Increment Loop => Proportional to time HIGH

}

if (count_distance > MaxDistance) // Do some arithmetic to Calibrate

count_distance = MaxDistance; // Reading with real world Distance

count_distance = (MaxDistanceScaled*count_distance/MaxDistance);

return count_distance;

Wait

Echo

Trigger

Vss

Vdd

RB1 (OUTPUT)

RB0 (INPUT) PIC

Trigger

Echo