首页 > 试题广场 >

简易秒表

[编程题]简易秒表
  • 热度指数:16064 时间限制: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比特位宽,秒表的分读数
`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

编辑于 2024-03-16 11:27:14 回复(0)
`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

发表于 2023-03-16 14:58:49 回复(0)
wire stop_cnt = (minute == 6'd60)&(second == 6'd1) ? 1 : 0;
   
always@(posedge clk or negedge rst_n)begin
    if(!rst_n ||stop_cnt)   second <= 0;
    else if(second == 6'd60)    second <= 6'd1;
    else        second <= second + 1;
end
always@(posedge clk or negedge rst_n)begin
    if(!rst_n) minute <= 0;
    else if(stop_cnt) minute <= minute;
    else if(second == 6'd60)    minute <= minute + 1;
end
发表于 2024-10-05 11:12: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 or negedge rst_n)
begin
    if(!rst_n)
    begin
        second = 0;
        minute = 0;
    end
    else
    begin
       
        minute = minute==60?minute:second==60?minute+1:minute;
        second = minute==60?0:second == 60?1:second+1;
    end

end
   
endmodule
编辑于 2024-03-25 22:30:21 回复(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)
begin
if(!rst_n) begin
    second<=6'd0;
    minute<=6'd0;
end
else begin
    second<=(second==60)? 6'd1:(minute==60)?1'b0:second+1'b1;
    minute<=(second==60)?(minute==60?60:minute+1'b1):minute;
    end
end
endmodule
发表于 2023-12-01 11:19:05 回复(0)
	//引用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)
行吧 是我傻了 60过来是0...
发表于 2023-11-02 20:05:20 回复(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, 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

发表于 2023-09-21 22:13:01 回复(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==1'b0)
       second <= 6'b0;
   else if(second == 60)
       second <=1;
   else
       second <=second +1'd1;
   

always@(posedge clk or negedge rst_n)
   if(rst_n==1'b0)
       minute <= 6'b0;  
        else if( second==60)
        minute <= minute +1'd1;
        else if(minute==60)
        minute <= 6'b0;  
       

   
   
endmodule
发表于 2023-08-01 17:51:09 回复(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) begin
        if(!rst_n) begin
            minute <= 0;
            second <= 0;
        end
        else begin
            if(second == 60) begin
                second <= 1;
                if(minute == 60) begin
                    second <= 0;
                end
                else begin
                    minute <= minute + 1;
                end
            end
            else begin
                second <= second + 1;
            end
        end
    end
   
endmodule
发表于 2023-07-23 21:53:52 回复(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)
            second<=0;
        else if(minute == 'd60)
            second <= 0;
        else if(second=='d60)
            second<=1;
        else
            second <= second + 1'b1;
   

    always@(posedge clk or negedge rst_n)
        if(!rst_n)
            minute <= 0;
        else if(minute=='d60)
            minute <= minute;
        else if(second=='d60)
            minute <= minute + 1'b1;
   
endmodule
发表于 2023-07-17 21:56:58 回复(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)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)
我用更短的代码解题。
我是初学者,没有学过Verilog,所以不是很懂,为什么要用两个always,我认为可以一个always解决的时候,为什么要两个呢??希望大佬指教
发表于 2023-05-06 15:08:49 回复(0)
`timescale 1ns/1ns

module count_module(
    input clk,
    input rst_n,

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

    // minute
    always@(posedge clk&nbs***bsp;negedge rst_n) begin
        if(~rst_n) 
            minute <= 'd0;
        else
            minute <= second == 'd60 ? minute + 1 :
                      minute == 'd60 ? 'd60 : minute; // if minute = 60, stop conuting
    end

endmodule
发表于 2022-12-15 23:06:15 回复(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)