请编写一个模块,实现简易秒表的功能:具有两个输出,当输出端口second从1-60循环计数,每当second计数到60,输出端口minute加一,一直到minute=60,暂停计数。
模块的接口信号图如下:
模块的时序图如下:
请使用Verilog HDL实现以上功能,并编写testbench验证模块的功能
请编写一个模块,实现简易秒表的功能:具有两个输出,当输出端口second从1-60循环计数,每当second计数到60,输出端口minute加一,一直到minute=60,暂停计数。
模块的时序图如下:
clk:系统时钟信号rst_n:异步复位信号,低电平有效
second:6比特位宽,秒表的秒读数minute:6比特位宽,秒表的分读数
`timescale 1ns/1ns module count_module( input clk, input rst_n, output reg [5:0]second, output reg [5:0]minute ); always @(posedge clk&nbs***bsp;negedge rst_n) begin if (!rst_n)begin second<=0; minute<=0; end else begin if (second<60)begin second<=second+1; end else begin minute<=minute+1; second<=1; end end end endmodule
`timescale 1ns/1ns module count_module( input clk, input rst_n, output reg [5:0]second, output reg [5:0]minute ); wire flag; //1:允许计数;0:停止计数 //秒计数 always@(posedge clk&nbs***bsp;negedge rst_n) begin if(!rst_n) second <= 'd0; else if(flag && (second == 6'd60)) second <= 6'd1; else if(flag) second <= second + 1'b1; else second <= 6'd0; end //分钟计数 always@(posedge clk&nbs***bsp;negedge rst_n) begin if(!rst_n) minute <= 6'd0; else if(flag && (second == 6'd60)) minute <= minute + 1'b1; else minute <= minute; end //计数标志 assign flag = (minute==6'd60)?1'b0:1'b1; endmodule
//引用flag信号标志 1允许计数,0不允许计数 wire flag; assign flag= ((minute=='d60)?'d0:'d1); always@(posedge clk&nbs***bsp;negedge rst_n) if(!rst_n)second<='d0; else if(flag&&(second=='d60)) second<='d1; else if(flag) second<=second+1'b1; else second<=second; always@(posedge clk&nbs***bsp;negedge rst_n) if(!rst_n) minute<='d0; else if(flag&&(second=='d60)) minute<=minute+1'b1; else minute<=minute;
`timescale 1ns/1ns module count_module( input clk, input rst_n, output reg [5:0]second, output reg [5:0]minute ); always @(posedge clk, negedge rst_n) begin if(!rst_n) begin second <= 6'd0; minute <= 6'd0; end else begin if(second == 6'd60) begin second <= 6'd1; if(minute == 6'd60) minute <= 6'd1; else minute <= minute + 1'b1; end else second <= second + 1'b1; end end endmodule
module count_module( input clk, input rst_n, output reg [5:0]second, output reg [5:0]minute ); always @(posedge clk&nbs***bsp;negedge rst_n) begin if( !rst_n ) begin second <= 6'd0; end else begin if( minute == 60 ) second <= 6'd0; else if( second == 60 ) second <= 6'd1; else second <= second + 1'b1; end end always @(posedge clk&nbs***bsp;negedge rst_n) begin if( !rst_n ) begin minute <= 6'd0; end else begin if( minute == 60 ) minute <= 6'd60; else if( second == 60 ) minute <= minute + 1'b1; else minute <= minute; end end endmodule
`timescale 1ns/1ns module count_module( input clk, input rst_n, output reg [5:0]second, output reg [5:0]minute ); always@(posedge clk&nbs***bsp;negedge rst_n) if(!rst_n) second<=6'd0; else if(second==6'd60) second<=6'd1; else if(second==6'd60) second<=6'd0; else second<=second+1'b1; always@(posedge clk&nbs***bsp;negedge rst_n) if(!rst_n) minute<=6'd0; else if(second==6'd60)begin if(minute==6'd60) minute<=6'd1; else minute<=minute+1'b1; end endmodule
module count_module ( input clk, input rst_n, output reg [5:0] second, output reg [5:0] minute ); parameter SIXTY = 60; reg pause; always @(posedge clk&nbs***bsp;rst_n) begin if(!rst_n)begin second <= 0; minute <= 0; pause <= 0; end else begin if(!pause) begin second <= second + 1; if(second == SIXTY)begin second <= 1; minute <= minute + 1; end else minute <= minute; if(minute >= SIXTY)begin pause <= 1; end else pause <= 0; end else second <= 0; end end endmodule全部写进一个always了, 可有逻辑有点混乱。
注意系统复位时second是0,但作为秒计数器复位是6'd1。
`timescale 1ns/1ns module count_module( input clk, input rst_n, output reg [5:0]second, output reg [5:0]minute ); parameter CNT_MAX = 6'd60; always @ (posedge clk or negedge rst_n) begin if (~rst_n || minute == CNT_MAX) second <= 6'd0; else second <= (second == CNT_MAX)? 6'd1: (second + 1'b1); end always @ (posedge clk or negedge rst_n) begin if (~rst_n) minute <= 6'd0; else minute <= (second == CNT_MAX)? (minute + 1'b1): minute; end endmodule
`timescale 1ns/1ns module count_module( input clk, input rst_n, output reg [5:0]second, output reg [5:0]minute ); always@(posedge clk&nbs***bsp;negedge rst_n) begin if(!rst_n) begin second <= 0; minute <= 0; end else begin second <= minute==60?0: // 这是否有些问题?会出现60:01跳60:00. second==60?1: second+1; minute <= minute==60?60: second==60?minute+1: minute; end end endmodule
`timescale 1ns/1ns module count_module( input clk, input rst_n, output reg [5:0]second, output reg [5:0]minute ); always@(posedge clk&nbs***bsp;negedge rst_n) begin if(!rst_n) begin second <= 0; minute <= 0; end else begin second <= minute == 60 ? 0 : (second == 60 ? 1 : second + 1); minute <= minute == 60 ? minute : (second == 60 ? minute + 1 : minute); end end endmodule