Sunteți pe pagina 1din 1

Ultrasonic Distance Sensor: MikroC Implementation REV 0.1 By: Bangon Kali (bangonkali@istoryahi.

me, 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 doesnt 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 Trigger
RB1 (OUTPUT) Vdd PIC Vss Echo RB0 (INPUT) Trigger

Wait

Echo

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;

S-ar putea să vă placă și