首页 > 其他分享 >小梅哥课程学习——LED花式玩法(从计数器器到线性序列机)——实验五

小梅哥课程学习——LED花式玩法(从计数器器到线性序列机)——实验五

时间:2023-02-12 10:24:20浏览次数:51  
标签:reset LED clk 器到 counter input led 小梅哥

//让多个LED灯按照设置的模式,各自在一个变化值循环独立亮灭。
//源代码
module counter_led_5(
    clk,
    reset_n,
    Time,
    ctrlA,
    ctrlB,
    led
);
    input clk;
    input reset_n;
    input [31:0]Time;
    input [7:0]ctrlA;
    input [7:0]ctrlB;
    output reg [1:0] led;
    reg [31:0]counter;
    always @(posedge clk or negedge reset_n)
    if(!reset_n)
        counter<=0;
    else if(counter==Time-1)
        counter<=0;
    else
        counter<=counter+1'b1;
    reg [2:0] counter2;
    always@(posedge clk or negedge reset_n)
    if(!reset_n)
        counter2<=0;
    else if(counter==Time-1)
        counter2<=counter2+1'b1;
    else
        counter2<=counter2;
    always@(posedge clk or negedge reset_n)
    if(!reset_n)
        led<=0;
    else case(counter2)
        0:begin led[0]<=ctrlA[0];led[1]<=ctrlB[0];end
        1:begin led[0]<=ctrlA[1];led[1]<=ctrlB[1];end 
        2:begin led[0]<=ctrlA[2];led[1]<=ctrlB[2];end 
        3:begin led[0]<=ctrlA[3];led[1]<=ctrlB[3];end 
        4:begin led[0]<=ctrlA[4];led[1]<=ctrlB[4];end
        5:begin led[0]<=ctrlA[5];led[1]<=ctrlB[5];end
        6:begin led[0]<=ctrlA[6];led[1]<=ctrlB[6];end
        7:begin led[0]<=ctrlA[7];led[1]<=ctrlB[7];end
        default led<=led;
    endcase
 
endmodule
仿真源代码
`timescale 1ns/1ns
module counter_led_5_tb();
    reg clk;
    reg reset_n;
    reg [7:0]ctrlA;
    reg [7:0]ctrlB;
    reg [31:0]Time;//这里面两个的,位宽容易忘记写
    wire [1:0] led;
    counter_led_5 counter_led_5_inst0(
    .clk(clk),
    .reset_n(reset_n),
    .ctrlA(ctrlA),
    .ctrlB(ctrlB),
    .Time(Time),
    .led(led)
    );
    initial clk=1;
    always #10 clk=~clk;
    
    initial begin
    reset_n=0;
    ctrlA=0;//这个容易忘记给初值,
    ctrlB=0;
    Time=0;//这个容易忘记给初值,
    
    #201
    reset_n=1;
    ctrlA=8'b1000_0110;
    ctrlB=8'b1001_0110;
    Time=2500;
    #2000;
    
    $stop;
    end
endmodule

 

标签:reset,LED,clk,器到,counter,input,led,小梅哥
From: https://www.cnblogs.com/wangwin4/p/17113345.html

相关文章