Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Distance measurement using FPGA with ultrasonic sensor HC-SR04

Status
Not open for further replies.

vineeth_gk

Newbie level 2
Joined
Oct 27, 2017
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
india
Activity points
13
iam new to verilog coding and fpga's.i would like to get help about my following code for interfacing ultrasonic sensor with fpga and measuring distance.
this is my code please suggest corrections on implementation.


Code:
module usensor (trig, echo, distance, reset, clk);


output  trig, distance;
input clk, echo, reset;

reg trig,distance;
reg [20:0] dist_counter=0;
reg [25:0] counter=0;
reg [20:0] echo_time;
reg waitn;

always @ (posedge clk)
begin
    
	if (reset)
		begin
			counter<=0;
			dist_counter<=0;
		end
	else begin
	    
		if(counter==0)
			trig <= 1;
		else if (counter <= 1000)         //10usec to initialize sensor
				begin
					waitn<=1;
					trig<=0;
					//counter<=0;             
				end
		else if (counter==3800000)        //off duration of 38ms for the trigger pulse
		     begin
			 counter<=0;
			 trig<=1;
			 end
	counter=counter+1;		 	
	 if (echo==1)                   // sensing 5v at echo pin so echo pin is high
				begin
					dist_counter <= dist_counter+1;
				
				end
	else if(echo==0 && waitn==1)  //assigning the count of duration of echo to echo_time
		         begin	
		          dist_counter=0;
		          waitn=0;
		          echo_time<=dist_counter;
		         end
	else if(counter==100000000)   //refreshing the sensor after 1 seconds
	     begin
	      counter=0;
	      dist_counter=0;
	      trig=0;
	     end 	         
	         
	assign distance = echo_time  * 17000;      // speed of sound in air	         	
	end
end

endmodule
 

I am also new to verilog
but what is conflicting me is that the value you are trying to put in the "echo_time" is a value of count according to the time scale; what i mean is, this value is in multiple of some clock time period i.e (if 50 Mhz oscillator input to FPGA then the "actual echo time = distcount * 20 ns ") thus further calculations are to be carried
rather would you explain me how you got on this statement?
"assign distance = echo_time * 17000;"
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top