"); //-->
SEG数码管动态显示,采用扫描方式,轮询,只要满足刷新频率快于人眼视觉暂留时间即可。
程序比较简单,1s间隔分别显示0~7,8~F
代码:
//SEG数码管模块
module m_seg(clk, rst, seg, dig);
input clk; //系统时钟输入,50MHz
input rst; //系统复位输入
output[7:0] seg; //seg输出
output[7:0] dig; //dig输出
reg[6:0] seg_r;
reg[7:0] dig;
assign seg = { 1'b1, seg_r };
reg[15:0] scan; //扫描
//扫描时钟
always @(posedge clk or negedge rst)
begin
if(rst == 0)
scan = 16'd0;
else
scan = scan + 1'b1;
end
//片选
always @(scan[15:13])
begin
case(scan[15:13])
3'd0: dig <= ~8'h01;
3'd1: dig <= ~8'h02;
3'd2: dig <= ~8'h04;
3'd3: dig <= ~8'h08;
3'd4: dig <= ~8'h10;
3'd5: dig <= ~8'h20;
3'd6: dig <= ~8'h40;
3'd7: dig <= ~8'h80;
endcase
end
//译码
always @(scan[15:13] or n)
begin
case(scan[15:13] + n)
0: seg_r <= 7'b100_0000; // 0
1: seg_r <= 7'b111_1001; // 1
2: seg_r <= 7'b010_0100; // 2
3: seg_r <= 7'b011_0000; // 3
4: seg_r <= 7'b001_1001; // 4
5: seg_r <= 7'b001_0010; // 5
6: seg_r <= 7'b000_0010; // 6
7: seg_r <= 7'b101_1000; // 7
8: seg_r <= 7'b000_0000; // 8
9: seg_r <= 7'b001_1000; // 9
10: seg_r <= 7'b000_1000; // A
11: seg_r <= 7'b000_0011; // b
12: seg_r <= 7'b100_0110; // c
13: seg_r <= 7'b010_0001; // d
14: seg_r <= 7'b000_0110; // E
15: seg_r <= 7'b000_1110; // F
default: seg_r <= 7'b111_1111;
endcase
end
reg clk_1s; //分频时钟
reg [25:0] clk_cnt; //分频计数器
//系统时钟分频,产生1s的时钟
always @(posedge clk)
begin
if(clk_cnt == 25'd25000000)
begin
clk_1s = ~clk_1s;
clk_cnt = 25'd0;
end
else
begin
clk_cnt = clk_cnt + 1'b1;
end
end
reg[3:0] n;
always @(posedge clk_1s or negedge rst)
begin
if(rst == 0)
n <= 4'd0;
else
n <= n + 4'd8;
end
endmodule
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。