秒记数器, 0-9循环

[复制链接]
 楼主| 34af9uc 发表于 2025-6-8 23:37 | 显示全部楼层 |阅读模式
  1. /******************秒记数器, 0-9循环****************/
  2. /**************************************************/
  3. /**************************************************/
  4. /******************20250529************************/
  5. `timescale 1ns/10ps
  6. module s_counter
  7. (
  8. clk,
  9. res,
  10. s_num
  11. );

  12. input                        clk;
  13. input                        res;
  14. output[3:0]                s_num;

  15. parameter                frequency_clk =24;                //24MHZ

  16. reg[24:0]                con_t;                        //秒脉冲分频计数器
  17. reg                                s_pulse;                //秒脉冲尖
  18. reg[3:0]                s_num;
  19. /**************************************************/                //复位
  20. always@(posedge clk or negedge res)
  21. begin

  22. if(~ res)
  23.         begin
  24.         con_t                <= 0;
  25.         s_pulse                <= 0;
  26.         s_num                <= 0;
  27.         end
  28. /**************************************************/                //秒计数
  29. else
  30.         begin
  31. //        if(con_t ==49_999_999)
  32.         //if(con_t ==frequency_clk *1000_000 -1)
  33.         if(con_t ==frequency_clk *100 -1)
  34.                 begin
  35.                 con_t                =0;
  36.                 end
  37.         else
  38.                 begin
  39.                 con_t                <=con_t +1;
  40.                 end
  41.         end
  42. /**************************************************/                //秒脉冲尖
  43. if(con_t ==0)
  44.         begin
  45.         s_pulse                <=1;
  46.         end
  47. else
  48.         begin
  49.         s_pulse                <=0;
  50.         end
  51. /**************************************************/                //秒记数
  52. if(s_pulse)
  53. begin
  54.         if(s_num ==9)
  55.         begin
  56.                 s_num <=0;
  57.         end
  58. end
  59. else
  60. begin
  61.         s_num <=s_num +1;
  62. end
  63. /**************************************************/

  64. end

  65. endmodule

  66. /**********testbench of s_counter******************/
  67. module s_counter_tb;
  68. reg                        clk, res;
  69. wire[3:0]                s_num;

  70. s_counter s_counter
  71.                                         (
  72.                                         .clk(clk),
  73.                                         .res(res),
  74.                                         .s_num(s_num)
  75.                                         );
  76. initial
  77. begin
  78.                         clk                <=0;
  79.                         res                <=0;
  80.         #17                res                <=1;
  81.         #1000        $stop;

  82. end

  83. always                #5        clk                <=~clk;

  84. endmodule
  85. /**************************************************/
  86. /**************************************************/


 楼主| 34af9uc 发表于 2025-6-10 23:26 | 显示全部楼层
秒记数器终于写好了
  1. /******************秒记数器, 0-9循环****************/
  2. /**************************************************/
  3. /**************************************************/
  4. /******************20250610************************/
  5. `timescale 1ns/10ps
  6. module s_counter
  7.                                 (
  8.                                 clk,
  9.                                 res,
  10.                                 s_num
  11.                                 );
  12. input                        clk;
  13. input                        res;
  14. output[3:0]                s_num;

  15. parameter                frequency_clk =24;                //24MHz

  16. reg[24:0]                con_t;                        //秒脉冲分频计数器;
  17. reg                                s_pulse;                //秒脉冲尖;
  18. reg        [3:0]                s_num;
  19. /**************************************************/
  20. always@(posedge clk or negedge res)
  21. if(~res)
  22.         begin
  23.         con_t                <=0;
  24.         s_pulse                <=0;
  25.         s_num                <=0;
  26.         end
  27. /**************************************************/
  28. else
  29.         begin
  30.         //if(con_t ==24000000-1)
  31.         //if(con_t ==frequency_clk*1000_000 -1)
  32.         if(con_t ==frequency_clk-1)
  33.                 begin
  34.                 con_t                <=0;
  35.                 end
  36.         else
  37.                 begin
  38.                 con_t                <=con_t +1;
  39.                 end
  40. /**************************************************/                //秒脉冲尖
  41. if(con_t ==0)
  42.         begin
  43.         s_pulse                <=1;
  44.         end
  45. else
  46.         begin
  47.         s_pulse                <=0;
  48.         end
  49. /**************************************************/
  50. if(s_pulse)
  51.         begin
  52.         if(s_num ==9)
  53.                 begin
  54.                 s_num                <=0;               
  55.                 end

  56.         else
  57.                 begin
  58.                 s_num                <=s_num+1;
  59.                 end



  60.         end


  61. /**************************************************/
  62.         end
  63. /**************************************************/


  64. endmodule


  65. /*************testbench of s_counter***************/
  66. module s_counter_tb;
  67. reg                                clk,res;       
  68. wire[3:0]                s_num;
  69. s_counter s_counter
  70.                                 (
  71.                                 .clk(clk),
  72.                                 .res(res),
  73.                                 .s_num(s_num)
  74.                                 );
  75. initial
  76. begin
  77.                                 clk <=0;
  78.                                 res <=0;
  79.                 #17                clk <=1;
  80.                                 res <=1;
  81.                 #1000        $stop;
  82. end

  83. always                #5                clk <=~clk;

  84. endmodule
  85. /**************************************************/
  86. /**************************************************/
miao3.png
 楼主| 34af9uc 发表于 2025-6-10 23:28 | 显示全部楼层
程序一开始判断那里写错了,通过单步调试才发现,现在秒记数对了.

miao3.zip

741 Bytes, 下载次数: 0

319

主题

487

帖子

63

粉丝
快速回复 在线客服 返回列表 返回顶部