数电实验1拓展
——模16可逆计数流水灯的VerilogHDL程序设计与仿真
一、实验目的
- 使用verilogHDL编程实现模16可逆计数流水灯
二、使用verilogHDL编程实现模16可逆计数流水灯
1、设计思路
- 由模16可逆计数器产生可逆的信号
- 再由4-16线译码器得到输出LED信号
2、实现代码
//文件名称:M_16.v
//文件名称:T_4_16.v
//文件名称:M_16.v
//fuchaoxinHUST03092021
module M_4(CP, CLR_, EN, PE, D, flag, LED);
input CP, EN, PE, CLR_, flag;
input [3:0] D;
output reg [3:0] LED;
/*说明:
输入端:CP时钟信号,上升沿敏感;EN使能信号,PE置数信号,flag方向信号,[3:0] D预置输入数组;
输出端:[3:0] LED输出数组。
*/
always @(posedge CP, negedge CLR_) begin
if(CLR_ == 0) LED = 4'b0000; //CLR_置零信号为0时,置零
else begin
if(EN == 1) begin
if(flag == 0)begin
if(LED == 4'b0000) LED = 4'b1111;
else LED <= LED - 1;
end
else begin
if(LED == 4'b1111) LED = 4'b0000;
else LED <= LED + 1;
end
end
else LED <= LED; //EN使能信号为0时,停止倒计时,HOLD
end
end
always @(posedge PE) begin
LED <= D; // 进行置数
end
endmodule
module T_4_16(A, EN, B);
input EN;
input [3:0] A;
output reg [15:0] B;
/*说明:
输入端:EN=1时才有有效输出
输出端:B为译码输出
*/
always @(*)
if (EN==1) begin
case(A[3:0])
4'b0000: B[15:0]=16'b0000000000000001;
4'b0001: B[15:0]=16'b0000000000000010;
4'b0010: B[15:0]=16'b0000000000000100;
4'b0011: B[15:0]=16'b0000000000001000;
4'b0100: B[15:0]=16'b0000000000010000;
4'b0101: B[15:0]=16'b0000000000100000;
4'b0110: B[15:0]=16'b0000000001000000;
4'b0111: B[15:0]=16'b0000000010000000;
4'b1000: B[15:0]=16'b0000000100000000;
4'b1001: B[15:0]=16'b0000001000000000;
4'b1010: B[15:0]=16'b0000010000000000;
4'b1011: B[15:0]=16'b0000100000000000;
4'b1100: B[15:0]=16'b0001000000000000;
4'b1101: B[15:0]=16'b0010000000000000;
4'b1110: B[15:0]=16'b0100000000000000;
4'b1111: B[15:0]=16'b1000000000000000;
endcase
end
else begin
B[15:0]=16'b0000000000000000;
end
endmodule
module M_16(CP, CLR_, EN_0, PE, flag, D, B);
input CP, CLR_, EN_0, PE, flag;
input [3:0] D;
output wire [15:0] B;
// 中间变量定义
wire [3:0]LED;
wire EN;
assign EN = 1;
M_4 U0(CP, CLR_, EN_0, PE, D, flag, LED);
T_4_16 U1(LED, EN, B);
endmodule
3、测试代码
//文件名称:Test_M_16.v
//fuchaoxinHUST03092021
`timescale 100ns/10ns
module Test_M_16;
reg CP, EN_0, PE, CLR_, flag;
reg [3:0] D;
wire [15:0] B;
M_16 M1(CP, CLR_, EN_0, PE, flag, D, B);
initial
$monitor($time,":\t CP=%b, CLR_=%b, EN_0=%b, PE=%b, flag=%b, D=%b, B=%b \n", CP, CLR_, EN_0, PE, flag, D, B);
//监视器的显示内容
initial
CP = 1;
always
#5 CP = ~CP;
// 产生CP上升沿
initial begin
EN_0 = 1; PE = 0; CLR_ = 0; D = 4'b1111; flag = 0;
#10
EN_0 = 1; PE = 0; CLR_ = 1; D = 4'b1111; flag = 1;
#100
EN_0 = 1; PE = 0; CLR_ = 1; D = 4'b1111; flag = 0;
#100
$stop;//停止模拟仿真
end
endmodule
4、仿真
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210322211502554.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FudG9uaW94dg==,size_16,color_FFFFFF,t_70)
- 监控器
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210322211510686.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FudG9uaW94dg==,size_16,color_FFFFFF,t_70)