vineeth_gk
Newbie level 2
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.
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