本文為大家?guī)硭姆N不同的vhdl數(shù)碼管動(dòng)態(tài)掃描程序設(shè)計(jì)。
vhdl數(shù)碼管動(dòng)態(tài)掃描一:循環(huán)滾動(dòng)
實(shí)現(xiàn)的功能
循環(huán)滾動(dòng),始終點(diǎn)亮6個(gè)數(shù)碼管,左出右進(jìn)。狀態(tài)為:012345-123450-234501-345012-450123-501234-012345
實(shí)現(xiàn)代碼:
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL
PORT(CLK:IN STD_LOGIC;
SG:OUT STD_LOGIC_VECTOR(6 DOWNTO 0); BT:OUT STD_LOGIC_VECTOR(5 DOWNTO 0)); END LED2;
ARCHITECTURE ACE OF LED2 IS component div_2K
port(clk_in:instd_logic; clk_out:outstd_logic);
end component;
SIGNAL CNT6:INTEGER RANGE 0 TO 5;
SIGNAL A:INTEGER RANGE 0 TO 5;
SIGNAL COUNT:INTEGER RANGE 0 TO 4999 := 0;
SIGNAl FLAG:INTEGER RANGE 0 TO 6 := 0;
SIGNAl FLAG_A:INTEGER RANGE 0 TO 5;
SIGNAL clk_tmp:STD_LOGIC;
BEGIN
u1:div_2k port map(clk_in=>CLK,clk_out=>clk_tmp);
P1:process(CNT6)
BEGIN
CASE CNT6 IS --3線至6線譯碼器
WHEN 0 => BT <= “011111” A <= 0 --A為位碼
WHEN 1 => BT<= “101111” A <= 1
WHEN 2 => BT<= “110111” A <= 2
WHEN 3 => BT<= “111011” A <= 3
WHEN 4 => BT<= “111101” A <= 4
WHEN 5 => BT<= “111110” A <= 5
WHEN OTHERS => NULL;
END CASE
END PROCESS P1;
P2:process(clk_tmp)
BEGIN
IF clk_tmp‘EVENT AND clk_tmp= ’1‘ THEN --實(shí)現(xiàn)模6計(jì)數(shù)器 if CNT6= 5 then CNT6<= 0; else
CNT6<=CNT6 + 1; end if;
IF (FLAG = 6) THEN --設(shè)置標(biāo)志 FLAG <= 0;
END IF;
IF COUNT =4999 THEN
--相當(dāng)于另一個(gè)時(shí)鐘
COUNT <= 0; --計(jì)數(shù)周期為5000
FLAG <=FLAG+1; --當(dāng)記滿5000時(shí)左移動(dòng)一位 ELSE
COUNT <=COUNT+1; --不滿5000繼續(xù)計(jì)數(shù) END IF; END IF;
END PROCESS P2;
P3:process(A,F(xiàn)LAG,F(xiàn)LAG_A)
BEGIN
FLAG_A<=(( A + FLAG )mod 6) --使用求余運(yùn)算實(shí)現(xiàn)移位
CASE FLAG_A IS --實(shí)現(xiàn)數(shù)碼管的顯示功能
WHEN 0=> SG <= “1111110”;
WHEN 1=> SG <= “0110000”;
WHEN 2=> SG <= “1101101”;
WHEN 3=> SG <= “1111001”;
WHEN 4=> SG <=“0110011”;
WHEN 5=> SG <= “1011011”;
WHEN OTHERS =>NULL
END CASE
END PROCESS P3;
END ACE;
代碼分析:
為實(shí)現(xiàn)移位,關(guān)鍵改動(dòng)為新增一個(gè)計(jì)數(shù)器,技術(shù)周期遠(yuǎn)遠(yuǎn)大于掃描周期,這樣,在一個(gè)大的計(jì)數(shù)周期內(nèi),對(duì)于要顯示的6位數(shù)碼進(jìn)行動(dòng)態(tài)掃描(和實(shí)驗(yàn)任務(wù)1中相同),顯示出6種移位狀態(tài)中的一種;在下一個(gè)大的周期內(nèi),利用FLAG標(biāo)志,并使用求余運(yùn)算將顯示位的數(shù)碼移位,比如,大的計(jì)數(shù)周期為0時(shí),F(xiàn)LAG為0,顯示“012345”六位數(shù)碼,大的周期為1時(shí),F(xiàn)ALG為1,此時(shí)各位求余(即FLAG_A求余)對(duì)應(yīng)的數(shù)碼為123450,顯示的數(shù)碼也就為“123450”。以此類推,實(shí)現(xiàn)循環(huán)移位。