标题:
基于FPGA的交通灯vhdl程序设计
[打印本页]
作者:
@lmq
时间:
2019-12-29 11:15
标题:
基于FPGA的交通灯vhdl程序设计
51hei.png
(39.99 KB, 下载次数: 74)
下载附件
2019-12-29 12:34 上传
单片机源程序如下:
-------------------------------------
-- Title:交通灯控制器 --
-- Data: 2019-12-11 --
-------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
--------------------------------------------------------------------
entity traffic_light is
port( Clk : in std_logic; --时钟输入 为50MHz
Rst : in std_logic; --复位输入
R1,R2 : out std_logic; --红灯输出
Y1,Y2 : out std_logic; --黄灯输出
G1,G2 : out std_logic; --绿灯输出
Display : out std_logic_vector(6 downto 0); --七段码管显示输出
SEG_SEL : buffer std_logic_vector(3 downto 0) --七段码管扫描驱动(四位数码管)
);
end traffic_light ;
--------------------------------------------------------------------
architecture behave of traffic_light is
signal Disp_Temp : integer range 0 to 15;
signal Disp_Decode : std_logic_vector(6 downto 0); --段选缓存
signal SEC1,SEC10 : integer range 0 to 9;
signal Direction : integer range 0 to 15;
signal Clk_Count1 : std_logic_vector(9 downto 0); --产生0.5Hz时钟的分频计数器
signal Clk1Hz : std_logic;
signal Dir_Flag : std_logic; --方向标志
signal C : integer range 0 to 50000:=0; --50MHz转1kHz
signal L : integer range 0 to 50000:=0; --50MHz转1kHz
signal Wei_Flag : std_logic_vector(1 downto 0); --当前扫描数码管位数
signal Wei : std_logic_vector(3 downto 0); --位选缓存
begin
process(Clk)
begin
if(Clk'event and Clk='1') then
C<=C+1;
if(C>=50000) then --50MHz转1kHz
C<=0;
if(Clk_Count1<1000) then
Clk_Count1<=Clk_Count1+"01";
else
Clk_Count1<="0000000001";
end if;
end if;
end if;
end process;
Clk1Hz<=Clk_Count1(9);
process(Clk1Hz,Rst)
begin
if(Rst='0') then
SEC1<=0;
SEC10<=2;
Dir_Flag<='0';
elsif(Clk1Hz'event and Clk1Hz='1') then
if(SEC1=0) then
SEC1<=9;
if(SEC10=0) then
SEC10<=1;
else
SEC10<=SEC10-1;
end if;
else
SEC1<=SEC1-1;
end if;
if(SEC1=0 and SEC10=0) then
Dir_Flag<=not Dir_Flag;
end if;
end if;
end process;
process(Clk1Hz,Rst)
begin
if(Rst='0') then
R1<='1';
G1<='0';
R2<='1';
G2<='0';
else --正常运行
if(SEC10>0 or SEC1>3) then
if(Dir_Flag='0') then --横向通行
R1<='0';
G1<='1';
R2<='1';
G2<='0';
else
R1<='1';
G1<='0';
R2<='0';
G2<='1';
end if;
else
if(Dir_Flag='0') then --横向通行
R1<='0';
G1<='0';
R2<='1';
G2<='0';
else
R1<='1';
G1<='0';
R2<='0';
G2<='0';
end if;
end if;
end if;
end process;
process(Clk1Hz)
begin
if(SEC10>0 or SEC1>3) then
Y1<='0';
Y2<='0';
elsif(Dir_Flag='0') then
Y1<=Clk1Hz;
Y2<='0';
else
Y1<='0';
Y2<=Clk1Hz;
end if;
end process;
process(Dir_Flag)
begin
if(Dir_Flag='0') then --横向
Direction<=10;
else --纵向
Direction<=11;
end if;
end process;
process(Wei_Flag)
begin
case (Wei_Flag+"01") is
when "00"=>
Wei<="1110";
Disp_Temp<=Direction;
when "01"=>
Wei<="1101";
Disp_Temp<=Direction;
when "10"=>
Wei<="1011";
Disp_Temp<=SEC10;
when "11"=>
Wei<="0111";
Disp_Temp<=SEC1;
end case;
end process;
process(Clk)
begin
if(Clk'event and Clk='1') then --扫描累加
L<=L+1; --50MHz转1kHz
if(L>=50000) then
L<=0;
SEG_SEL<=Wei;
Wei_Flag<=Wei_Flag+1; --准备下一数码管
Display<=Disp_Decode;
end if;
end if;
end process;
process(Disp_Temp) --显示转换
begin
case Disp_Temp is
when 0=>Disp_Decode<="0111111"; --'0'
when 1=>Disp_Decode<="0000110"; --'1'
when 2=>Disp_Decode<="1011011"; --'2'
when 3=>Disp_Decode<="1001111"; --'3'
when 4=>Disp_Decode<="1100110"; --'4'
when 5=>Disp_Decode<="1101101"; --'5'
when 6=>Disp_Decode<="1111101"; --'6'
when 7=>Disp_Decode<="0000111"; --'7'
when 8=>Disp_Decode<="1111111"; --'8'
when 9=>Disp_Decode<="1101111"; --'9'
when 10=>Disp_Decode<="1001000"; --'='
when 11=>Disp_Decode<="0110110"; --'||'
when others=>Disp_Decode<="0000000"; --全灭
end case;
end process;
end behave;
复制代码
所有资料51hei提供下载:
交通灯.pdf
(137.48 KB, 下载次数: 37)
2019-12-29 11:12 上传
点击文件名下载附件
外围电路
下载积分: 黑币 -5
traffic_light.zip
(1.56 KB, 下载次数: 42)
2019-12-29 11:14 上传
点击文件名下载附件
程序
下载积分: 黑币 -5
作者:
zxopenljx
时间:
2020-4-8 14:23
谢谢楼主分享
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1