当前位置:   article > 正文

FPGA 03 LED跑马灯实验_fpgaledpaoma灯代码

fpgaledpaoma灯代码

目录

1. 位拼接实现

1.1 设计代码

1.2 testbench

1.3 仿真波形

​编辑2. 3-8译码器实现

2.1 设计代码

2.2 仿真波形

3. 参数化实现

3.1 设计代码

3.2 testbench

3.3 仿真波形

4. 8位LED以不同的频率闪烁——参数化实现

4.1 设计代码   

4.2 testbench

4.3 仿真波形


1. 位拼接实现

        8个led灯以每个0.5ms的速率循环闪烁。

1.1 设计代码

  1. module led_run1(
  2. clk,
  3. reset_n,
  4. led
  5. );
  6. input clk;
  7. input reset_n;
  8. output reg[7:0]led;
  9. reg [24:0]counter;
  10. always@(posedge clk or negedge reset_n)
  11. if(!reset_n)
  12. counter <= 0;
  13. else if(counter == 24999)
  14. counter <= 0;
  15. else
  16. counter <= counter+1;
  17. always@(posedge clk or negedge reset_n)
  18. if(!reset_n)
  19. led <= 8'b0000_0001;
  20. else if(counter == 24999)
  21. led <= {led[6:0],led[7]};
  22. else
  23. led <= led;
  24. endmodule

1.2 testbench

  1. `timescale 1ns / 1ns
  2. module led_run_tb;
  3. reg clk;
  4. reg reset_n;
  5. wire [7:0]led;
  6. led_run led_run(
  7. .clk(clk),
  8. .reset_n(reset_n),
  9. .led(led)
  10. );
  11. initial clk = 1;
  12. always #10 clk = ~clk;
  13. initial begin
  14. reset_n = 0;
  15. #201;
  16. reset_n = 1;
  17. #50000000;
  18. $stop;
  19. end
  20. endmodule

1.3 仿真波形

2. 3-8译码器实现

2.1 设计代码

  1. module led_run2(
  2. clk,
  3. reset_n,
  4. led
  5. );
  6. input clk;
  7. input reset_n;
  8. output [7:0]led;
  9. reg [24:0]counter;
  10. always@(posedge clk or negedge reset_n)
  11. if(!reset_n)
  12. counter <= 0;
  13. else if(counter == 24999)
  14. counter <= 0;
  15. else
  16. counter <= counter+1'd1;
  17. reg [2:0]cnt;
  18. always@(posedge clk or negedge reset_n)
  19. if(!reset_n)
  20. cnt <= 0;
  21. else if(counter == 24999)
  22. cnt <= cnt+1'd1;
  23. decoder_3_8 decoder_3_8(
  24. .a(cnt[2]),
  25. .b(cnt[1]),
  26. .c(cnt[0]),
  27. .out(led));
  28. endmodule

        其中3-8译码器的代码见FPGA 01 3-8译码器https://blog.csdn.net/SameenZhang/article/details/135256863

2.2 仿真波形

        testbench同1.2,仿真波形见下图

3. 参数化实现

        参数化意义:方便仿真、板级调试参数修改

3.1 设计代码

        将闪烁时间设置为参数“MCNT”,代码如下

  1. module led_run3(
  2. clk,
  3. reset_n,
  4. led
  5. );
  6. input clk;
  7. input reset_n;
  8. output reg[7:0]led;
  9. parameter MCNT = 25'd24999999;
  10. reg [24:0]counter;
  11. always@(posedge clk or negedge reset_n)
  12. if(!reset_n)
  13. counter <= 0;
  14. else if(counter == MCNT)
  15. counter <= 0;
  16. else
  17. counter <= counter+1;
  18. always@(posedge clk or negedge reset_n)
  19. if(!reset_n)
  20. led <= 8'b0000_0001;
  21. else if(counter == MCNT)
  22. led <= {led[6:0],led[7]};
  23. else
  24. led <= led;
  25. endmodule

3.2 testbench

        为了提高仿真速度,将闪烁时间改为500ms,即在tb文件中重新定义参数MCNT的值

  1. `timescale 1ns / 1ns
  2. module led_run_self_tb;
  3. reg clk;
  4. reg reset_n;
  5. wire [7:0]led;
  6. led_run3 led_run3_inst0(
  7. .clk(clk),
  8. .reset_n(reset_n),
  9. .led(led)
  10. );
  11. defparam led_run3_inst0.MCNT = 24999;
  12. initial clk = 1;
  13. always #10 clk = ~clk;
  14. initial begin
  15. reset_n = 0;
  16. #201;
  17. reset_n = 1;
  18. #50000000;
  19. $stop;
  20. end
  21. endmodule

3.3 仿真波形

4. 8位LED以不同的频率闪烁——参数化实现

        Q:8位LED灯分别以0.1s,0.2s,0.3s,0.4s,0.4s,0.3s,0.2s,0.1s的速率闪烁。

4.1 设计代码   

1.控制闪烁的文件(同led——flash)    

  1. module led_run4(
  2. clk,
  3. reset_n,
  4. led
  5. );
  6. input clk;
  7. input reset_n;
  8. output reg[7:0]led;
  9. parameter MCNT = 25'd24999999;
  10. reg [24:0]counter;
  11. always@(posedge clk or negedge reset_n)
  12. if(!reset_n)
  13. counter <= 0;
  14. else if(counter == MCNT)
  15. counter <= 0;
  16. else
  17. counter <= counter+1;
  18. always@(posedge clk or negedge reset_n)
  19. if(!reset_n)
  20. led <= 0;
  21. else if(counter == MCNT)
  22. led <= !led;
  23. endmodule

2.例化module led_run4,设置不同的MCNT数值

  1. module led_run4_test(
  2. clk,
  3. reset_n,
  4. led
  5. );
  6. input clk;
  7. input reset_n;
  8. output [7:0]led;
  9. led_run4 led_run4_inst0(
  10. .clk(clk),
  11. .reset_n(reset_n),
  12. .led(led[0])
  13. );
  14. defparam led_run4_inst0.MCNT = 2499999;
  15. led_run4 led_run4_inst1(
  16. .clk(clk),
  17. .reset_n(reset_n),
  18. .led(led[1])
  19. );
  20. defparam led_run4_inst1.MCNT = 4999999;
  21. led_run4 led_run4_inst2(
  22. .clk(clk),
  23. .reset_n(reset_n),
  24. .led(led[2])
  25. );
  26. defparam led_run4_inst2.MCNT = 7499999;
  27. led_run4 led_run4_inst3(
  28. .clk(clk),
  29. .reset_n(reset_n),
  30. .led(led[3])
  31. );
  32. defparam led_run4_inst3.MCNT = 9999999;
  33. led_run4 led_run4_inst4(
  34. .clk(clk),
  35. .reset_n(reset_n),
  36. .led(led[4])
  37. );
  38. defparam led_run4_inst4.MCNT = 9999999;
  39. led_run4 led_run4_inst5(
  40. .clk(clk),
  41. .reset_n(reset_n),
  42. .led(led[5])
  43. );
  44. defparam led_run4_inst5.MCNT = 7499999;
  45. led_run4 led_run4_inst6(
  46. .clk(clk),
  47. .reset_n(reset_n),
  48. .led(led[6])
  49. );
  50. defparam led_run4_inst6.MCNT = 4999999;
  51. led_run4 led_run4_inst7(
  52. .clk(clk),
  53. .reset_n(reset_n),
  54. .led(led[7])
  55. );
  56. defparam led_run4_inst7.MCNT = 2499999;
  57. endmodule

4.2 testbench

对module led_run4_test文件进行仿真

  1. `timescale 1ns / 1ps
  2. module led_run4_test_tb;
  3. reg clk;
  4. reg reset_n;
  5. wire [7:0]led;
  6. led_run4_test led_run4_test(
  7. .clk(clk),
  8. .reset_n(reset_n),
  9. .led(led)
  10. );
  11. initial clk = 1;
  12. always #10 clk = ~clk;
  13. initial begin
  14. reset_n = 0;
  15. #201;
  16. reset_n = 1;
  17. #500000000;
  18. $stop;
  19. end
  20. endmodule

4.3 仿真波形

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

闽ICP备14008679号