数码管的封装实验。显示使能信号置高才可以显示。对于小数点不用,故不显示。
数码管分为共阴数码管和共阳数码管,数码管不同,编码不同,下面是两种数码管显示0-F以及消隐的不同编码:
共阴数码管(高有效):
parameter seg_H_0 = 'h3f, seg_H_1 = 8'h06, seg_H_2 = 'h5b,
seg_H_3 = 'h4f, seg_H_4 = 8'h66, seg_H_5 = 'h6d,
seg_H_6 = 'h7d, seg_H_7 = 8'h07, seg_H_8 = 'h7f,
seg_H_9 = 'h67, seg_H_A = 8'h77, seg_H_B = 'h7c,
seg_H_C = 'h39, seg_H_D = 8'h5e, seg_H_E = 'h79,
seg_H_F = 'h71, seg_H_X = 8'h00;
共阳数码管(低有效):
parameter seg_L_0 = 'hc0, seg_L_1 = 8'hf9, seg_L_2 = 'ha4,
seg_L_3 = 'hb0, seg_L_4 = 8'h99, seg_L_5 = 'h92,
seg_L_6 = 'h82, seg_L_7 = 8'hf8, seg_L_8 = 'h80,
seg_L_9 = 'h98, seg_L_A = 8'h88, seg_L_B = 'h83,
seg_L_C = 'hc6, seg_L_D = 8'ha1, seg_L_E = 'h86,
seg_L_F = 'h8e, seg_L_X = 8'hff;
。
显示程序:
头文件
/********************************Copyright**************************************
**----------------------------File information--------------------------
** File name :seg_interface.v
** CreateDate :2015.04
** Funtions :数码管封装,6位,数据为23位,4位在一个数码管上显示
** Operate on :M5C06N3L114C7
** Copyright :All rights reserved.
** Version :V1.0
**---------------------------Modify the file information----------------
** Modified by :
** Modified data :
** Modify Content:
*******************************************************************************/ module seg_interface(
clk,
rst_n, segdisplay_en,
number_data, seg_data,
seg_bit );
input clk; /* clk = 24M */
input rst_n;
input [:] number_data; /* 输入的数据 */
input segdisplay_en; /* 数码管显示使能 */
output [:] seg_data;
output [:] seg_bit; wire [:] seg_data;
wire [:] seg_bit; wire [:] number_data_reg; smg_control U1(
.clk(clk),
.rst_n(rst_n), .segdisplay_en(segdisplay_en), .number_data(number_data), //in 23位输入数据
.seg_data_reg(number_data_reg), //数据的中间变量 out to U1
.seg_bit(seg_bit) //output 6 位数码管的位数
);
smg_encode U2(
.number_data_reg(number_data_reg), //In 要显示的数据的中间变量
.seg_data(seg_data)
); endmodule
底层:
/********************************Copyright**************************************
**----------------------------File information--------------------------
** File name :smg_control.v
** CreateDate :2015.04
** Funtions :数码管的位码控制和数据控制,状态机控制,1ms转移一位数码管
** Operate on :M5C06N3L114C7
** Copyright :All rights reserved.
** Version :V1.0
**---------------------------Modify the file information----------------
** Modified by :
** Modified data :
** Modify Content:
*******************************************************************************/ module smg_control(
clk,
rst_n, segdisplay_en,
number_data,
seg_data_reg,
seg_bit
);
input clk;
input rst_n;
input [:] number_data; //共使用6个数码管,每个4位数据,共24位
input segdisplay_en; output [:] seg_data_reg;
output [:] seg_bit;
wire [:] seg_data_reg;
wire [:] seg_bit; parameter t1ms = 'd23999; /* reality */ // parameter t1ms = 15'd3; /* just fo test */
reg [:] count1;
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
count1 <= 'd0;
else if(segdisplay_en)
begin
if(count1 == t1ms) count1 <= 'd0;
else count1 <= count1 + ;
end
else
count1 <= 'd0;
end //****************************************
reg [:] i;
reg [:] rnumber;
reg [:] rbit; always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
i <= 'd0;
rnumber <= 'd0;
rbit <= 'b111111; /* 有消隐的作用 */
end
else if(segdisplay_en)
begin
case(i)
'd0:
begin
if(count1 == t1ms)
i <= 'd1;
else
begin
rnumber <= number_data[:];
rbit <= 'b011111;
end
end
'd1:
begin
if(count1 == t1ms)
i <= 'd2;
else
begin
rnumber <= number_data[:];
rbit <= 'b101111;
end
end
'd2:
begin
if(count1 == t1ms)
i <= 'd3;
else
begin
rnumber <= number_data[:];
rbit <= 'b110111;
end
end
'd3:
begin
if(count1 == t1ms)
i <= 'd4;
else
begin
rnumber <= number_data[:];
rbit <= 'b111011;
end
end
'd4:
begin
if(count1 == t1ms)
i <= 'd5;
else
begin
rnumber <= number_data[:];
rbit <= 'b111101;
end
end
'd5:
begin
if(count1 == t1ms)
i <= 'd0;
else
begin
rnumber <= number_data[:];
rbit <= 'b111110;
end
end
default : i <= 'd0;
endcase
end
else
begin
i <= 'd0;
rnumber <= 'd0;
rbit <= 'b111111; /* 有消隐的作用 */
end
end assign seg_data_reg = rnumber;
assign seg_bit = rbit; endmodule
/********************************Copyright**************************************
**----------------------------File information--------------------------
** File name :smg_encode.v
** CreateDate :2015.04
** Funtions :数码管的编码文件,输入4位,输出8位
** Operate on :M5C06N3L114C7
** Copyright :All rights reserved.
** Version :V1.0
**---------------------------Modify the file information----------------
** Modified by :
** Modified data :
** Modify Content:
*******************************************************************************/ module smg_encode (
number_data_reg,
seg_data
);
input [:] number_data_reg; output [:] seg_data;
wire [:] seg_data; //parameter seg_0 = 8'b1100_0000, seg_1 = 8'b1111_1001, seg_2 = 8'b1010_0100,
// seg_3 = 8'b1011_0000, seg_4 = 8'b1001_1001, seg_5 = 8'b1001_0010,
// seg_6 = 8'b1000_0010, seg_7 = 8'b1111_1000, seg_8 = 8'b1000_0000,
// seg_9 = 8'b1001_0000;
// ----
// | |
// | |
// ----
// | |
// | |
// ---- 。 parameter seg_H_0 = 'h3f, seg_H_1 = 8'h06, seg_H_2 = 'h5b,
seg_H_3 = 'h4f, seg_H_4 = 8'h66, seg_H_5 = 'h6d,
seg_H_6 = 'h7d, seg_H_7 = 8'h07, seg_H_8 = 'h7f,
seg_H_9 = 'h67, seg_H_A = 8'h77, seg_H_B = 'h7c,
seg_H_C = 'h39, seg_H_D = 8'h5e, seg_H_E = 'h79,
seg_H_F = 'h71, seg_H_X = 8'h00; /*
parameter seg_L_0 = 8'hc0, seg_L_1 = 8'hf9, seg_L_2 = 8'ha4,
seg_L_3 = 8'hb0, seg_L_4 = 8'h99, seg_L_5 = 8'h92,
seg_L_6 = 8'h82, seg_L_7 = 8'hf8, seg_L_8 = 8'h80,
seg_L_9 = 8'h98, seg_L_A = 8'h88, seg_L_B = 8'h83,
seg_L_C = 8'hc6, seg_L_D = 8'ha1, seg_L_E = 8'h86,
seg_L_F = 8'h8e, seg_L_X = 8'hff;
*/
//*********************************************************
reg [:] rsmg;
always @(*)
begin
case(number_data_reg)
'h0: rsmg <= seg_H_0;
'h1: rsmg <= seg_H_1;
'h2: rsmg <= seg_H_2;
'h3: rsmg <= seg_H_3;
'h4: rsmg <= seg_H_4;
'h5: rsmg <= seg_H_5;
'h6: rsmg <= seg_H_6;
'h7: rsmg <= seg_H_7;
'h8: rsmg <= seg_H_8;
'h9: rsmg <= seg_H_9;
'ha: rsmg <= seg_H_A;
'hb: rsmg <= seg_H_B;
'hc: rsmg <= seg_H_C;
'hd: rsmg <= seg_H_D;
'he: rsmg <= seg_H_E;
'hf: rsmg <= seg_H_F;
default : rsmg <= seg_H_X;
endcase
end assign seg_data= rsmg; endmodule
测试文件:
/********************************Copyright**************************************
**----------------------------File information--------------------------
** File name :seg_tb.v
** CreateDate :2015.04
** Funtions : 数码管的测试文件
** Operate on :M5C06N3L114C7
** Copyright :All rights reserved.
** Version :V1.0
**---------------------------Modify the file information----------------
** Modified by :
** Modified data :
** Modify Content:
*******************************************************************************/ `timescale ns/ ns module seg_tb;
reg clk; /* clk = 24M */
reg rst_n;
reg [:] number_data; /* 输入的数据 */
reg segdisplay_en; wire [:] seg_data;
wire [:] seg_bit; seg_interface seg_interface1(
.clk,
.rst_n,
.number_data,
.segdisplay_en(segdisplay_en), .seg_data,
.seg_bit
); parameter tck = ;
parameter t = /tck; always
#(t/) clk = ~clk; initial
begin
clk = ;
rst_n = ;
number_data = ;
segdisplay_en = ; #(*t) rst_n = ;
#(*t) segdisplay_en = ;
number_data = 'h123456; #(*t) number_data = 'h789abc;
end
endmodule
仿真图:
注:
实验板上的数码管是共阳数码管:
但是实验板上驱动数码管使用的是PNP型的,所以驱动会相反,即是:共阳数码管的驱动是
位选择要为低,数据有效码值也为低(bit->0,segdata --> 8'hc0,则显示数值0).