您的位置:首页 > 其它

【厉害了FPGA】Verilog和VHDL对于一个always块或者一个process下的多个边沿触发事件处理

2018-03-18 22:30 267 查看
    在我们设计FPGA数字电路的时候,经常会遇到多个边沿触发事件的情况:
    比如:

    Verilog实例:

    我们有两路时钟信号,一路是1HZ的信号,一路是10M的时钟信号,现在实现在1HZ信号上升沿的时候开始用10M信号对1HZ信号进行计数的功能,如下图:


    1HZ信号触发开始计数功能,10M时钟信号触发计数寄存器+1操作。
    实现代码:

/**************对1pps计数模块*******************/
reg[23:0] Count_timerA;
reg[9:0] Count_timerB;
reg pps_int0;//对1pps的采样上升沿信号
reg pps_int1;

always@(posedge i_clk_10M or negedge i_reset)
begin
if(!i_reset)
begin
Count_timerA<=24'd0;
Count_timerB<=10'd0;
end
else
begin
pps_int0<=i_clk_1pps;
pps_int1<=pps_int0;
if((!pps_int1)&&(pps_int0))//检测到1pps的上升沿时计数寄存器复位
begin
Count_timerA<=24'd0;
Count_timerB<=10'd0;
end
if(Count_timerB<10'd1000)//
begin
Count_timerB<=Count_timerB+1;
end
else
begin
Count_timerB<=10'd0;
end
end
Count_timerB<=Count_timerB+1'b1;
end
/********************************************/    在此模块中使用了10M的时钟信号采样的方式检测出1HZ信号的上升沿:
    pps_int0<=i_clk_1pps;

    pps_int1<=pps_int0;
    由于使用的非阻塞赋值,在10M信号的第一个时钟沿来的时候pps_int0信号赋i_clk_1pps的值,pps_int1赋的是pps_int0没赋i_clk_1pps的pps_int0之前的值(即上一个时钟沿的i_clk_1pps的采样值),由此得到pps_int1是前一个时钟沿的i_clk_1pps的值,pps_int0是当前时钟沿i_clk_1pps的值。当满足(!pps_int1)&&(pps_int0))为'真'的时候即为1pps的上升沿。

    VHDL实例:

    在两个按键控制一个计数寄存器的“+”和“-”,假设按键是接上拉电阻的情况:按键按下输入为‘0’,按键不按下输入为‘1’,此时我们需要检测到“+”按键的一个下降沿的时候,触发寄存器+1,检测到一个“-”按键时,触发寄存器-1。

    和Verilog实现的原理相同,VHDL中是分两个进程去实现的,一个进程做延时,一个进程做判断。speed_add_reg信号需要在时
4000
钟沿的触发下才能更新数据,所以正好延时了一个时钟的信号。(下面有时序图) P1_aid:process(clk,reset)--延后一个时钟周期的按键+和按键-信号,用来异步检测其下降沿
begin
if(reset='0') then
speed_add_reg<='0';
speed_add_reg<='0';
elsif(clk'event and clk='1') then
speed_add_reg<=speed_add;
speed_dec_reg<=speed_dec;
end if;
end process P1_aid;

P1:process(reset,enable,clk)--通过控制计数满值new_cnt_fre来控制频率
begin
if(reset='0' or enable='0') then
speed<=0;
elsif(clk'event and clk='1') then
if(speed_add='0' and speed_add_reg='1') then--下降沿(按下速度“+”键),speed_add_reg是speed_add的上一个时钟状态
                        --此处省略一万行代码***************    
elsif(speed_dec='0' and speed_dec_reg='1') then
                        --此处省略一万行代码***************
end if;
end if;
end process P1;时序图:



    介个图画的真费劲,有啥画这种波形图的软件木有推荐的。。。

    time is late,洗洗睡了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐