• ZYNQ_project:key_led


    条件里是十进制可以不加进制说明,编译器默认是10进制,其他进制要说明。

    实验目标:

     模块框图:

    时序图:

    代码:

    1. `include "para.v"
    2. module key_filter (
    3. input wire sys_clk ,
    4. input wire sys_rst_n ,
    5. input wire [`key_length -1 :0] key_in ,
    6. output reg [`key_length -1:0] key_flag
    7. );
    8. reg [`key_length -1 :0] key_in_r1 ;
    9. reg [`key_length -1 :0] key_in_r2 ;
    10. reg [3:0] state_c ;
    11. reg [3:0] state_n ;
    12. reg [19:0] cnt_core ;
    13. wire nege ;
    14. wire pose ;
    15. wire cnt_done_filter ;
    16. wire IDLEtoFILTER_UP ;
    17. wire FILTER_UPtoSAMPLING ;
    18. wire FILTER_UPtoIDLE ;
    19. wire SAMPLINGtoFILTER_BACK ;
    20. wire FILTER_BACKtoIDLE ;
    21. localparam IDLE = 4'b0001 ,
    22. FILTER_UP = 4'b0010 ,
    23. SAMPLING = 4'b0100 ,
    24. FILTER_BACK = 4'b1000 ;
    25. /*********************************************************************/
    26. // reg [`key_length -1 :0] key_in_r1 ;
    27. // reg [`key_length -1 :0] key_in_r2 ;
    28. always @(posedge sys_clk or negedge sys_rst_n) begin
    29. if(~sys_rst_n) begin
    30. key_in_r1 <= 2'b11 ;
    31. key_in_r2 <= 2'b11 ;
    32. end
    33. else begin
    34. key_in_r1 <= key_in ;
    35. key_in_r2 <= key_in_r1 ;
    36. end
    37. end
    38. // wire nege ;
    39. assign nege = |(~key_in_r1 & key_in_r2) ;
    40. // wire pose ;
    41. assign pose = |( key_in_r1 & ~key_in_r2 );
    42. // reg [3:0] state_c ;
    43. always @(posedge sys_clk or negedge sys_rst_n) begin
    44. if(~sys_rst_n)
    45. state_c <= IDLE ;
    46. else
    47. state_c <= state_n ;
    48. end
    49. // reg [3:0] state_n ;
    50. always @(*) begin
    51. case(state_c)
    52. IDLE : if(IDLEtoFILTER_UP)
    53. state_n = FILTER_UP ;
    54. else
    55. state_n = IDLE ;
    56. FILTER_UP : if(FILTER_UPtoSAMPLING)
    57. state_n = SAMPLING ;
    58. else if(FILTER_UPtoIDLE)
    59. state_n = IDLE ;
    60. else
    61. state_n = FILTER_UP ;
    62. SAMPLING : if(SAMPLINGtoFILTER_BACK)
    63. state_n = FILTER_BACK ;
    64. else
    65. state_n = SAMPLING ;
    66. FILTER_BACK : if(FILTER_BACKtoIDLE)
    67. state_n = IDLE ;
    68. else
    69. state_n = FILTER_BACK ;
    70. default : state_n = IDLE ;
    71. endcase
    72. end
    73. assign IDLEtoFILTER_UP = state_c == ( IDLE ) && ( nege ) ;
    74. assign FILTER_UPtoSAMPLING = state_c == ( FILTER_UP ) && ( cnt_done_filter ) ;
    75. assign FILTER_UPtoIDLE = state_c == ( FILTER_UP ) && ( pose ) ;
    76. assign SAMPLINGtoFILTER_BACK = state_c == ( SAMPLING ) && ( pose ) ;
    77. assign FILTER_BACKtoIDLE = state_c == ( FILTER_BACK ) && ( cnt_done_filter ) ;
    78. // reg [19:0] cnt_core ;
    79. always @(posedge sys_clk or negedge sys_rst_n) begin
    80. if(~sys_rst_n)
    81. cnt_core <= 20'd0 ;
    82. else
    83. case (state_c)
    84. IDLE : cnt_core <= 20'd0 ;
    85. FILTER_UP : if( cnt_core == `MAX_CNT_10MS - 1 )
    86. cnt_core <= 20'd0 ;
    87. else
    88. cnt_core <= cnt_core + 1'b1 ;
    89. SAMPLING : cnt_core <= 20'd0 ;
    90. FILTER_BACK : if( cnt_core == `MAX_CNT_10MS - 1 )
    91. cnt_core <= 20'd0 ;
    92. else
    93. cnt_core <= cnt_core + 1'b1 ;
    94. default : cnt_core <= 20'd0 ;
    95. endcase
    96. end
    97. // cnt_done_filter
    98. assign cnt_done_filter = ( cnt_core == `MAX_CNT_10MS - 1 ) ;
    99. // reg [`key_length -1:0] key_flag
    100. always @(posedge sys_clk or negedge sys_rst_n) begin
    101. if(~sys_rst_n)
    102. key_flag <= 0 ;
    103. else if(FILTER_UPtoSAMPLING)
    104. key_flag <= ~key_in_r2 ;
    105. else
    106. key_flag <= 0 ;
    107. end
    108. endmodule
    1. // led闪烁实验,间隔0.25s
    2. `include "para.v"
    3. module led(
    4. input wire sys_clk ,
    5. input wire sys_rst_n ,
    6. input wire [1:0] key_in ,
    7. output reg [1:0] led_out
    8. );
    9. reg [1:0] led_mod ;
    10. reg [23:0] cnt_25ms ;
    11. wire cnt_25ms_flag ;
    12. // led_mod
    13. always @(posedge sys_clk or negedge sys_rst_n) begin
    14. if(~sys_rst_n)
    15. led_mod <= 2'b00 ;
    16. else if((led_mod == 2'b01 && key_in == 2'b01) || (led_mod == 2'b10 && key_in == 2'b10))
    17. led_mod <= 2'b00 ;
    18. else if(key_in == 2'b01)
    19. led_mod <= 2'b01 ;
    20. else if(key_in == 2'b10)
    21. led_mod <= 2'b10 ;
    22. else
    23. led_mod <= led_mod ;
    24. end
    25. // cnt_25ms
    26. always @(posedge sys_clk or negedge sys_rst_n) begin
    27. if(~sys_rst_n)
    28. cnt_25ms <= 24'd0 ;
    29. else if(led_mod == 2'b01 || led_mod == 2'b10) begin
    30. if(cnt_25ms == `MAX_CNT_250MS - 1)
    31. cnt_25ms <= 24'd0 ;
    32. else
    33. cnt_25ms <= cnt_25ms + 1'b1 ;
    34. end
    35. else
    36. cnt_25ms <= 24'd0 ;
    37. end
    38. assign cnt_25ms_flag = (cnt_25ms == `MAX_CNT_250MS - 1) ;
    39. // led_out
    40. always @(posedge sys_clk or negedge sys_rst_n) begin
    41. if(~sys_rst_n)
    42. led_out <= 2'b11 ;
    43. else
    44. case (led_mod)
    45. 2'b00: led_out <= 2'b11 ;
    46. 2'b01: if(led_out[1] == led_out[0])
    47. led_out <= 2'b10 ;
    48. else if(cnt_25ms_flag)
    49. led_out <= ~led_out ;
    50. else
    51. led_out <= led_out ;
    52. 2'b10: if(led_out[1] != led_out[0])
    53. led_out <= 2'b11 ;
    54. else if(cnt_25ms_flag)
    55. led_out <= ~led_out ;
    56. else
    57. led_out <= led_out ;
    58. default: led_out <= led_out ;
    59. endcase
    60. end
    61. endmodule

    1. `define key_length 2
    2. `define MAX_CNT_10MS 500_000
    3. `define MAX_CNT_500MS 25_000_000
    4. `define MAX_CNT_250MS 12_500_000

    1. `include "para.v"
    2. module top(
    3. input wire sys_clk ,
    4. input wire sys_rst_n ,
    5. input wire [1:0] key_in ,
    6. output wire [1:0] led_out
    7. );
    8. // 例化间连�?
    9. wire [`key_length -1:0] key_flag ;
    10. key_filter key_filter_inst(
    11. .sys_clk ( sys_clk ) ,
    12. .sys_rst_n ( sys_rst_n ) ,
    13. .key_in ( key_in ) ,
    14. .key_flag ( key_flag )
    15. );
    16. led led_inst(
    17. .sys_clk ( sys_clk ) ,
    18. .sys_rst_n ( sys_rst_n ) ,
    19. .key_in ( key_flag ) ,
    20. .led_out ( led_out )
    21. );
    22. endmodule

    仿真:

  • 相关阅读:
    界面拖动界面拖动
    华为手机怎么开启生产模式 | 华为手机打开生产模式后有驱动没有安装好怎么办 | 华为荣耀9手机打开生产模式后有驱动有叹号怎么办
    怎么剔除掉六十岁(退休)以上的人(python自动化办公)
    Mybatis的二级缓存 (默认方式)
    vue面试题
    《洛谷深入浅出基础篇》——P3405 citis and state ——哈希表
    2023年安全生产监管人员证考试题库及安全生产监管人员试题解析
    01 _ 为什么要学习数据结构和算法?
    使用 Next.js 搭建 Monorepo 组件库文档
    [附源码]计算机毕业设计JAVA人力资源管理系统论文2022
  • 原文地址:https://blog.csdn.net/Meng_long2022/article/details/134259028