module counter_24s( input clk,rst_n,en_stop_n,en_time,en_24s, output reg [15:0]times_24s ); parameter timer_1 = 19'd250_000; reg [18:0]cnt; reg div_1s; always@(posedge clk or negedge rst_n)begin if(!rst_n) begin cnt<=1'b0; div_1s<=1'b1; end else if(cnt==(timer_1-1))begin cnt<=1'b0; div_1s<=~div_1s; end else if(en_stop_n==0) cnt<= cnt; else cnt<=cnt+1'b1; end always @(posedge div_1s or negedge rst_n)begin if(!rst_n) times_24s[7:4]<=4'h0; else if(en_time==0) times_24s[7:4]<=4'h0; else if(en_24s==0) times_24s[7:4]<=4'h0; else if({times_24s[15:12],times_24s[11:8],times_24s[7:4],times_24s[3:0]}==16'h0000) times_24s[7:4]<=4'h0; else if({times_24s[7:4],times_24s[3:0]}==8'h00) times_24s[7:4]<=4'h9; else if(times_24s[3:0]==4'h0) times_24s[7:4]<=times_24s[7:4]-1'b1; else times_24s[7:4]<=times_24s[7:4]; end always@(posedge div_1s or negedge rst_n)begin if(!rst_n) times_24s[3:0]<=4'h0; else if(en_time==0) times_24s[3:0]<=4'h0; else if(en_24s==0) times_24s[3:0]<=4'h0; else if({times_24s[15:12],times_24s[11:8],times_24s[7:4],times_24s[3:0]}==16'h0000) times_24s[3:0]<=4'h0; else if(times_24s[3:0]==4'h0) times_24s[3:0]<=4'h9; else times_24s[3:0]<=times_24s[3:0]-1'b1; end always @(posedge div_1s or negedge rst_n)begin if(!rst_n) times_24s[15:12]<=4'h2; else if(en_time==0) times_24s[15:12]<=4'h2; else if(en_24s==0) times_24s[15:12]<=4'h2; else if({times_24s[15:12],times_24s[11:8],times_24s[7:4],times_24s[3:0]}==16'h0000) times_24s[15:12]<=4'h2; else if({times_24s[11:8],times_24s[7:4],times_24s[3:0]}==12'h000) times_24s[15:12]<=times_24s[15:12]-1'b1; else times_24s[15:12]<=times_24s[15:12]; end always@(posedge div_1s or negedge rst_n)begin if(!rst_n) times_24s[11:8]<=4'h4; else if(en_time==0) times_24s[11:8]<=4'h4; else if(en_24s==0) times_24s[11:8]<=4'h4; else if({times_24s[15:12],times_24s[11:8],times_24s[7:4],times_24s[3:0]}==16'h0000) times_24s[11:8]<=4'h4; else if({times_24s[11:8],times_24s[7:4],times_24s[3:0]}==12'h000) times_24s[11:8]<=4'h9; else if({times_24s[7:4],times_24s[3:0]}==8'h00) times_24s[11:8]<=times_24s[11:8]-1'b1; else times_24s[11:8]<=times_24s[11:8]; end endmodule
时间: 2024-03-29 22:36:06 浏览: 68
n_Bit_Counter.rar_counter_n bit counter
这是一个 Verilog HDL 语言编写的模块,名为 counter_24s,它实现了一个 24 小时计时器。这个模块有以下输入和输出:
输入:
- clk:时钟信号;
- rst_n:异步复位信号,低电平有效;
- en_stop_n:使能停止计数信号,低电平有效;
- en_time:使能计时信号,低电平有效;
- en_24s:使能 24 小时计时信号,低电平有效;
输出:
- times_24s:24 小时计时数值,16 位寄存器。
该模块的实现原理是在时钟上升沿或异步复位信号变化时对计时器进行更新。具体实现方式是在 always 块中根据不同的条件对计时器进行加/减/清零操作。在 always 块中还需要判断计时器是否被使能和是否达到了计时器的上限值。
阅读全文