vhdl-ultrasonic sensor(hc-sr04)

故事扮演 提交于 2019-12-08 14:09:05

问题


I have a project. In my project, I am creating a car that keeps the distance between anything next to the car and the car itself. But, coding is a headache for me. I created 3 different project, and all of them seemed to me okay. Yet none of them worked in practice. Then, I created this code(the most basic one to understand-- no component at all).

The sensor need 10 us pulse and waits for new pulse for 100ms. When I send the trigger signal, sensor responses and sends a ultrasonic wave. While it waits to recieve the echo back, the sensor sends 5v signal and and I find the duration of this signal. According to thisduration, I change the signal(motor) to 10,01 or to 11. In theory, ıt works but in preactice it does not work.

here is the code.

begin
  process(clk)
    variable c1,c2: integer:=0;
    variable y :std_logic:='1';
  begin
    if rising_edge(clk) then

        if(c1=0) then
            trig<='1';
        elsif(c1=500) then--100us
            trig<='0';
            y:='1';
        elsif(c1=5000000) then-- 100 ms
            c1:=0;
            trig<='1';
        end if;
        c1:=c1+1;

        if(echo = '1') then
            c2:=c2+1;
        elsif(echo = '0' and y='1' ) then-- I change the y to not get echo_time =0;
            echo_time<= c2;
            c2:=0;
            y:='0';
        end if;

        if(echo_time < 100000) then--20 cm
            motor<="10";
        elsif(echo_time > 150000)then--30 cm
            motor<="01";
        else-- between  
            motor<="11";
        end if;
    end if; 
end process ;

`

I could not solve my problem. Everything seems fine to me. I created a simulation and imaginary signal. On pc, it works, but in the real life it doesn't.

Pleeaaassssee help me.

Edit: Problem solved, code is working. I was giving 3.3V to sensor as Vcc, then I realized that I should give 5v. It worked. Thank you for everyone.


回答1:


Problem solved, code is working. I was giving 3.3V to sensor as Vcc, then I realized that I should give 5v. It worked. Thank you for everyone.



来源:https://stackoverflow.com/questions/27580741/vhdl-ultrasonic-sensorhc-sr04

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!