• [HDLBits] Count clock


    Create a set of counters suitable for use as a 12-hour clock (with am/pm indicator). Your counters are clocked by a fast-running clk, with a pulse on ena whenever your clock should increment (i.e., once per second).

    reset resets the clock to 12:00 AM. pm is 0 for AM and 1 for PM. hh, mm, and ss are two BCD (Binary-Coded Decimal) digits each for hours (01-12), minutes (00-59), and seconds (00-59). Reset has higher priority than enable, and can occur even when not enabled.

    The following timing diagram shows the rollover behaviour from 11:59:59 AM to 12:00:00 PM and the synchronous reset and enable behaviour.

    1. module top_module(
    2. input clk,
    3. input reset,
    4. input ena,
    5. output pm,
    6. output [7:0] hh,
    7. output [7:0] mm,
    8. output [7:0] ss);
    9. wire [2:0] ena_wire; //进位标识符
    10. reg pm_test;
    11. assign ena_wire[0]=ena;
    12. bcd seconds(clk,reset,ena_wire[0],ss); //sec
    13. assign ena_wire[1]=(ss==8'h59)&&ena;
    14. bcd minutes(clk,reset,ena_wire[1],mm); //min
    15. assign ena_wire[2]=(ss==8'h59&&mm==8'h59)&&ena;
    16. bcd_hour hours(clk,reset,ena_wire[2],hh); //hour
    17. always@(posedge clk) begin //p.m.
    18. if(reset)
    19. pm_test<=0;
    20. else if(ss==8'h59&&mm==8'h59&&hh==8'h11)
    21. pm_test=~pm_test;
    22. else;
    23. end
    24. assign pm=pm_test;
    25. endmodule
    26. module bcd(
    27. input clk,
    28. input reset,
    29. input ena,
    30. output reg[7:0] q
    31. );
    32. always@(posedge clk) begin
    33. if(reset)
    34. q<=0;
    35. else if(ena) begin //若需进位
    36. if(q==8'h59) //且到了59,进位为0
    37. q<=0;
    38. else if(q[3:0]==9) begin //不是59就要考虑有两位数字,个位若为9则进位,十位+1
    39. q[3:0]<=4'b0;
    40. q[7:4]<=q[7:4]+1;
    41. end
    42. else //个位不为9就不进位,个位+1
    43. q[3:0]<=q[3:0]+1;
    44. end
    45. //else;
    46. end
    47. endmodule
    48. module bcd_hour(
    49. input clk,
    50. input reset,
    51. input ena,
    52. output reg[7:0] q
    53. //output pm
    54. );
    55. always@(posedge clk) begin
    56. //pm=0;
    57. if(reset)
    58. q<=8'h12; //和上面几乎一样,就这里reset是12需要注意
    59. else if(ena) begin
    60. if(q==8'h12) begin
    61. q<=1;
    62. //pm=~pm;
    63. end
    64. else if(q[3:0]==9) begin
    65. q[3:0]<=4'b0;
    66. q[7:4]<=q[7:4]+1;
    67. end
    68. else
    69. q[3:0]<=q[3:0]+1;
    70. end
    71. //else;
    72. end
    73. endmodule
    74. /*
    75. 这道题有坑,就是ss mm hh这种都有两位,ss这两位之间也需要进位。
    76. 以及这里的进位判定都是'h59,不知道为什么不是'd59
    77. 以及分钟和秒用的bcd计数器可以是同一个,但小时的要单独写。
    78. HDLBits resoultion 的答案我没看懂
    79. */

    这道题有坑,就是ss mm hh这种都有两位,ss这两位之间也需要进位。
    以及这里的进位判定都是'h59,不知道为什么不是'd59
    以及分钟和秒用的bcd计数器可以是同一个,但小时的要单独写。
    HDLBits resoultion 的答案我没看懂

  • 相关阅读:
    辊轧机液压系统泵站比例阀放大器
    std::function
    你真的会开发测试框架?
    聊聊HttpClient的ConnectionBackoffStrategy
    FFmpeg 解码 H.264 视频出现花屏和马赛克的解决办法
    vulnhub靶机DC1
    [Spring Boot]10 使用RestTemplate调用第三方接口
    安全扫描项目
    Outlook 邮箱使用技巧
    ROS2——动作(十一)
  • 原文地址:https://blog.csdn.net/m0_66480474/article/details/133151386