HDLBits Day12 count clock 做一个钟表

在这里插入图片描述
1.BCD码进位时,判断条件是 if(m < 8’h59),这里是用16进制数表示,即4位二进制数表示5,四位二进制数表示9,BCD数实际上就是十六进制数,不过是人为设置满10进1.
自己写的:

module top_module(
    input clk,
    input reset,
    input ena,
    output pm,
    output [7:0] hh,
    output [7:0] mm,
    output [7:0] ss);
    
    always@(posedge clk )
        if (reset) ss <= 8'h00;
   		else if(ena) begin
        if(ss == 8'h59)
           ss <= 8'h00;
        else   begin
            if(ss[3:0] < 4'h9)
               ss[3:0] <= ss[3:0] + 1'h1; 
             else
              begin
               ss[3:0] <= 0;
               ss[7:4] <= ss[7:4] + 1'h1;
              end 
        end
        end
    always@(posedge clk)
        if (reset) mm <= 8'h00;
   		else if(ena) begin
            if(ss == 8'h59)
                if(mm == 8'h59)
            mm <= 8'h00;
            else  
                if(mm[3:0] < 4'h9)
                                        begin
                                            mm[3:0] <= mm[3:0] + 1'h1; 
                                        end
                                    else
                                        begin
                                           mm[3:0] <= 0;
                                            mm[7:4] <= mm[7:4] + 1'h1;
                                        end 
                                end
       
    always@(posedge clk )
        if (reset) hh <= 8'h12;
   		else if(ena) begin
            if(mm == 8'h59 && ss == 8'h59) begin
                if(hh == 8'h12)
            hh <= 8'h01;
            else  
                if(hh[3:0] < 4'h9)
                                        begin
                                            hh[3:0] <= hh[3:0] + 1'h1; 
                                        end
                                    else
                                        begin
                                            hh[3:0] <= 0;
                                            hh[7:4] <= hh[7:4] + 1'h1;
                                        end 
            end
                                
        end
    always@(posedge clk )
             if (reset) pm <= 0;
    else if(hh ==  8'h11 && mm == 8'h59 && ss == 8'h59)
        pm =!pm;
        

endmodule

时分秒分开写的,以为这样理解起来简单。至于为什么用同步复位,异步复位时序不对,同步复位时序对了,就这样,题面里没有说明白,答案是这样。
人家写的:

module top_module 
    (
        input clk,
        input reset,
        input ena,
        output pm,
        output [7:0] hh,
        output [7:0] mm,
        output [7:0] ss
    );

    reg p;	//0 was am, 1 was pm
    reg [7:0] h;
    reg [7:0] m;
    reg [7:0] s;

    always @ (posedge clk)
        begin
            if(reset)
                begin
                    p <= 0;
                    h <= 8'h12;
                    m <= 8'h00;
                    s <= 8'h00;
                end
            else
                begin
                    if(ena)
                        begin
                            if(s < 8'h59)
                                begin
                                    if(s[3:0] < 4'h9)
                                        begin
                                            s[3:0] <= s[3:0] + 1'h1; 
                                        end
                                    else
                                        begin
                                            s[3:0] <= 0;
                                            s[7:4] <= s[7:4] + 1'h1;
                                        end 
                                end
                            else
                                begin
                                    s <= 0;
                                    if(m < 8'h59)
                                        begin
                                            if(m[3:0] < 4'h9)
                                                begin
                                                    m[3:0] <= m[3:0] + 1'h1; 
                                                end 
                                            else
                                                begin
                                                    m[3:0] <= 0;
                                                    m[7:4] <= m[7:4] + 1'h1;
                                                end
                                        end
                                    else
                                        begin
                                            m <= 1'h0;
                                            if(h == 8'h11)
                                                p = !p;
                                            if(h < 8'h12)
                                                begin
                                                    if(h[3:0] < 4'h9)
                                                        h[3:0] <= h[3:0] + 1'h1;
                                                    else
                                                        begin
                                                            h[3:0] <= 4'h0;
                                                            h[7:4] <= h[7:4] + 1'h1;
                                                        end
                                                end
                                            else
                                                begin
                                                   h <= 1'h1; 
                                                end
                                        end
                                end
                        end
                end
        end

    assign pm = p;
    assign hh = h;
    assign mm = m;
    assign ss = s;

endmodule 

tb文件:

`timescale 1ns / 1ns
module tb();

	reg clk,reset,ena;
	wire pm;
	wire[7:0] hh,mm,ss;
	syn_fifo syn_fifo 
    (
         clk,
        reset,
        ena,
         pm,
         hh,
         mm,
        ss
    );
	initial begin 
		clk = 1'b0;
		forever #10 clk = ~clk;
	end
	
	initial begin
		reset = 1'b0;
		ena = 1'b1;
		#100 reset = 1'b1;
		#100 reset = 1'b0;
	end
	
endmodule
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
制作一个简易钟表可以分为以下几个步骤: 1. 创建一个 Java 项目并导入需要的依赖库。 2. 创建一个窗口并设置窗口的大小和标题。 3. 创建一个 JPanel 并将其添加到窗口中。 4. 在 JPanel 中绘制钟表的表盘和指针。可以使用 Java 提供的 Graphics2D 类进行绘制。 5. 使用 Java 提供的 Timer 类实现钟表的时针和分针的运动。可以通过计算当前时间来确定时针和分针的角度,并在每秒钟更新它们的位置。 下面是一个简单的 Java 代码示例,可以实现一个简单的钟表: ``` import java.awt.*; import javax.swing.*; import java.util.*; import java.text.*; public class Clock extends JPanel { private int hour; private int minute; private int second; public void setTime() { Calendar now = Calendar.getInstance(); hour = now.get(Calendar.HOUR_OF_DAY); minute = now.get(Calendar.MINUTE); second = now.get(Calendar.SECOND); } public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int width = getWidth(); int height = getHeight(); int cx = width / 2; int cy = height / 2; int radius = Math.min(width, height) / 2 - 5; g2d.setColor(Color.WHITE); g2d.fillOval(cx - radius, cy - radius, 2 * radius, 2 * radius); g2d.setColor(Color.BLACK); g2d.drawOval(cx - radius, cy - radius, 2 * radius, 2 * radius); for (int i = 0; i < 12; i++) { double angle = Math.PI / 6 * i; int x1 = (int) (cx + (radius - 20) * Math.sin(angle)); int y1 = (int) (cy - (radius - 20) * Math.cos(angle)); int x2 = (int) (cx + radius * Math.sin(angle)); int y2 = (int) (cy - radius * Math.cos(angle)); g2d.drawLine(x1, y1, x2, y2); } double hourAngle = Math.PI / 6 * (hour % 12) + Math.PI / 360 * minute; double minuteAngle = Math.PI / 30 * minute; double secondAngle = Math.PI / 30 * second; int hourHand = radius / 2; int minuteHand = radius * 3 / 4; int secondHand = radius - 10; g2d.setStroke(new BasicStroke(3)); g2d.setColor(Color.RED); int x1 = (int) (cx + hourHand * Math.sin(hourAngle)); int y1 = (int) (cy - hourHand * Math.cos(hourAngle)); g2d.drawLine(cx, cy, x1, y1); g2d.setStroke(new BasicStroke(2)); g2d.setColor(Color.BLACK); x1 = (int) (cx + minuteHand * Math.sin(minuteAngle)); y1 = (int) (cy - minuteHand * Math.cos(minuteAngle)); g2d.drawLine(cx, cy, x1, y1); g2d.setStroke(new BasicStroke(1)); g2d.setColor(Color.BLUE); x1 = (int) (cx + secondHand * Math.sin(secondAngle)); y1 = (int) (cy - secondHand * Math.cos(secondAngle)); g2d.drawLine(cx, cy, x1, y1); } public static void main(String[] args) { JFrame frame = new JFrame("Clock"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setResizable(false); frame.setLocationRelativeTo(null); Clock clock = new Clock(); frame.add(clock); Timer timer = new Timer(1000, e -> { clock.setTime(); clock.repaint(); }); timer.start(); frame.setVisible(true); } } ``` 运行程序后,将会显示一个简单的钟表窗口,该窗口会不断地更新时间,并绘制出钟表的表盘和指针,效果如下图所示: ![clock.png](https://cdn.jsdelivr.net/gh/krislinzhao/ImgHosting/Java/clock.png)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值