赞
踩
一、实验目的
1. 学习动态扫描显示数码管的使用。
2. 学习数据选择器及其信号分配方法。
3. 巩固 Verilog HDL 层次化设计电路的方法。
利用modelsim仿真
1.modelsim仿真代码
- `timescale 1 ps/1 ps
-
- module ljq_2344_5_vlg_tst();
-
- reg clk;
- reg en;
-
- wire [2:0] DIG;
- wire [1:0] Q;
- wire [3:0] Y;
- wire [6:0] codeout;
- wire [4:0] seg;
-
- ljq_2344_5 i1 (
- .DIG(DIG),
- .Q(Q),
- .Y(Y),
- .clk(clk),
- .codeout(codeout),
- .en(en),
- .seg(seg)
- );
-
- initial begin
- en = 1;
- clk = 1;
- end
- reg [25:0] counter = 26'b0;
- always begin
-
- #25 clk = ~clk;
- counter = counter + 1'b1;
- if(counter[20]) begin
- en = ~en;
- counter = 26'b0;
- end
- end
- endmodule
2.程序源代码
- module ljq_2344_5 (clk,en,DIG,seg,codeout,Y,Q);
- input clk,en; //时钟和使能信号
- output [1:0] Q; //计数信号
- output [3:0] Y;
- output [2:0] DIG; //位选信号,SEG3~SEG0,根据Q值变化
- output [6:0] codeout; //七段数码管译码信号
- output [4:0] seg; //位选信号,SEG7~SEG4, 恒为0
- wire clk1000;
- //调用子模块
- ljq_2344_5_0 clk_(clk, clk1000);
- ljq_2344_5_1 counter(clk1000,Q,en);
- ljq_2344_5_2 segswitch(Q,DIG,seg);
- ljq_2344_5_3 numselect(Q,Y);
- ljq_2344_5_4 decoder(Y,codeout);
-
- endmodule
- module ljq_2344_5_0(clk,clk1000);//分频器
-
- parameter clkin=50000000;
- parameter clkout=1000;
- input clk;
- output reg clk1000=0;
-
- reg [20:0] count;
-
- always @(posedge clk)
-
- begin
- if (count<(clkin/(2*clkout)-1))
- begin
- count<=count+1'b1;
- clk1000<=clk1000;
- end
- else
- begin
- clk1000<=~clk1000;
- count<=0;
- end
- end
- endmodule
- module ljq_2344_5_1(clk,Q,en); //3-digit计数器
- input clk,en;
- output reg[1:0]Q=2'b00;
- always @(posedge clk)
- if(en==1'b1)
- begin
- if(Q==2'd3)
- Q<=2'd1;
- else
- Q<=2'd1+Q;
- end
- else
- begin
- Q <= 2'd0;
- end
- endmodule
- module ljq_2344_5_2(x,DIG,seg); //2-3线译码器
- input [1:0] x;
- output reg[2:0] DIG; //位选信号
- output [4:0]seg; //位选信号
- assign seg=4'b0000;
- always @(x)
- begin
- case(x)
- 2'd1:DIG <= 4'b0001; //数码管SEG1亮
- 2'd2:DIG <= 4'b0010; //数码管SEG2亮
- 2'd3:DIG <= 4'b0100; //数码管SEG3亮
- default:DIG <= 4'b0; //默认不亮
- endcase
- end
- endmodule
- module ljq_2344_5_3(SEL,Y); //数据选择器
- input [1:0] SEL;
- output reg[3:0] Y;
- always @(SEL)
- begin
- case(SEL)
- 2'd1:Y <= 4'b0100;
- 2'd2:Y <= 4'b0100;
- 2'd3:Y <= 4'b0011;
- default: Y <=4'b0;
- endcase
- end
- endmodule
- module ljq_2344_5_4 (Y,codeout); //译码器
- input[3:0] Y;
- output[6:0] codeout;
- reg [6:0]codeout;
- always@(Y)
- begin
- case(Y)
- 4'd0 : codeout=7'b1111110;
- 4'd1 : codeout=7'b0110000;
- 4'd2 : codeout=7'b1101101;
- 4'd3 : codeout=7'b1111001;
- 4'd4 : codeout=7'b0110011;
- 4'd5 : codeout=7'b1011011;
- 4'd6 : codeout=7'b1011111;
- 4'd7 : codeout=7'b1110000;
- 4'd8 : codeout=7'b1111111;
- 4'd9 : codeout=7'b1111011;
- default: codeout=7'bx;
- endcase
- end
- endmodule
标准参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。