1(4)计数器

Proteus仿真计数器

工程搭建

计数器从0计数到15,当计数到10时触发led灯
代码:

点击查看代码
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2024/11/21 15:41:56
// Design Name: 
// Module Name: count_module
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////
// Company:         xxx
// Engineer:        dahunzi
// 
// Create Date:     2023/07/01
// Design Name:     xxx
// Module Name:     xxx
// Project Name:    xxx
// Target Devices:  xxx
// Tool Versions:   VIVADO2017.4
// Description:     xxx
// 
// Dependencies:    xxx
// 
// Revision:     v0.1
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////

module count_module#(
    parameter       P_CNT_WIDTH = 8   
)(
    input                       i_clk       ,
    input                       i_rst       ,
    input                       i_en        ,    

    output  [P_CNT_WIDTH-1:0]   o_cnt       ,
    output                      o_led
);

/***************function**************/

/***************parameter*************/

/***************port******************/             

/***************mechine***************/

/***************reg*******************/
reg [P_CNT_WIDTH-1:0]           ro_cnt      ;
reg                             ro_led      ;


/***************wire******************/

/***************component*************/

/***************assign****************/
assign o_cnt = ro_cnt;
assign o_led = ro_led;

/***************always****************/
always @(posedge i_clk or negedge i_rst) begin
    if(!i_rst) begin
        ro_cnt <= 8'd0; //大位宽赋值给小位宽允许(自动截位),小位宽赋值给大位宽不允许(软件不会自动补位)
    end
    else begin
        ro_cnt <= ro_cnt + 1;
    end
end

always @(posedge i_clk or negedge i_rst) begin
    if(!i_rst) begin    //上电复位初值
        ro_led <= 1'd0; 
    end
    else if(ro_cnt == 8'd10) begin
            ro_led <= 1'd1;
        end
    else begin
        ro_led <= 1'd0;
    end
end

endmodule

这节课没上板)
他这个代码的i_en没用到
不过代码中没有直接将端口设置为reg,而是另外定义了reg寄存器然后assign给了端口的操作还是值得学习的

posted @ 2024-11-21 16:41  xuxuxu69  阅读(6)  评论(0编辑  收藏  举报