当前位置:   article > 正文

Verilog设计交通信号灯_verilog交通灯

verilog交通灯

目录

一、设计要求

二、模块总和

三、模块设计

1.顶层模块

2.分频模块

3.计数模块

4.状态机模块

5.倒计时模块

6.数码显示模块

7.约束代码

四、引脚分配

五、板上测试

总结

一、设计要求

1.利用 NEXYS4 DDR 开发板设计一款交通灯控制系统,能够显示红、黄、绿灯;
2.交通灯控制系统具有秒表倒计时功能;

3.我通过修改led六个分别表示主干道红绿黄和支干道红绿黄

4.信号灯设计时间

主干道绿灯,支干道红灯 30s
主干道红灯,支干道黄灯 5s
主干道红灯,支干道绿灯 30s
主干道黄灯,支干道红灯 5s

二、模块总和

三、模块设计

1.顶层模块

  1. module top(
  2. input clk,
  3. input rst_n,
  4. output [7:0]sel,
  5. output [7:0]seg,
  6. output [5:0]led
  7. );
  8. wire clk_1s;
  9. wire [6:0]cnt;
  10. wire [3:0]en1,en2;
  11. div div(
  12. .clk(clk),
  13. .rst_n(rst_n),
  14. .clk_1s(clk_1s)
  15. );
  16. counter counter(
  17. .clk(clk_1s),
  18. .rst_n(rst_n),
  19. .cnt(cnt)
  20. );
  21. vlg_traffic a1(
  22. .clk(clk),
  23. .rst_n(rst_n),
  24. .cnt(cnt),
  25. .led(led)
  26. );
  27. countdown countdown(
  28. .clk(clk),
  29. .rst_n(rst_n),
  30. .cnt(cnt),
  31. .en1(en1),
  32. .en2(en2)
  33. );
  34. digital digital(
  35. .clk(clk),
  36. .rst_n(rst_n),
  37. .en1(en1),
  38. .en2(en2),
  39. .sel(sel),
  40. .seg(seg)
  41. );
  42. endmodule

2.分频模块

  1. module div(
  2. input clk,
  3. input rst_n,
  4. output reg clk_1s//1s
  5. );
  6. reg[29:0]count1;//计数1s
  7. //1s计数
  8. always @(posedge clk or negedge rst_n)
  9. if(!rst_n)
  10. count1 <= 1'b0;
  11. else if(count1 == 49999999)//计数器1完成0.5s计数
  12. count1 <= 1'b0;
  13. else
  14. count1 <= count1 + 1'b1;
  15. //1s分频
  16. always @(posedge clk or negedge rst_n)
  17. if(!rst_n)
  18. clk_1s <= 0;
  19. else if(count1 == 49999999)
  20. clk_1s <= ~clk_1s;//时钟100Mhz 完成1s分频
  21. else
  22. clk_1s <= clk_1s;
  23. endmodule

3.计数模块

  1. module counter(
  2. input clk,
  3. input rst_n,
  4. output reg [6:0]cnt//实现70s红绿黄灯
  5. );
  6. always @(posedge clk or negedge rst_n)
  7. if(!rst_n)
  8. cnt <= 0;
  9. else if(cnt == 7'd70)
  10. cnt <= 0;
  11. else
  12. cnt <= cnt + 1;
  13. endmodule

4.状态机模块

  1. module vlg_traffic(
  2. input clk,
  3. input rst_n,
  4. input [6:0]cnt,
  5. output reg [5:0]led//led[5]主干道黄灯 led[4]主干道红灯 led[3]主干道绿灯
  6. //led[2]支干道黄灯 led[1]支干道红灯 led[0]支干道绿灯
  7. );
  8. reg [3:0]state;
  9. parameter s0 = 4'b00001;//s0:主干道绿灯,支干道红灯 30s 灯6'b001_010
  10. parameter s1 = 4'b00010;//s1:主干道黄灯,支干道红灯 5s 灯6'b100_010
  11. parameter s2 = 4'b00100;//s2:主干道红灯,支干道黄灯 5s 灯6'b010_100
  12. parameter s3 = 4'b01000;//s3:主干道红灯,支干道绿灯 30s 灯6'b010_001
  13. always @(posedge clk or negedge rst_n)
  14. if(!rst_n)
  15. state <= s0;
  16. else case(state)
  17. s0:begin
  18. if(cnt <= 7'd29)
  19. begin
  20. led <= 6'b001_010;
  21. state <= s0;
  22. end
  23. else
  24. state <= s1;
  25. end
  26. s1:begin
  27. if((cnt > 7'd29)&&(cnt <= 7'd34))//用于板上显示从5s倒计时
  28. begin
  29. led <= 6'b100_010;
  30. state <= s1;
  31. end
  32. else
  33. state <= s2;
  34. end
  35. s2:begin
  36. if((cnt > 7'd34)&&(cnt <= 7'd39))
  37. begin
  38. led <= 6'b010_100;
  39. state <= s2;
  40. end
  41. else
  42. state <= s3;
  43. end
  44. s3:begin
  45. if((cnt > 7'd39)&&(cnt <= 7'd69))
  46. begin
  47. led <= 6'b010_001;
  48. state <= s3;
  49. end
  50. else
  51. state <= s0;
  52. end
  53. default:state <= s0;
  54. endcase
  55. endmodule

5.倒计时模块

  1. module countdown(
  2. input clk,
  3. input rst_n,
  4. input [6:0]cnt,
  5. output reg [3:0]en1,// 个位 最多为9 位宽为4
  6. output reg [3:0]en2 // 十位 最多为9 位宽为4
  7. );
  8. always @(posedge clk or negedge rst_n)
  9. if(!rst_n)
  10. begin
  11. en1 <= 0;
  12. en2 <= 0;
  13. end
  14. else if(cnt <= 7'd29)//用于板上从29s倒计时
  15. begin
  16. en1 <= (29-cnt)%10;
  17. en2 <= ((29-cnt)-en1)/10;
  18. end
  19. else if((cnt > 7'd29)&&(cnt <= 7'd34))//板上从4s倒计时
  20. begin
  21. en1 <= (34-cnt);
  22. en2 <= 0;
  23. end
  24. else if((cnt > 7'd34)&&(cnt <= 7'd39))//板上从4s倒计时
  25. begin
  26. en1 <= (39-cnt);
  27. en2 <= 0;
  28. end
  29. else if((cnt > 7'd39)&&(cnt <= 7'd69))//板上从29s倒计时
  30. begin
  31. en1 <= (69-cnt)%10;
  32. en2 <= ((69-cnt)-en1)/10;
  33. end
  34. endmodule

6.数码显示模块

  1. module digital(
  2. input clk,
  3. input rst_n,
  4. input [3:0]en1,
  5. input [3:0]en2,
  6. output reg [7:0]sel,//片选信号
  7. output reg [7:0]seg //段选信号
  8. );
  9. //1ms计数器
  10. reg [16:0]cnt1;
  11. always @(posedge clk or negedge rst_n)
  12. if(!rst_n)
  13. cnt1 <= 0;
  14. else if(cnt1 == 99999)//1000000/10=100000
  15. cnt1 <= 0;
  16. else
  17. cnt1 <= cnt1 + 1;
  18. //1ms使能时钟控制信号
  19. reg clk_1ms;
  20. always @(posedge clk or negedge rst_n)
  21. if(!rst_n)
  22. clk_1ms <= 0;
  23. else if(cnt1 == 99999)
  24. clk_1ms <= 1;
  25. else
  26. clk_1ms <= 0;
  27. //位选计数器
  28. reg [2:0]num_cnt;
  29. always @(posedge clk or negedge rst_n)
  30. if(!rst_n)
  31. num_cnt <= 0;
  32. else if(clk_1ms)
  33. num_cnt <= num_cnt + 1;
  34. //板上位选信号 共阳极 低电平有效
  35. always @(posedge clk)
  36. case(num_cnt)
  37. 0:sel <=8'b1111_1110;//个位
  38. 1:sel <=8'b1111_1101;//十位
  39. 2:sel <=8'b1111_1011;
  40. 3:sel <=8'b1111_0111;
  41. 4:sel <=8'b1110_1111;
  42. 5:sel <=8'b1101_1111;
  43. 6:sel <=8'b1011_1111;
  44. 7:sel <=8'b0111_1111;
  45. endcase
  46. //数据分配
  47. wire [31:0]data;
  48. assign data[3:0] = en1;
  49. assign data[7:4] = en2;
  50. assign data[11:8] = 0;
  51. assign data[15:12] = 0;
  52. assign data[19:16] = 0;
  53. assign data[23:20] = 0;
  54. assign data[27:24] = 0;
  55. assign data[31:28] = 0;
  56. reg [3:0]disp_tmp;
  57. always @(posedge clk)
  58. case(num_cnt)
  59. 0:disp_tmp <= data[3:0];
  60. 1:disp_tmp <= data[7:4];
  61. 2:disp_tmp <= data[11:8];
  62. 3:disp_tmp <= data[15:12];
  63. 4:disp_tmp <= data[19:16];
  64. 5:disp_tmp <= data[23:20];
  65. 6:disp_tmp <= data[27:24];
  66. 7:disp_tmp <= data[31:28];
  67. endcase
  68. //板上段选信号 共阳极 低电平有效
  69. always @(posedge clk)
  70. case(disp_tmp)
  71. 4'd0:seg <= 8'b1100_0000;
  72. 4'd1:seg <= 8'b1111_1001;
  73. 4'd2:seg <= 8'b1010_0100;
  74. 4'd3:seg <= 8'b1011_0000;
  75. 4'd4:seg <= 8'b1001_1001;
  76. 4'd5:seg <= 8'b1001_0010;
  77. 4'd6:seg <= 8'b1000_0010;
  78. 4'd7:seg <= 8'b1111_1000;
  79. 4'd8:seg <= 8'b1000_0000;
  80. 4'd9:seg <= 8'b1001_0000;
  81. endcase
  82. endmodule

7.约束代码

  1. set_property IOSTANDARD LVCMOS33 [get_ports {led[5]}]
  2. set_property IOSTANDARD LVCMOS33 [get_ports {led[4]}]
  3. set_property IOSTANDARD LVCMOS33 [get_ports {led[3]}]
  4. set_property IOSTANDARD LVCMOS33 [get_ports {led[2]}]
  5. set_property IOSTANDARD LVCMOS33 [get_ports {led[1]}]
  6. set_property IOSTANDARD LVCMOS33 [get_ports {led[0]}]
  7. set_property IOSTANDARD LVCMOS33 [get_ports {seg[7]}]
  8. set_property IOSTANDARD LVCMOS33 [get_ports {seg[6]}]
  9. set_property IOSTANDARD LVCMOS33 [get_ports {seg[5]}]
  10. set_property IOSTANDARD LVCMOS33 [get_ports {seg[4]}]
  11. set_property IOSTANDARD LVCMOS33 [get_ports {seg[3]}]
  12. set_property IOSTANDARD LVCMOS33 [get_ports {seg[2]}]
  13. set_property IOSTANDARD LVCMOS33 [get_ports {seg[1]}]
  14. set_property IOSTANDARD LVCMOS33 [get_ports {seg[0]}]
  15. set_property IOSTANDARD LVCMOS33 [get_ports {sel[7]}]
  16. set_property IOSTANDARD LVCMOS33 [get_ports {sel[6]}]
  17. set_property IOSTANDARD LVCMOS33 [get_ports {sel[5]}]
  18. set_property IOSTANDARD LVCMOS33 [get_ports {sel[4]}]
  19. set_property IOSTANDARD LVCMOS33 [get_ports {sel[3]}]
  20. set_property IOSTANDARD LVCMOS33 [get_ports {sel[2]}]
  21. set_property IOSTANDARD LVCMOS33 [get_ports {sel[1]}]
  22. set_property IOSTANDARD LVCMOS33 [get_ports {sel[0]}]
  23. set_property IOSTANDARD LVCMOS33 [get_ports clk]
  24. set_property IOSTANDARD LVCMOS33 [get_ports rst_n]
  25. set_property PACKAGE_PIN E3 [get_ports clk]
  26. set_property PACKAGE_PIN C12 [get_ports rst_n]
  27. set_property PACKAGE_PIN U13 [get_ports {sel[7]}]
  28. set_property PACKAGE_PIN K2 [get_ports {sel[6]}]
  29. set_property PACKAGE_PIN T14 [get_ports {sel[5]}]
  30. set_property PACKAGE_PIN P14 [get_ports {sel[4]}]
  31. set_property PACKAGE_PIN J14 [get_ports {sel[3]}]
  32. set_property PACKAGE_PIN T9 [get_ports {sel[2]}]
  33. set_property PACKAGE_PIN J18 [get_ports {sel[1]}]
  34. set_property PACKAGE_PIN J17 [get_ports {sel[0]}]
  35. set_property PACKAGE_PIN T10 [get_ports {seg[0]}]
  36. set_property PACKAGE_PIN R10 [get_ports {seg[1]}]
  37. set_property PACKAGE_PIN K16 [get_ports {seg[2]}]
  38. set_property PACKAGE_PIN K13 [get_ports {seg[3]}]
  39. set_property PACKAGE_PIN P15 [get_ports {seg[4]}]
  40. set_property PACKAGE_PIN T11 [get_ports {seg[5]}]
  41. set_property PACKAGE_PIN L18 [get_ports {seg[6]}]
  42. set_property PACKAGE_PIN H15 [get_ports {seg[7]}]
  43. set_property PACKAGE_PIN H17 [get_ports {led[0]}]
  44. set_property PACKAGE_PIN K15 [get_ports {led[1]}]
  45. set_property PACKAGE_PIN J13 [get_ports {led[2]}]
  46. set_property PACKAGE_PIN N14 [get_ports {led[3]}]
  47. set_property PACKAGE_PIN R18 [get_ports {led[4]}]
  48. set_property PACKAGE_PIN V17 [get_ports {led[5]}]

四、引脚分配

五、板上测试

S0:

S1:

S2:

S3:

总结

进行顶层模块时钟分配的时候,千万不能分配错,不然容易导致时间不一致导致数码显示错误;除了计数70s模块使用1s分频时钟,其余模块都是使用的原时钟信号,数码显示模块内部进行使用了1ms时钟使能,利用视觉暂留,进行数码管的动态扫描。

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

闽ICP备14008679号