按键程序 Johnson计数器
module keyscan(
clk,
rst_n,
sw1_n,
sw2_n,
sw3_n,
//output
led_d3,
led_d4,
led_d5
);
input clk; //主时钟信号,48MHz
input rst_n; //复位信号,低有效
input sw1_n,sw2_n,sw3_n; //三个独立按键,低表示按下
output led_d3,led_d4,led_d5; //发光二极管,分别由按键控制
// ---------------------------------------------------------------------------
reg [19:0] cnt; //计数寄存器
always @ (posedge clk or negedge rst_n)
if (!rst_n) //异步复位
cnt <= 20'd0;
else
cnt <= cnt + 1'b1;
reg [2:0] low_sw;
always @(posedge clk or negedge rst_n)
if (!rst_n)
low_sw <= 3'b111;
else if (cnt == 20'hfffff) //满20ms,将按键值锁存到寄存器low_sw中
low_sw <= {sw3_n,sw2_n,sw1_n};
// ---------------------------------------------------------------------------
reg [2:0] low_sw_r; //每个时钟周期的上升沿将low_sw信号锁存到low_sw_r中
always @ ( posedge clk or negedge rst_n )
if (!rst_n)
low_sw_r <= 3'b111;
else
low_sw_r <= low_sw;
//当寄存器low_sw由1变为0时,led_ctrl的值变为高,维持一个时钟周期
wire [2:0] led_ctrl = low_sw_r[2:0] & ( ~low_sw[2:0]);
reg d1;
reg d2;
reg d3;
always @ (posedge clk or negedge rst_n)
if (!rst_n)
begin
d1 <= 1'b0;
d2 <= 1'b0;
d3 <= 1'b0;
end
else
begin //某个按键值变化时,LED将做亮灭翻转
if ( led_ctrl[0] ) d1 <= ~d1;
if ( led_ctrl[1] ) d2 <= ~d2;
if ( led_ctrl[2] ) d3 <= ~d3;
end
assign led_d5 = d1 ? 1'b1 : 1'b0; //LED翻转输出
assign led_d3 = d2 ? 1'b1 : 1'b0;
assign led_d4 = d3 ? 1'b1 : 1'b0;
endmodule
也许初看起来这段代码似乎有点吃力,好多的always好多的wire啊,而我们通常用得最多的判断转移好像不是主流。的确是这样,一个好的verilog代码,用多个always语句来分摊一个大的always来执行,会使得综合起来更快,这也是接前两篇日志说到代码优化的一个值得学习的方面。其次是wire连线很多,你要是仔细研究代码,不难发现所有的锁存器的连线关系编程者都考虑到了,这样就不会平白无故的生成意想不到的寄存器了,这也是一个优秀代码的必备要素。
上面说的是代码风格,下面就看程序的编程思想吧。前两个always语句里其实是做了一个20ms的计数,每隔20ms就会读取键值,把这个键值放到寄存器low_sw中,接下来的一个always语句就是把low_sw的值锁存到low_sw_r里,这样以来,low_sw和low_sw_r就是前后两个时钟周期里的键值了,为什么要这样呢?看下一个语句吧:
wire [2:0] led_ctrl = low_sw_r[2:0] & ( ~low_sw[2:0]);
仔细分析,你会发现当没有键按下时,low_sw=low_sw_r=3’b111,此时的led_ctrl=3’b000;只有当low_sw和low_sw_r的某一位分别为0和1时,才可能使led_ctrl的值改变(也就是把led_ctrl的某一位拉高)。那么这意味着当键值由1跳变到0时才可能把led_ctrl拉高。回顾前面的20ms赋键值,也就是说每20ms内如果出现按键被按下,那么有一个时钟周期里led_ctrl是会被拉高的,而再看后面的程序,led_ctrl的置高就使得相应的LED灯的亮灭做一次改变,这就达到了目的。
程序中用到了一个防抖动的按键下降沿检测电路,两级锁存
1 module test
2 (
3 input clk,
4 input rst_n,
5 input data_in,
6 output keyctr
7 );
8
9 reg [19:0] cnt;
10
11 always @ (posedge clk,negedge rst_n)
12 if(!rst_n)
13 cnt<=20'd0;
14 else
15 cnt<=cnt+1'b1;
16
17 reg low_sw;
18
19 always @ (posedge clk,negedge rst_n)
20 if(!rst_n)
21 low_sw<=1'b1;
22 else if(cnt==20'hfffff)
23 low_sw<=data_in;
24
25 reg low_sw_r;
26
27 always @(posedge clk,negedge rst_n)
28 if(!rst_n)
29 low_sw_r<=1'b1;
30 else
31 low_sw_r<=low_sw;
32
33 wire keyctr_r = low_sw_r & (~low_sw);
34
35 assign keyctr =keyctr_r;
36
37 endmodule
------------------------------------------------------------------------------------------------------------------------------
Verilog之Johnson计数器
带停止控制的双向4bit Johnson 计数器示例,可以通过LED灯直观的在开发板上进行演示。
KEY2: 控制向左移动
KEY3: 控制向右移动
KEY1: 第一次按下时将停止移动,再次按下时就会恢复移动
1 module test
2 (
3 input clk,
4 input rst_n,
5 input key1,
6 input key2,
7 input key3,
8 output [9:0] led
9 );
10
11 reg [19:0] cnt;
12
13 always @ (posedge clk,negedge rst_n)
14 if(!rst_n)
15 cnt<=20'd0;
16 else
17 cnt<=cnt+1'b1;
18
19 reg [2:0] low_sw;
20
21 always @ (posedge clk,negedge rst_n)
22 if(!rst_n)
23 low_sw<=3'b111;
24 else if(cnt==20'hfffff)
25 low_sw<={key3,key2,key1};
26
27 reg [2:0] low_sw_r;
28
29 always @(posedge clk,negedge rst_n)
30 if(!rst_n)
31 low_sw_r<=3'b111;
32 else
33 low_sw_r<=low_sw;
34
35 wire [2:0] key_en = low_sw_r & (~low_sw);
36
37 reg start,direction;
38
39 always @ (posedge clk,negedge rst_n)
40 if(!rst_n)
41 begin
42 start<=1'b0;
43 direction<=1'b0;
44 end
45 else
46 begin
47 if(key_en[0]) start <= ~start;
48 else if(key_en[1]) direction<=1'b0;
49 else if(key_en[2]) direction<=1'b1;
50 end
51 reg [9:0] led_r;
52
53 always @(posedge clk,negedge rst_n)
54 if(!rst_n)
55 led_r<=10'b0000000001;
56 else if(cnt==20'hfffff && start==1)
57 case(direction)
58 1 : led_r<={led_r[0],led_r[9:1]};
59 0 : led_r<={led_r[8:0],led_r[9]};
60 endcase
61
62 assign led = led_r;
63
64 endmodule