Forum Discussion
Altera_Forum
Honored Contributor
10 years agoHello, thanks for your comment. Sorry if i wasn't precise. Let me explain what i exactly want to do with it. I want to measure the how far an object is from the sensor and if an object is detected i want a LED to light up and also a variable "dist" should give me the calculated distance of the object. I read the manual. but i am confused about timings. I am not sure if i am getting it right.
Here is the vhdl code for the sensor (i don't know vdhl but took some ideas from here): https://github.com/xesscorp/xula/blob/master/fpga/xula_lib/hcsr04.vhd Here's the verilog code I've written so far (i'm sure I'm missing a lot): timescale 1 us/ 1 us module sensor (trig, dist, clear, led, clk, echo); output trig, dist, clear, led; input clk, echo; reg [3:0] timer; reg [3:0] dist; always @ (posedge clk) begin timer <= timer +1; //timer will start soon as positive clock pulse is detected if timer = 10; //It takes 10 us to send pulse from sensor trig = 1; //then,trig is high after 10us else if echo; //when echo is high, dist counting starts, trig goes to low, led turns on begin dist <= dist + 1; //echo pulse width trig <= 0; led = 1; end if dist <= 38; //max echo back time = 38 us without obstacles begin //then everything will be back to initial condition clear =1; dist <= 0; timer <=0; led <= 0; end end assign dist = dist * 340; //multiplying with speed of sound in air endmodule