Localizador de distância baseado em AT89S52 e sensores ultrassônicos HC-SR04

Distance finder based on AT89S52 and HC-SR04 ultrasonic sensors

Distance finder based on HC-SR04 gives the distance to an obstacle in centimeters. It has a range of 2 cm to 400 cm. The project is built around the AT89S52, which is a microcontroller from the 8051 family. Distance is displayed on seven-segment displays.

Components:

AT89S52

Three 7 seconds. Expense (Common anode type)

HC-SR04 (ultrasonic sensor module)

Connectors, PCBs etc.

The code is written in Assembly and is easily understandable. The project can be used in robots to avoid obstacles, etc.

Working:

The operation of ultrasonic sensors is quite simple and easy to interface with the microcontroller. The sensor module has 4 pins, of which Pin-1 and Pin-4 are +Vcc and Gnd respectively. Pin-2 is Trigger and Pin-3 is Echo pin.

When a high pulse of 10us is applied to the TRIG pin, the ultrasonic transmitter sends 8 consecutive pulses of 40kHz frequency. As the eighth pulse is sent, the sensor's ECHO pin becomes HIGH. Now when ultrasonic waves are reflected from any surface and are received by the receiver, the ECHO pin goes LOW. The time it takes to leave and return to the sensor is used to find the distance to the reflecting surface.

Distance in centimeters = (Time/58) cms

In inches = (time/148)

The distance can also be calculated taking into account the speed of sound (=340m/s)

HC-SRO4

CODE Explanation:

· The data lines of seven-segment displays are interfaced to port-0

· Port-1, Pin-0,1 and 2 are selected lines for SSDs

· P3.0 is connected to Trig

· P3.1 is connected to the Echo pin of the sensor module

· In the main part of the Program, first Timer-1 is initialized in mode 2 (8-bit auto-reload)

When P3.0 is set high, the DELAY1 subroutine is called. After 10us P3.0 is reset to 0

· Now P3.1 is checked for a high signal. As P3.1 gets high, Timer 1 starts and every time it overflows, Register A is incremented.

· The loaded count value should be 58 (mentioned in the datasheet). So that after 58 cycles A is incremented once. But since the delay caused by instructions needs to be compensated, the count value I used was 45 (=> 255D-210D).

· The value stored in A is used to extract the measured Distance. This is done by Division instruction.

· The 8-bit data value is sent sequentially to the respective 7-segment display and is selected through the selection lines.

Distance finder based on AT89S52 and HC-SR04 ultrasonic sensors

Distance finder based on AT89S52 and HC-SR04 ultrasonic sensors

Project source code

###


 ORG 00H 
MOV DPTR,#SSDisplay // move LUT address to DPTR
MOV P1,#00000000B // define P1 as output port MOV P0,#00000000B // define P0 as output port CLR P3.0 // define P3.0 as output for sending the trigger SET P3.1 // define P3.1 as input to receive echo MOV TMOD,#00100000B // define timer1 as automatic recharge timer mode 2 MAIN: MOV TL1,#210D // load the initial value to start counting MOV TH1,#210D // load the recharge value MOV A,#00000000B // clear the accumulator P3.0 SET //start the clock CALL DELAY1 // provides 10uS width for the trigger pulse CLR P3.0 //end the clock HERE: JNB P3.1,HERE // loop here until the echo is received BACK: SETB TR1 //start timer1 HERE1: JNB TF1,HERE1 // loop here until the timer runs out (i.e.; 48 counts) CLR TR1 //for the timer CLR TF1 // clear timer 1 flag INCA // increment A for each timer1 overflow JB P3.1,BACK // jump to BACK if echo is still available MOV R4,A // save the value of A in R4 ACALL DLOOP //call the display loop MAIN SJMP // jump to MAIN loop DELAY1: MOV R6,#2D // 10uS delay LOOP1: DJNZ R6,LOOP1 RET DLOOP: MOV R5,#50D // load R5 with 100D BACK1: MOV A, R4 //load the value in R4 to A MOV B,#100D // load B with 100D DIVAB // isolate the first digit SET P1.0 // activates the D1 LED display unit CALL DISPLAY //call the DISPLAY subroutine MOV P0,A // move digit unit pattern from first digit to P0 CALL DELAY // 1mS delay CALL DELAY MOV A, B // move the rest of the 1st division to A MOV B,#10D // load B with 10D DIVAB // isolate the second digit CLR P1.0 //disables the D1 LED display unit SET P1.1 // activates the D2 LED display unit CALL DISPLAY MOV P0,A // move digit unit pattern from 2nd digit to P0 CALL DELAY CALL DELAY MOV A, B // move the rest of the 2nd division to A CLR P1.1 //disables the D2 LED display unit SET P1.2 // activates the D3 LED display unit CALL DISPLAY MOV P0,A // move digit unit pattern from 3rd digit to P0 CALL DELAY CALL DELAY CLR P1.2 //disables the D3 LED display unit DJNZ R5,BACK1 //repeat the display loop 100 times RET DELAY: MOV R7,#250D // 1mS delay LABEL2: DJNZ R7, LABEL2 RET DISPLAY: MOVC A,@A+DPTR // get the digit unit pattern for the content in A RET SSD Display: DB 0C0H // Hexadecimal code to display DISPLAY 0 DB 0F9H // DISPLAY 1 DB 0A2H // DISPLAY 2

3

Project video

https://www.youtube.com/watch?v=He7SEEitFP4

Questions related to this article?

👉 Ask and discuss on the EDAboard.com and Electro-Tech-Online.com forums.

Tell us what you think!! Cancel reply

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.