首页 新闻 搜索 专区 学院

黑金的按键实验

0
[已关闭问题] 关闭于 2016-05-13 21:50

官方提供的例程如下:

always @(posedge clk or negedge rst_n) //检测时钟的上升沿和复位的下降沿
begin
if(!rst_n) //复位信号低有效
count <= 20'd0; //计数器清0
else
begin
if(count ==20'd999_999) //20ms扫描一次按键,20ms计数(50M/50-1=999_999)
begin
count <= 20'b0; //计数器计到20ms,计数器清零
key_scan <= key_in; //采样按键输入电平
end
else
count <= count + 20'b1; //计数器加1
end
end
//=====================================================
// 按键信号锁存一个时钟节拍
//=====================================================
reg [3:0] key_scan_r;
always @(posedge clk)
key_scan_r <= key_scan;
wire [3:0] flag_key = key_scan_r[3:0] & (~key_scan[3:0]); //当检测到按键有下降沿变化时,代表该按键被按下,按键有效

我觉得这句单独一个always语句没必要,

always @(posedge clk)
key_scan_r <= key_scan; 

故把代码改成:

always @(posedge clk or negedge rst_n) //检测时钟的上升沿和复位的下降沿
begin
if(!rst_n) //复位信号低有效
count <= 20'd0; //计数器清0
else
begin
if(count ==20'd999_999) //20ms扫描一次按键,20ms计数(50M/50-1=999_999)
begin
count <= 20'b0; //计数器计到20ms,计数器清零
key_scan <= key_in; //采样按键输入电平
key_scan_r <= key_scan;
end
else
count <= count + 20'b1; //计数器加1
end
end
//=====================================================
// 按键信号锁存一个时钟节拍
//=====================================================
reg [3:0] key_scan_r;
//always @(posedge clk)
// key_scan_r <= key_scan;
wire [3:0] flag_key = key_scan_r[3:0] & (~key_scan[3:0]); 

之清的主页 之清 | 菜鸟二级 | 园豆:204
提问于:2016-05-13 21:38
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册