查看: 1071|回复: 0

基于Altera MAX 10的超声波数据读取

[复制链接]
  • TA的每日心情
    开心
    2018-7-3 21:49
  • 签到天数: 750 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2015-1-20 14:58:55 | 显示全部楼层 |阅读模式
    分享到:
    之前在spartan6的平台上做过一个小车的自主避障,用的就是3个超声波传感器。
    (1)超声波模块接线:



    +5V接VCC
    触发信号输入(10us的TTL脉冲) Trig
    回响信号输出(输出TTL电平信号,与射程成比例)
                   OUT不接(也有些超声波没有这个引脚)
                   GND接地
    注意:不要带电连接。
    (2)超声波模块时序图

    需要提供一个短期的10us脉冲触发信号。该模块内部将发出8个40Khz周期电平并检测回波。一旦检测到有回波信号则输出回响信号,它是一个脉冲的宽度成正比的距离对象,为了保证发射信号对回响信号的影响,触发信号的周期最好60ms,太小了有影响,太大了测的不准。
    即FPGA提供一个trig信号,周期为60ms左右,高电平为10us。

    然后就是检测echo信号了,设回响电平的宽度为某x us,计算为x /58,单位是cm,即将测得的回响电平的宽度(us单位),除以58,就得到了障碍物的距离。这个公式很好理解,其实就是我们在初中物理的一个回声的公式s=ct/2

    我这里用了一个distance_cnt来保存echo的高电平长度,echo的高电平长度=distance_cnt*1000/48(ns,晶振为50m),化为us,为distance_cnt/48,
    然后计算距离就是s= distance_cnt/48/58= distance_cnt/2784,这里我就用了一个除法器了。



    然后就是得到了结果就保存到distance里面了,这里超声波的测量距离只有几米
    Verilog设计:
    <span style="color: rgb(0, 0, 0);">module            sonic(                                                                clk,                                                                rst_n,                                                                echo,                                                                trig,                                                                distance);input clk;input rst_n;input echo;output trig;output [23 : 0] distance;reg [21:0]cnt;always @(posedge clk)begin        if(!rst_n)                cnt<=0;        else        if(cnt==22'd2880000)                                cnt<=0;                        else                                cnt<=cnt+1'b1;endassign trig=((cnt>=22'd100) & (cnt<=22'd579)) ? 1:0;reg echo_reg1,echo_reg2;wire start,finish;always @(posedge clk)begin        if(!rst_n)                begin                        echo_reg1<=0;                        echo_reg2<=0;                end        else                begin                        echo_reg1<=echo;                        echo_reg2<=echo_reg1;                endendassign start=echo_reg1 & (~echo_reg2);   assign finish=(~echo_reg1) & echo_reg2;  reg [23:0]distance_cnt;reg [23:0]distance_temp;parameter idle=2'b00;parameter state1=2'b01;parameter state2=2'b10;reg [1:0]state;always @(posedge clk)begin        if(!rst_n)                begin                                state<=idle;                        distance_cnt<=0;                        distance_temp<=0;                end        else                        begin                        case(state)                        idle:         begin                                                        if(start)                                                                state<=state1;                                                        else                                                                state<=idle;                                                end                        state1:        begin                                                        if(finish)                                                                state<=state2;                                                        else                                                                begin                                                                        distance_cnt<=distance_cnt+1'b1;                                                                        state<=state1;                                                                end                                                end                        state2:  begin                                                        distance_cnt<=0;                                                        distance_temp<=distance_cnt;                                                        state<=idle;                                                end                        default: state<=idle;                        endcase                endendwire [23 : 0] distance;wire [11:0] divisor;  //assign divisor=12'd2900;  50m assign divisor=12'd2784;wire [11 : 0] fractional; div        div(                        .denom ( divisor ),                        .numer ( distance_temp ),                        .quotient ( distance ),                        .remain ( fractional )                        );endmodule</span>
    这里max10评估板没有数码管之类的显示模块,那么我就试用signaltap来观察数据了。
    实验接线图:

    当我没有遮挡超声波的时候,测到的是275cm,基本上应该就是顶的距离了。

    当我用手遮挡的时候,
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-25 17:54 , Processed in 0.123556 second(s), 18 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.