Altera_Forum
Honored Contributor
10 years agoLooping problem faced since loops are not synthesizable. Any other way?
Hi can anyone please help me with this code i wrote for hc-sr04 (ultrasonic sensor)? I need to loop the process after 1 sec. Since i cannot use while or for loops because they are not synthesizable. Can someone tell me how i can loop this?
module usensor3 (trig, echo, distance_o,reset);
output trig, distance_o;
input clk, echo, reset;
reg dist_counter=0;
reg counter=0;
reg trig;
always @ (posedge clk)
begin
if (reset)
begin
counter<=0;
dist_counter<=0;
end
else
begin
counter <= counter + 1;
if (counter <= 500) //10usec to initialize sensor
trig<=1; // trig is set high
if (echo) // sensing 5v at echo pin so echo pin is high
begin
dist_counter <= counter; // counter value will be recorded in distance
trig <=0;
end
if (counter<= 1900000) // maximum time of sensing any object is 38ms
trig<=0;
if (counter<= 5000000) // wait 1 sec to begin again
begin
counter<=0;
distance_o <=0;
end
// How to start the whole cycle again
end
end
assign distance_o = (dist_counter * 340) << 1; //The velocity of the ultrasonic burst is 340m/sec in air
endmodule