请尊重作者版权,转载请注明原地址: http://www.cnblogs.com/connorzx/p/3676746.html
数电实验交通灯VHDL版,程序写的扩展性很差,待以后有时间进行优化。
程序如下:
1 library IEEE; 2 use IEEE.STD_LOGIC_1164.ALL; 3 use IEEE.STD_LOGIC_ARITH.ALL; 4 use IEEE.STD_LOGIC_UNSIGNED.ALL; 5 entity main is 6 Port ( clk : in STD_LOGIC; 7 rst : in STD_LOGIC; 8 led : out STD_LOGIC_VECTOR (5 downto 0); 9 an : out STD_LOGIC_VECTOR (3 downto 0); 10 seg : out STD_LOGIC_VECTOR (6 downto 0)); 11 end main; 12 13 architecture Behavioral of main is 14 signal sclk: std_logic; 15 signal cnt0:integer:=0; 16 signal cnt1:integer:=0; 17 signal cnt2:integer:=0; 18 signal cnt3:integer:=0; 19 signal disp1:integer:=0; 20 signal disp2:integer:=3; 21 signal display:integer; 22 signal num1:integer; 23 signal num1_l:integer; 24 signal num1_h:integer; 25 signal num2:integer; 26 signal num2_l:integer; 27 signal num2_h:integer; 28 signal ledt: std_logic_vector (5 downto 0); 29 signal an_sel: integer; 30 begin 31 led(0)<=ledt(0); 32 led(1)<=ledt(1); 33 led(2)<=ledt(2); 34 led(3)<=ledt(3); 35 led(4)<=ledt(4); 36 led(5)<=ledt(5); 37 process(clk) 38 begin 39 if(clk'event and clk='1') then 40 if(cnt0=25000000)then 41 cnt0<=0; 42 sclk<=not sclk; 43 else 44 cnt0<=cnt0+1; 45 end if; 46 end if; 47 end process; 48 49 process(clk) 50 begin 51 if(clk'event and clk='1') then 52 if(cnt2=50000) then 53 cnt2<=0; 54 if(an_sel=3)then 55 an_sel<=0; 56 else 57 an_sel<=an_sel+1; 58 end if; 59 else 60 cnt2<=cnt2+1; 61 end if; 62 end if; 63 end process; 64 65 process(rst,sclk) 66 begin 67 if(rst='1') then 68 ledt(0)<='0'; 69 ledt(1)<='0'; 70 ledt(2)<='0'; 71 elsif(sclk'event and sclk='1') then 72 cnt1<=cnt1+1; 73 cnt3<=cnt3+1; 74 if(cnt1>=1 and cnt1<=16)then 75 ledt(0)<='1'; 76 ledt(1)<='0'; 77 ledt(2)<='0'; 78 elsif(cnt1>=17 and cnt1<=29)then 79 ledt(0)<='0'; 80 ledt(1)<='1'; 81 ledt(2)<='0'; 82 elsif(cnt1=30)then 83 ledt(0)<='0'; 84 ledt(1)<='0'; 85 ledt(2)<='1'; 86 elsif(cnt1=31)then 87 cnt1<=0; 88 end if; 89 if(cnt3>=1 and cnt3<=13)then 90 ledt(3)<='0'; 91 ledt(4)<='1'; 92 ledt(5)<='0'; 93 elsif(cnt3>=14 and cnt3<=16)then 94 ledt(3)<='0'; 95 ledt(4)<='0'; 96 ledt(5)<='1'; 97 elsif(cnt3=17)then 98 ledt(3)<='1'; 99 ledt(4)<='0'; 100 ledt(5)<='0'; 101 elsif(cnt3=31)then 102 cnt3<=0; 103 end if; 104 end if; 105 end process; 106 107 process(sclk,ledt,rst) 108 begin 109 if (rst='1')then 110 disp1<=0; 111 disp2<=3; 112 elsif(sclk'event and sclk='1') then 113 if(ledt(0)='1' or ledt(1)='1' or ledt(2)='1')then 114 if(ledt(0)='1' and disp1=15)then 115 disp1<=3; 116 elsif(ledt(1)='1' and disp1=15)then 117 disp1<=13; 118 elsif(ledt(2)='1' and disp1=15)then 119 disp1<=0; 120 else 121 disp1<=disp1 + 1; 122 end if; 123 end if; 124 if(ledt(3)='1' or ledt(4)='1' or ledt(5)='1')then 125 if(ledt(3)='1' and disp2=15)then 126 disp2<=3; 127 elsif(ledt(4)='1' and disp2=15)then 128 disp2<=13; 129 elsif(ledt(5)='1' and disp2=15)then 130 disp2<=0; 131 else 132 disp2<=disp2 + 1; 133 end if; 134 end if; 135 end if; 136 end process; 137 138 process(an_sel) 139 begin 140 num1<=15-disp1; 141 if(num1>=10)then 142 num1_h<=1; 143 num1_l<=num1-10; 144 else 145 num1_h<=0; 146 num1_l<=num1; 147 end if; 148 num2<=15-disp2; 149 if(num2>=10)then 150 num2_h<=1; 151 num2_l<=num2-10; 152 else 153 num2_h<=0; 154 num2_l<=num2; 155 end if; 156 case an_sel is 157 when 0=>an<="1110";display<=num1_l; 158 when 1=>an<="1101";display<=num1_h; 159 when 2=>an<="1011";display<=num2_l; 160 when 3=>an<="0111";display<=num2_h; 161 when others=>null; 162 end case; 163 case display is 164 when 0=>seg<=b"0000001"; 165 when 1=>seg<=b"1001111"; 166 when 2=>seg<=b"0010010"; 167 when 3=>seg<=b"0000110"; 168 when 4=>seg<=b"1001100"; 169 when 5=>seg<=b"0100100"; 170 when 6=>seg<=b"0100000"; 171 when 7=>seg<=b"0001111"; 172 when 8=>seg<=b"0000000"; 173 when 9=>seg<=b"0000100"; 174 when others=>null; 175 end case; 176 end process; 177 end Behavioral;
约束文件
NET "clk" LOC = "B8"; NET "led<0>" LOC = "M5"; NET "led<1>" LOC = "M11"; NET "led<2>" LOC = "P7"; NET "led<3>" LOC = "P6"; NET "led<4>" LOC = "N5"; NET "led<5>" LOC = "N4"; NET "an<0>" LOC="F12"; NET "an<1>" LOC="J12"; NET "an<2>" LOC="M13"; NET "an<3>" LOC="K14"; #NET "seg<7>" LOC="N13"; NET "seg<6>" LOC="L14"; NET "seg<5>" LOC="H12"; NET "seg<4>" LOC="N14"; NET "seg<3>" LOC="N11"; NET "seg<2>" LOC="P12"; NET "seg<1>" LOC="L13"; NET "seg<0>" LOC="M12"; NET "rst" LOC="P11";