当前位置:   article > 正文

Verilog实现手表计时

Verilog实现手表计时

实现手表的计时功能:

1.具有start启动信号、pause暂停信号,可以自定义其触发机制。

2.具有时间更改接口,可以更改时、分、秒。

3.输出时、分、秒。

Verilog设计

模块端口定义

  1. module watch1(
  2. input wire clk ,
  3. input wire rst_n ,
  4. input wire start , //
  5. input wire pause , //
  6. input wire h_add , //when it is 1, hour will add 1(when changing the time, the current time do not count)
  7. input wire m_add , //when it is 1, minute will add 1(when changing the time, the current time do not count)
  8. input wire s_add , //when it is 1, second will add 1(when changing the time, the current time do not count)
  9. output reg [4:0] hour ,
  10. output reg [5:0] minute ,
  11. output reg [5:0] second // second+1 per period
  12. );

手表计时使能:

  1. always@(posedge clk or negedge rst_n)
  2. if(!rst_n) running <= 1'b0;
  3. else if(pause && start) //push the keys in the same time, the watch still runs
  4. running <= 1'b1;//running;keep the state
  5. else if(pause) //
  6. running <= 1'b0;
  7. else if(start) //
  8. running <= 1'b1;
  9. else ;

或者:

  1. always@(posedge clk or negedge rst_n)
  2. if(!rst_n) running <= 1'b0;
  3. else if(start_rise) //push the key, the watch will run(higher priority)
  4. running <= 1'b1;
  5. else if(pause_rise) //push the key, the watch will stop
  6. running <= 1'b0;
  7. // else if(start_fall) //release the key, the watch will run
  8. // running <= 1'b1;
  9. else ;

小时:

  1. always@(posedge clk or negedge rst_n)
  2. if(!rst_n) hour <= 'b0;
  3. else if(h_add) begin
  4. if(hour == CNT_23)
  5. hour <= 'b0;
  6. else
  7. hour <= hour + 1'b1;
  8. end
  9. else if(running & ~m_add & ~s_add ) begin //when changing the time, the current time do not count
  10. if(second == CNT_59 && minute == CNT_59) begin
  11. if(hour == CNT_23)
  12. hour <= 'b0;
  13. else
  14. hour <= hour + 1'b1;
  15. end
  16. else ;
  17. end
  18. else ;

分钟:

  1. always@(posedge clk or negedge rst_n)
  2. if(!rst_n) minute <= 'b0;
  3. else if(m_add) begin
  4. if(minute == CNT_59)
  5. minute <= 'b0;
  6. else
  7. minute <= minute + 1'b1;
  8. end
  9. else if(running & ~s_add & ~h_add ) begin //when changing the time, the current time do not count
  10. if(second == CNT_59) begin
  11. if(minute == CNT_59)
  12. minute <= 'b0;
  13. else
  14. minute <= minute + 1'b1;
  15. end
  16. else ;
  17. end
  18. else ;

秒:

  1. always@(posedge clk or negedge rst_n)
  2. if(!rst_n) second <= 'b0;
  3. else if(s_add) begin
  4. if(second == CNT_59)
  5. second <= 'b0;
  6. else
  7. second <= second + 1'b1;
  8. end
  9. else if(running & ~m_add & ~h_add ) begin //when changing the time, the current time do not count
  10. if(second == CNT_59)
  11. second <= 'b0;
  12. else
  13. second <= second + 1'b1; // second+1 per period
  14. end
  15. else ;

仿真波形

时钟进位

启动&暂停:

或者:

顶层集成

  1. //
  2. module watch_top(
  3. input wire clk ,
  4. input wire rst_n ,
  5. input wire start_key , //按键:开始计时(按下按键时均为0)
  6. input wire pause_key , //按键:暂停计时
  7. input wire h_key , //按键:时+1
  8. input wire m_key , //按键:分+1
  9. input wire s_key , //按键:秒+1
  10. output wire [4:0] hour , //
  11. output wire [5:0] minute , //
  12. output wire [5:0] second //秒(每时钟周期+1)
  13. );
  14. // parameter ======================================================
  15. // wire =============================================================
  16. wire start;
  17. wire pause;
  18. wire h_add;
  19. wire m_add;
  20. wire s_add;
  21. // reg =============================================================
  22. // assign =============================================================
  23. // always ==========================================================
  24. // instantiation ======================================================================
  25. //
  26. key_filter u_start_filter(
  27. .clk (clk ),
  28. .rst_n (rst_n ),
  29. .key_in (start_key),
  30. .key_flag ( ),
  31. .key_out (start),
  32. .key_cont ()
  33. );
  34. key_filter u_pause_filter(
  35. .clk (clk ),
  36. .rst_n (rst_n ),
  37. .key_in (pause_key),
  38. .key_flag ( ),
  39. .key_out (pause),
  40. .key_cont ()
  41. );
  42. //
  43. key_filter u_h_filter(
  44. .clk (clk ),
  45. .rst_n (rst_n ),
  46. .key_in (h_key),
  47. .key_flag ( ),
  48. .key_out (),
  49. .key_cont (h_add)
  50. );
  51. key_filter u_m_filter(
  52. .clk (clk ),
  53. .rst_n (rst_n ),
  54. .key_in (m_key),
  55. .key_flag ( ),
  56. .key_out (),
  57. .key_cont (m_add)
  58. );
  59. key_filter u_s_filter(
  60. .clk (clk ),
  61. .rst_n (rst_n ),
  62. .key_in (s_key),
  63. .key_flag ( ),
  64. .key_out (),
  65. .key_cont (s_add)
  66. );
  67. //
  68. watch2 u_watch(
  69. .clk (clk ),
  70. .rst_n (rst_n ),
  71. .start (start ), //
  72. .pause (pause ), //
  73. .h_add (h_add ), //when it is 1, hour will add 1
  74. .m_add (m_add ), //when it is 1, minute will add 1
  75. .s_add (s_add ), //when it is 1, second will add 1
  76. .hour (hour ),
  77. .minute (minute),
  78. .second (second)
  79. );
  80. endmodule

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/400027
推荐阅读
相关标签
  

闽ICP备14008679号