Ultrasonic Ranging with GA
Design idea of ultrasonic distance measurement
Overall idea of ultrasonic distance measurement:
Ultrasound is sent through the ultrasonic module to detect the distance of the front obstacle, and the calculation results are returned to the field programmable gate, and the operation data is displayed through modules such as digital tube or display screen.
1. Introduction to Modules
Ultrasound module:
Ultrasound module pin:
- VCC: Operating voltage 5V
- GND: Ground
- TRIG (control end): control outlet sends a high level above 10US
- ECHO (Receiver): When the ultrasonic module completes sending a high level for 8 cycles, read the high level duration at the ECHO and calculate the detection distance from the front.
Ultrasound time series:
How the module works:
- Use IO trigger ranging to give a high level signal of at least 10us;
- The module sends eight 40 kHz square waves automatically to detect whether a signal is returned or not.
- A signal returns, and a high level is output through IO. The duration of the high level is
- The time from transmission to return of ultrasound. Test distance = (high level time * sound speed (340M/S)/2;
2. Implementation steps of ultrasonic distance measurement
1. Write TRIG modules
Principle:
From the principle of the above-mentioned ultrasonic module, it is known that the module needs to send a high level of 10 US or more to the ultrasound.
The code is as follows:
`timescale 1ns / 1ps module Trig( input sysclk , input rst_n , output trig ); parameter DELAY = ( 70 * 50_000 ) + 15 * 50;//Send Clock Cycle (70ms+15us) reg [20:0] cnt; //---------------count----------------// always@(posedge sysclk) if(!rst_n) cnt <= 0; else if(cnt == DELAY - 1) cnt <= 0; else cnt <= cnt + 1; //---------------initial trig--------------// assign trig = (( cnt > 0) && (cnt < 15 * 50 )) ? 1 : 0; endmodule
2. Write ECHO modules
Principle:
When a high level is detected, the counter starts timing, and when a low level counter is detected again, the result of the count is assigned to the secondary register and the distance is calculated through the formula. (High Level Time * 20) / 58/1000 (cm)
State Diagram:
The code is as follows (example):
`timescale 1ns / 1ps module ECHO( input sysclk , input rst_n , input echo , output [13:0] dis //distance ); //----state---// parameter IDLE = 3'b001, s1 = 3'b010, s2 = 3'b100; reg [2:0] cur_state; reg [2:0] next_state; reg [31:0] cnt; reg [31:0] cnt_reg; //----first----// always@(posedge sysclk) if(!rst_n) cur_state <= IDLE; else cur_state <= next_state; //----second----// always@(*) case(cur_state) IDLE :begin//Idle state if(echo == 1) next_state = s1; else next_state = IDLE; end s1 :begin// if(echo == 0) next_state = s2; else next_state = s1; end s2 :begin next_state = IDLE; end default:next_state = IDLE; endcase //-----third-----// always@(posedge sysclk) if(!rst_n) begin cnt <= 0; cnt_reg <= 0; end else case(cur_state) IDLE :begin cnt <= 0; cnt_reg <= cnt_reg; end s1 :begin cnt <= cnt + 1; cnt_reg <= cnt_reg; end s2 :begin cnt_reg <= cnt; cnt <= 0; end default:begin cnt <= 0; cnt_reg <= 0; end endcase //----distance-----// assign dis = (cnt_reg * 20)/58/1000; endmodule
3. Write top-level modules
Principle:
The TRIG module and ECHO module are instantiated and the distance can be displayed on the digital tube or display.
Code implementation:
`timescale 1ns / 1ps module TOP( input sysclk , input rst_n , input echo , output trig , output [13:0] dis ); Trig trg( .sysclk (sysclk ) , .rst_n (rst_n ) , .trig (trig ) ); ECHO echo_u( .sysclk (sysclk ) , .rst_n (rst_n ) , .echo (echo ) , .dis (dis ) ); endmodule
3. Summary and Expansion
You can write the driver of the digital tube by yourself and encapsulate it as an IP direct call, and display the distance on the digital tube in real time to achieve ultrasonic distance measurement or to connect the buzzer and other fixed distance alarm devices.