本篇展示四个按键各控制一个数码管,按一次数字加1,显示从0到9循环
1.数码管、按键硬件原理如下
图1:数码管按键原理
2.代码准备与解读
(1)顶层模块,按键消抖和技术与前面相似。按键数码管关联关联四个控制块及扫描
-----------------------------------------------------------------------------------------------------
/*==============数码管显示==============/
reg [1:0]sel=0;
wire [3:0]dig0;
wire [7:0]smg0;
always @(posedge clk_100khz) //时钟上升沿计数
begin
sel <= `UD sel+1'b1;
end
//控制块0-3
seq_control seq_control_0
(
.sel(2'd3),
.key(key0_cnt),
.dig(dig0),
.smg(smg0)
);
wire [3:0]dig1;
wire [7:0]smg1;
seq_control seq_control_1
(
.sel(2'd2),
.key(key1_cnt),
.dig(dig1),
.smg(smg1)
);
wire [3:0]dig2;
wire [7:0]smg2;
seq_control seq_control_2
(
.sel(2'd1),
.key(key2_cnt),
.dig(dig2),
.smg(smg2)
);
wire [3:0]dig3;
wire [7:0]smg3;
seq_control seq_control_3
(
.sel(2'd0),
.key(key3_cnt),
.dig(dig3),
.smg(smg3)
);
//dig、seg扫描
always @(posedge clk_100khz)
begin
if(sel==2'b00)
dig <= `UD dig0;
else if(sel==2'b01)
dig <= `UD dig1;
else if(sel==2'b10)
dig <= `UD dig2;
else if(sel==2'b11)
dig <= `UD dig3;
end
always @(posedge clk_100khz)
begin
if(sel==2'b00)
smg <= `UD smg0;
else if(sel==2'b01)
smg <= `UD smg1;
else if(sel==2'b10)
smg <= `UD smg2;
else if(sel==2'b11)
smg <= `UD smg3;
end
endmodule
-----------------------------------------------------------------------------------------------------
(2)数码管显示模块定义了dig和seg位选择映射输出,控制数码管显示
------------------------------------------------------------------
/*===================================================
位选择映射
===================================================*/
always @(*)
begin
case(sel) //dig选择
2'd0:dig = 4'b0001;
2'd1:dig = 4'b0010;
2'd2:dig = 4'b0100;
2'd3:dig = 4'b1000;
default:dig = 4'b0000;
endcase
end
//共阳极数码管,为0有效,即点亮
always @(*)
begin
case(key) //数码管依键值显示数值0-9
4'd0:smg = 8'b1000_0001;//"0"
4'd1:smg = 8'b1100_1111;//"1"
4'd2:smg = 8'b1001_0010;//"2"
4'd3:smg = 8'b1000_0110;//"3"
4'd4:smg = 8'b1100_1100;//"4"
4'd5:smg = 8'b1010_0100;//"5"
4'd6:smg = 8'b1010_0000;//"6"
4'd7:smg = 8'b1000_1111;//"7"
4'd8:smg = 8'b1000_0000;//"8"
4'd9:smg = 8'b1000_0100;//"9"
default:smg = 8'b1111_1111;
endcase
end
------------------------------------------------------------------------
3.工程约束
这里改为硬件板上按键与上方数码管位置对应。工程约束如下
4.烧录程序运行
分别循环按按键KEY4、KEY3、KEY2、KEY1,数码管依次从0-9循环显示。实测效果见如下视频.
至此实现按键控制数码管显示