首页 > 试题广场 >

简易秒表

[编程题]简易秒表
  • 热度指数:16046 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

请编写一个模块,实现简易秒表的功能:具有两个输出,当输出端口second1-60循环计数,每当second计数到60,输出端口minute加一,一直到minute=60,暂停计数。

模块的接口信号图如下:


       模块的时序图如下:

      

      

请使用Verilog HDL实现以上功能,并编写testbench验证模块的功能

输入描述:
clk:系统时钟信号
rst_n:异步复位信号,低电平有效


输出描述:
second:6比特位宽,秒表的秒读数
minute:6比特位宽,秒表的分读数
	//引用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;

发表于 2023-12-01 09:41:32 回复(1)
`timescale 1ns/1ns

module count_module(
    input clk,
    input rst_n,

    output reg [5:0]second,
    output reg [5:0]minute
    );
   
    always @(posedge clk or negedge rst_n)begin
        if(!rst_n) begin
            second <= 0;
            minute <= 0;
        end
        else if(second==6'd60) begin second <= 1; minute <= minute + 1;end
        else    second <= second + 1;
        if(minute==6'd60) second <= 0;
    end
   
endmodule
发表于 2023-07-17 14:44:12 回复(0)
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

发表于 2023-06-06 17:19:48 回复(0)
`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

发表于 2022-09-05 10:29:04 回复(0)
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了, 可有逻辑有点混乱。
发表于 2022-08-18 11:14:47 回复(0)

注意系统复位时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
发表于 2022-08-17 10:02:40 回复(0)
感觉题目要求和tb都有点不合常理。应该会出现60:01跳60:00.
`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


发表于 2022-07-31 21:57:47 回复(0)
`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


发表于 2022-07-17 17:42:25 回复(0)
  reg  clock_en ;
  
  always@(posedge clk or negedge rst_n)begin
    if(!rst_n)
      minute <= 6'd0 ;
    else if(second == 6'd60)
      minute <= minute + 6'd1 ;
    else
      minute <= minute ;
  end 
  
  always@(posedge clk or negedge rst_n)begin
    if(!rst_n)
      second <= 6'd0 ;
    else if(second == 6'd60)
      second <= 6'd1 ;
    else if(clock_en)
      second <= 6'd0 ;
    else
      second <= second + 6'd1 ;
  end
  
  always@(posedge clk or negedge rst_n)begin
    if(!rst_n)
      clock_en <= 1'b0 ;
    else if((second == 6'd60) & (minute == 6'd59))
      clock_en <= 1'b1 ;
    else
      clock_en <= clock_en ;
  end
发表于 2022-07-13 00:40:04 回复(0)
`timescale 1ns/1ns

module count_module(
    input clk,
    input rst_n,

    output reg [5:0]second,
    output reg [5:0]minute
    );
    always@(posedge clk or negedge rst_n)
        if(!rst_n)
        minute<=0;
    else if(minute<=60)
        minute<=(second==6'd60)?minute+1:minute;
    else
        minute<=60;
    always@(posedge clk or negedge rst_n)
        if(!rst_n)
    second<=0;
    else if(minute<=60)
        second<=(second==6'd60)?1:second+1;
    else
        second<=0;
endmodule
            
发表于 2022-07-03 22:05:43 回复(1)
Notice: 满60后归1不归0。
`timescale 1ns/1ns

module count_module(
	input clk,
	input rst_n,

    output reg [5:0]second,
    output reg [5:0]minute
	);
    // 60 second = 1 min 
    always @ (posedge clk&nbs***bsp;negedge rst_n) begin
        if(~rst_n) begin
            second <= 0;
        end
        else begin
            second <= (second == 60) ? 1 : second + 1;
        end
    end
    // 60 min = next hour
    always @ (posedge clk&nbs***bsp;negedge rst_n) begin
        if(~rst_n) begin
            minute <= 0;
        end
        else begin
            minute <= (minute == 60) ? 1 : 
                      (second == 60) ? minute + 1 : 
                       minute;
        end
    end

endmodule


发表于 2022-06-14 11:06:54 回复(0)
`timescale 1ns/1ns

module count_module(
	input clk,
	input rst_n,

    output reg [5:0]second,
    output reg [5:0]minute
	);
    reg stop;
    always@(posedge clk&nbs***bsp;negedge rst_n)
        if(!rst_n)
            begin
                stop=1'd0;
                second<=6'd0;
                minute<=6'd0;
            end
    else if(minute==6'd60)
        begin
            stop<=1'd1;
        end
    else if(!stop&&second==6'd60)
        begin
            second<=6'd1;
            minute<=minute+1'd1;
        end    
    else if(!stop)
        second<=second+1'd1;
    else second<=second;
	
	
	
endmodule

发表于 2022-06-06 15:43:13 回复(0)