;---------------------------; ; Ultrasonic Sensor Trial ; ;---------------------------; ; Mike Shegedin ; 20.November.2011 ; ; Ver1.2 ; Cleanup of code and documentation ; ; AVR: ATTiny13A ; AVR PIN FUNCTION ; ======= ======================= ; Pin2 PB3 SENSELED (sense LED) (optional) ; Pin3 PB4 BUTTON (optional) ; Pin5 PB0 TRIG pin on ultrasonic sensor ; Pin6 PB1 ECHO pin on ultrasonic sensor ; Pin7 PB2 RANGELED (range LED) (optional) ; ; Ultrasonic Sensor: ; HC-SR04 Ultrasonic ranging module ; ; Operation: ; Apply a 10µs signal on TRIG to start the sensing cycle. The HC-SR04 sensor will ; send out a 40kHz pulse for 200µs. If something is in the path of the ultrasonic ; pulse, this pulse will bounch off that and return to the sensor. The ECHO line on ; the sensor will go high for 150µs~25ms depending on distance to detected object. ; If the ECHO line is high for 38ms it indicates that no return pulse was detected. ; There should be an interval of at least 50ms between subsequent TRIG pulses ; otherwise a lingering ultrasonic pulse may be detected during the next ping cycle. ; ; ===================================================================================== ; ; Distance Calculation for ultrasonic sensing: ; Speed of sound is 340.29 m/s. Pulse must be sent and then return. ; Therefore time for a sound pulse (ping) to hit the target and return to the sensor ; is 170.15 m/s. ; ; Convert to µs/s: ; ; s m 1x10^6(1E6)µs 58.772µs ; ------- X ----- X ------------- = -------- ; 170.15m 100cm s cm ; ; This means it takes about 59µs for a sound pulse to travel 1cm to an object and ; then return to the sensor. ; ; Compute distance based on time: ; Example 1: A ping takes 150µs to hit an object and return. How far away is the ; object? ; ; cm ; 150µs X -------- = approx. 2.55cm ; 58.772µs ; ; Example 2: How long for a ping to hit and return from an object 30cm away? ; ; 58.772µs ; 30cm X -------- = approx. 1,763µs or 1.763ms ; cm ; ; ===================================================================================== ; ; AVR TIMINGS ; Use default ATTINY13 timings (9.6MHz with /8 prescaler) for a 1.2MHz clock. ; Fuse setting: -U lfuse:w:0x6a:m -U hfuse:w:0xff:m ; ; ============= 1.2MHz clock timings ============= ; ; 1,200,000 clk = 1s 1,000,000 clk = 833.3ms ; 120,000 clk = 100ms 100,000 clk = 83.3ms ; 12,000 clk = 10ms 10,000 clk = 8.3ms ; 1,200 clk = 1ms 1,000 clk = 833.3µs ; 120 clk = 100µs 100 clk = 83.3µs ; 12 clk = 10µs 10 clk = 8.3µs ; 1.2 clk = 1µs 1 clk = 833.3ns ; ; NOTE: Timing accuracy of the AVR clock without a crystal is only about ±10%. Much ; higher accuracy can be achieved by using an external crystal if desired. Of ; course, if an external crystal is used, clock timings will be based on the ; value of the crystal. ; ; ===================================================================================== ; ; DELAY: Double Byte Timing Loop Routine ; By counting the number of clock cycles for the routine, including loading n ; into YH and YL, calling the delay routine, and returning back, the number of ; clock cycles (clks) was found to be as follows: ; ; Delay in clock pulses (clks) based on n: Time(clks) = 4n + 8 ; ; Time delay in µs based on n: ; 10µs ; Time(µs) = (4n+8)clks X ------ ; 12clks ; ; Therefore, simplified: ; ; 10n + 20 ; Time(µs) = -------- ; 3 ; ; Compute n for the desired time T(µs) (solve for n): ; ; 3T(µs) - 20 ; n = =========== ; 10 ; ; Example: What should n be for a 100µs delay? ; ; (3 x 100) - 20 ; n = -------------- ; 10 ; ; n = 28 ; ; Example 2: What should n be for a 50ms delay? ; ; 1000µs ; First, convert ms to µs --> 50ms x ------ = 50,000µs ; 1ms ; ; (3 x 50,000) - 20 (3 x 50,000) ; n = ----------------- or n = ------------ ; 10 10 ; ; n = 14,998 or n = 15,000 ; ; Note that the "- 20" term can probably be dropped for millisecond ; calculations since doing so results in a less than 1% error in these cases. ; ; ===================================================================================== ; ; Conversion Summary ; ; Time/Distance Ratio from start of sending ping to the time it is received back: ; ; 58.772µs ; -------- ; cm ; ; AVR Clock/Time Ratio (for 1.2MHz AVR clock) ; ; 1.2clks ; ------- ; µs ; ; AVR Clock clks/Distance Formulae ; ; 70.526clks 0.014179cm ; ---------- or ---------- ; cm clks ; ; Timing of pulse indicating no object sensed (according to sensor datasheet): ; ; 38ms which is the equivilant of 31,667clks. ; ; ===================================================================================== ; .INCLUDE "TN13DEF.INC" ; ATTINY13 DEFINITIONS .EQU SENSELED=PORTB3 ; SENSELED pin (Output on AVR) .EQU RANGELED=PORTB2 ; RANGELED pin (Output on AVR) .EQU BUTT=PORTB4 ; Button pin (Input on AVR) .EQU TRIG=PORTB0 ; Sensor TRIG pin (Output on AVR, input on sensor) .EQU ECHO=PORTB1 ; Sensor ECHO pin (Input on AVR, output on sensor) .EQU BDV10usCNT=1 ; Number of iterations for (approx.) 10µs delay .EQU WDV50msCNT=10415 ; WDELAY count for 50ms delay .EQU BDV1cmCNT=19 ; Number of iterations for BDELAY loop to get 1cm ; resolution. .EQU BDV2mmCNT=6 ; Number of iterations for BDELAY loop to get 2mm ; resolution. .DEF A = R16 ; GENERAL PURPOSE ACCUMULATOR .DEF BDV = R17 ; Counter used in BDELAY routine. .DEF CNT = R18 ; CNT will be the no. of 150µs intervals of the ECHO ; ECHO pulse, each of which indicates 2.55cm. CNT=0 ; indicates object not detected. .DEF DTEST=R19 ; DTEST is used to test for particular distances. ; Will be 1 if distance test passes or 0 if failed .DEF DRNG1=R20 ; Distance in increments of CNT for DNEARER and the shorter ; distance for DBETWEEN. .DEF DRNG2=R21 ; Longer distance (in increments of CNT) for DBETWEEN. .ORG 0000 RJMP ON_RESET ; GO HERE WHEN CHIP IS TURNED ON OR RESET ON_RESET: ldi A, low(RAMEND) ; 1 Set Stack Pointer to end of RAM (1 BYTE ON TINY13) out SPL,A ldi A, (1<