找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1197|回复: 0
打印 上一主题 下一主题
收起左侧

交通灯的FPGA程序

[复制链接]
跳转到指定楼层
楼主
ID:530516 发表于 2019-5-9 18:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
以前做的,以前调试出来了,现在不知道合适没有
摘要:
可编程逻辑器件,不仅速度快、集成度高,并且能随心所欲地完成用户定义的逻辑功能,还可以加密和重新编程,其编程次数最大可达1万次以上。使用可编程逻辑器件可以大大简化硬件系统、降低成本、提高系统的可靠性、灵活性和保密性交通灯控制电路自动控制十字路口两组红、黄、绿交通灯的状态转换,指挥各种车辆和行人安全通行,实现十字路口交通管理的自动化。交通信号灯的出现,使交通得以有效管制,对于疏导交通流量、提高道路通行能力、减少交通事故有明显效果。本设计主要以74190芯片为核心实现交通灯控制。这个电路采用两个74190芯片连成一个从25到00的计数器,用JK触发器实现信号灯的转换,倒计时显示采用七段数码管作为显示,非常简单地实现了交通信号灯的控制。使用电脑仿真技术对系统进行仿真。

目录
1.EDA技术概述17
2.设计思路17
2.1设计思路18
2.2所实现功能说明19
3.各模块的编程实现20
3.1  时基脉冲发生模块20
3.2  计数及红绿灯控制模块22
3.3  译码模块23
3.4  主程序25
3.5  仿真时序图29
4.心得体会30
5.参考文献30

1.EDA技术概述
FPGA(现场可编程门阵列)和CPLD(复杂可编程逻辑器件)都是可编程逻辑器件,他们是在PAL,GAL等逻辑器件的基础上发展起来的。同以往的PAL,GAL相比较,FPGA/CPLD的规模比较大,它可以替代几十甚至几千块通用IC芯片。这样的FPGA/CPLD实际上就是一个子系统部件。这种芯片受到世界范围内电子工程设计人员的广泛关注和普遍欢迎。比较典型的就是Altera公司和Xilinx公司的CPLD器件系列和FPGA器件系列,他们开发较早,占用了较大的PLD市场。
对用户而言,虽然FPGA/CPLD的内部结构稍有不同,但其用法都一样,所以大多数情况下,不加以区分。FPGA/CPLD芯片都是特殊的ASIC芯片,除了具有ASIC的特点外,还具有以下几个优点:
(1) 随着VLSI(Very LargeScale IC,超大集成电路)工艺的不断提高单一芯片内部可以容纳上百万个晶体管,FPGA/CPLD芯片的规模也越来越大,其单片逻辑门数已达到上百万门,它所实现的功能也越来越强,同时也可以实现系统集成,即片上系统SOC.
(2) FPGA/CPLD芯片在出厂之前都做过百分之百的测试,不需要设计人员承担芯片风险和费用,设计人员只需在自己的实验室就可以通过相关的软硬件环境来完成芯片的最终功能设计。所以,FPGA/CPLD的资金投入就小,减少了潜在的花费。
(3) 用户可以反复地编程、擦除、使用或者在外围电路不动的情况下用不同软件就可实现不同的功能。FPGA/CPLD软件包中有各种输入工具和仿真工具,及版图设计工具和编程器等全线产品,电路设计人员在很短的时间内就可完成电路的输入、编译、优化。仿真,直至最后芯片的制作。当电路有少量的改动,更能显示FPGA/CPLD的优势。电路设计人员在使用FPGA/CPLD进行电路设计时,不需要具有专门的IC(集成电路)深层次的知识,FPGA/CPLD软件易学易用,能使设计人员更能集中精力进行电路设计,快速将产品推向市场。
(4) 在线可编程技术(ISP)使得使用FPGA/CPLD的产品可以做到远程升级。
2.设计思路
2.1设计思路
所设计的交通信号灯控制电路,主要适用于在两条干道汇合点形成的十字交叉路口,路口设计两组红绿灯分别对两个方向上的交通运行状态进行管理。
东西方向和南北方向各使用3个LED显示,红黄绿各代表红黄绿灯,绿灯亮同时是人行灯。
东西方向和南北方向计时均为2位数,共需要4个LED七段数码管显示。Time信号输出显示的内容。采用EPF10K10LC84-4 实现
2.2 所实现功能说明
南北和东西方向各有一组绿,人行,红,黄灯,各自的持续时间分别为20s,20s,25s,5s;用两组数码管,以倒计时方式显示两个方向允许通行或禁止通行的剩余时间;绿灯也可看做人行灯;
3.各模块的编程实现
3.1时基脉冲发生模块
在红绿灯交通信号系统中,大多数的情况是通过自动控制的方式指挥交通的。因此,为了避免意外事件的发生,电路必须给出一个稳定的时钟才能让系统正常的工作。因此时钟发生模块最主要的功能就是产生一些稳定的输出信号,并将其用做后面几个电路的使能控制与同步信号。通过编程实现1Hz脉冲的产生,程序如下所示。
  1. Library IEEE ;      
  2. use IEEE.std_logic_1164.all ;
  3. ENTITY CLKGEN IS
  4.      PORT(    CLK1:IN STD_LOGIC;
  5.                CLK:OUT STD_LOGIC);
  6. END CLKGEN;
  7. ARCHITECTURE behav OF CLKGEN IS
  8.   SIGNAL CNTER:INTEGER RANGE 0 TO 25000000;
  9. BEGIN
  10.    PROCESS(CLK)            BEGIN
  11.      IF CLK'EVENT AND CLK='1' THEN
  12.       IF CNTER=25000000 THEN CNTER<=0;
  13.        ELSE CNTER<=CNTER+1;  
  14.       END IF;
  15.      END IF;
  16.    END PROCESS;
  17.    PROCESS(CNTER)      
  18.     BEGIN
  19.      IF CNTER=12500000 THEN CLK<='1';
  20.        END IF;
  21.      IF CNTER=25000000 THEN CLK<='0';
  22.        END IF;
  23.    END PROCESS;
  24. END behav;
复制代码
3.2计数及红绿灯控制模块

南北向和东西向各有一组红黄绿灯,各自持续的时间为分别为25s、5s、20s。通过编程可以实现控制各通道的计数控制且输出为二进制数,并且可以控制各灯的亮灭情况,高电平为亮,低电平为灭。

东西向模块程序为:
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.std_logic_unsigned.all;
  4. entity daolu1 is
  5.         port(
  6.         clk:in std_logic;               
  7. timel:out std_logic_vector(4 downto 0);        
  8. r,g,t:out std_logic                );
  9. end daolu1;
  10. architecture daodu of daolu1 is
  11. type color is(green,yellow,red);
  12. begin
  13.         process(clk)
  14.         variable reset:std_logic:='0';
  15.         variable tempr,tempg,tempt:std_logic;
  16.         variable temp_l:std_logic_vector(4 downto 0);
  17.     variable temp_color:color:=green;
  18.         begin
  19.                 if(clk'event and clk='1')then        
  20.                         case temp_color is
  21.                         when yellow=>               
  22.                                         tempr:='1';
  23.                                         tempg:='0';
  24.                     tempt:='0';
  25.                                         case reset is               
  26.                                         when '0' =>               
  27.                                                 temp_l:="00100";
  28.                                                 reset:='1';
  29.                                         when others =>        
  30.                                                 case temp_l is
  31.                                                                 when "00000"=>
  32.                                                                 temp_l:="11000";
  33.                                                                 temp_color:=red;
  34.                                                         when others=>
  35.                                                                 temp_l:=temp_l-1;
  36.                                                          end case;
  37.                                                 end case;
  38.                                         when green=>        
  39.                                                 tempr:='0';
  40.                                                 tempg:='0';
  41.                         tempt:='1';
  42.                                                 case reset is               
  43.                                                 when '0' =>               
  44.                                                 temp_l:="10011";
  45.                                                         reset:='1';
  46.                                                 when others =>        
  47.                                                 case temp_l is                                       
  48.                         when "00000"=>
  49.                                                         temp_l:="00100";
  50.                                                         temp_color:=yellow;
  51.                                                 when others=>
  52.                                                         temp_l:=temp_l-1;
  53.                                                  end case;
  54.                                         end case;
  55.                                 when red=>                        
  56.                                         tempr:='0';
  57.                                         tempg:='1';
  58.                        tempt:='0';                        
  59.                                         case reset is               
  60.                                         when '0' =>        
  61.                                                 temp_l:="11000";
  62.                                                 reset:='1';
  63.                                         when others =>        
  64.                                                 case temp_l is
  65.                                                                 when "00000"=>
  66.                                                                 temp_l:="10011";
  67.                                                                 temp_color:=green;
  68.                                                         when others=>
  69.                                                                 temp_l:=temp_l-1;
  70.                                                          end case;
  71.                                                 end case;
  72.                  end case;        
  73.                                 
  74.         end if;
  75.         timel<=temp_l;
  76.         r<=tempr;
  77.         g<=tempg;
  78.     t<=tempt;
  79. end process;
  80. end;

  81. 南北向模块程序为:
  82. library ieee;
  83. use ieee.std_logic_1164.all;
  84. use ieee.std_logic_unsigned.all;
  85. entity daolu2 is
  86.         port(
  87.                 clk:in std_logic;               
  88.                         time2:out std_logic_vector(4 downto 0);        
  89.                 r1,g1,t1:out std_logic               
  90.                 );
  91. end daolu2;
  92. architecture daolu of daolu2 is
  93. type color is(red,green,yellow);
  94. begin
  95.         process(clk)
  96.         variable reset:std_logic:='0';
  97.         variable tempr,tempg,tempt:std_logic;
  98.         variable temp_l:std_logic_vector(4 downto 0);
  99.     variable temp_color:color:=red;
  100.         begin
  101.                 if(clk'event and clk='1')then
  102.                         
  103.                         case temp_color is
  104.                         when yellow=>               
  105.                                         tempr:='1';
  106.                                         tempg:='0';
  107.                     tempt:='0';
  108.                                         case reset is               
  109.                                         when '0' =>               
  110.                                                 temp_l:="00100";
  111.                                                 reset:='1';
  112.                                         when others =>        
  113.                                                 case temp_l is
  114.                                                                 when "00000"=>
  115.                                                                 temp_l:="11000";
  116.                                                                 temp_color:=red;
  117.                                                         when others=>
  118.                                                                 temp_l:=temp_l-1;
  119.                                                          end case;
  120.                                                 end case;
  121.                                         when green=>        
  122.                                                 tempr:='0';
  123.                                                 tempg:='0';
  124.                         tempt:='1';
  125.                                                 case reset is               
  126.                                                 when '0' =>               
  127.                                                         temp_l:="10011";
  128.                                                         reset:='1';
  129.                                                 when others =>        
  130.                                                 case temp_l is
  131.                                                                 when "00000"=>
  132.                                                                 temp_l:="00100";
  133.                                                                 temp_color:=yellow;
  134.                                                         when others=>
  135.                                                                 temp_l:=temp_l-1;
  136.                                                          end case;
  137.                                                 end case;
  138.                                         when red=>                        
  139.                                                 tempr:='0';
  140.                                                 tempg:='1';                                       
  141.                                                 tempt:='0';
  142.                         case reset is               
  143.                                                 when '0' =>        
  144.                                                         temp_l:="11000";
  145.                                                         reset:='1';
  146.                                                 when others =>        
  147.                                                 case temp_l is
  148.                                                                 when "00000"=>
  149.                                                                 temp_l:="10011";
  150.                                                                 temp_color:=green;
  151.                                                         when others=>
  152.                                                                 temp_l:=temp_l-1;
  153.                                                          end case;
  154.                                                 end case;
  155.                        end case;        
  156.                                         end if;
  157.                 time2<=temp_l;
  158.         r1<=tempr;
  159.         g1<=tempg;
  160.      t1<=tempt;
  161. end process;
  162. end;
复制代码

3.3 译码模块

计数控制电路输出为二进制码,要想用数码管显示计数需要把二进制码变为七段码。通过编程先把二进制码转换为BCD码再把BCD码转换为七段码实现程序如下:

将二进制码转化为BCD码
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. entity yima is
  4. port (time : in std_logic_vector(4 downto 0);
  5.       tim  : out std_logic_vector(7 downto 0));
  6. end yima;
  7. architecture one of yima is
  8. begin
  9. process(time)
  10. begin
  11. case time is
  12. when "00000" => tim <= "00000000";
  13. when "00001" => tim <= "00000001";
  14. when "00010" => tim <= "00000010";
  15. when "00011" => tim <= "00000011";
  16. when "00100" => tim <= "00000100";
  17. when "00101" => tim <= "00000101";
  18. when "00110" => tim <= "00000110";
  19. when "00111" => tim <= "00000111";
  20. when "01000" => tim <= "00001000";
  21. when "01001" => tim <= "00001001";
  22. when "01010" => tim <= "00010000";
  23. when "01011" => tim <= "00010001";
  24. when "01100" => tim <= "00010010";
  25. when "01101" => tim <= "00010011";
  26. when "01110" => tim <= "00010100";
  27. when "01111" => tim <= "00010101";
  28. when "10000" => tim <= "00010110";
  29. when "10001" => tim <= "00010111";
  30. when "10010" => tim <= "00011000";
  31. when "10011" => tim <= "00011001";
  32. when "10100" => tim <= "00100000";
  33. when "10101" => tim <= "00100001";
  34. when "10110" => tim <= "00100010";
  35. when "10111" => tim <= "00100011";
  36. when "11000" => tim <= "00100100";
  37. when others => NULL;
  38. end case;
  39. end process;
  40. end;

  41. 将BCD码转化为七段码
  42. library ieee;
  43. use ieee.std_logic_1164.all;
  44. use ieee.std_logic_unsigned.all;
  45. entity yima1 is
  46. port (jin : in std_logic_vector(3 downto 0);
  47.       chu  : out std_logic_vector(6 downto 0));
  48. end yima1;
  49. architecture art of yima1 is
  50. begin
  51. process (jin) is
  52. begin
  53. case jin is
  54. when "0000"=>chu<="0111111";
  55. when "0001"=>chu<="0000110";
  56. when "0010"=>chu<="1011011";
  57. when "0011"=>chu<="1001111";
  58. when "0100"=>chu<="1100110";
  59. when "0101"=>chu<="1101101";
  60. when "0110"=>chu<="1111101";
  61. when "0111"=>chu<="0000111";
  62. when "1000"=>chu<="1111111";
  63. when "1001"=>chu<="1101111";
  64. when others=>chu<="0000000";
  65. end case;
  66. end process;
  67. end architecture art;
  68. 3.4 主程序
  69. library ieee;
  70. use ieee.std_logic_1164.all;
  71. use ieee.std_logic_unsigned.all;
  72. entity jtd is
  73.         port(clk:in std_logic;        
  74.     r,g,t,r1,g1,t1:out std_logic;
  75. dout1,dout2,dout3,dout4:out std_logic_vector(6 downto 0));        
  76. end entity jtd;
  77. architecture art of jtd is
  78. component daolu1 is
  79. port(
  80.         clk:in std_logic;               
  81.         timel:out std_logic_vector(4 downto 0);        
  82.     r,g,t:out std_logic);
  83. end component daolu1;
  84. component daolu2 is
  85. port(
  86.         clk:in std_logic;               
  87.         time2:out std_logic_vector(4 downto 0);        
  88.     r1,g1,t1:out std_logic);
  89. end component daolu2;
  90. component yima is
  91. port (time : in std_logic_vector(4 downto 0);
  92.       tim  : out std_logic_vector(7 downto 0));
  93. end component yima;
  94. component yima1 is
  95. port (shr : in std_logic_vector(4 downto 0);
  96.       chu  : out std_logic_vector(7 downto 0));
  97. end component yima1;
  98. signal ad1,ad2:std_logic_vector(4 downto 0);
  99. signal ac1,ac2:std_logic_vector(7 downto 0);
  100. begin
  101. u1:daolu1 port map(clk=>clk,g=>g,t=>t,r=>r,timel=>ad1);
  102. u2:daolu2 port map(clk=>clk,g1=>g1,t1=>t1,r1=>r1,time2=>ad2);
  103. u3:yima  port map(time=>ad1,tim=>ac1);
  104. u4:yima  port map(time=>ad2,tim=>ac2);
  105. u5:yima1 port map(shr=>ac1(3 downto 0),chu=>dout1);
  106. u6:yima1 port map(shr=>ac1(7 downto 4),chu=>dout2);
  107. u7:yima1 port map(shr=>ac2(3 downto 0),chu=>dout3);
  108. u8:yima1 port map(shr=>ac2(7 downto 4),chu=>dout4);
  109. end architecture art;
复制代码
3.5 仿真时序图

4. 心得体会
在拿到题目后,首先进行了单元模块的设计,将每一个单元模块设计完成后再经行仿真,仿真成功后就可以进行顶层文件的编写了,在顶层文件的编写过程中遇到了一些问题,特别是各模块之间的连接,以及信号的定义,总是有错误。有的时候信号的定义容易出现混淆,在反复的修改过后,顶层文件终于能够编译成功了。
在波形仿真的过程中,同样遇到了困难,有的时候,由于END TIME的时间修改的太大,会出现仿真时间过长的问题,这个时候应该要把END TIME的时间相应的改小,或是修改系统时钟的频率。
在设计的过程中还应该多联系下实际情况,要了解实际情况下交通信号灯的工作情况,才能更好的完成此次的课程设计。在今后的工作和学习中,我们不能仅仅把目光停留在课本上,要多理论联系实际。有的时候,理论上是正确的东西放到现实中去,可能由于种种因素的制约,并不能达到实际的效果,还需要我们进行相应的修改才能完成要求。这次的课程设计使我巩固了以前学习到的知识,还使我掌握了以前没有掌握的知识,同时锻炼了自己的能力。





评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表