EDA通過VHDL代碼設計一個跑馬燈


      EDA實驗要求

只是參考,實驗報告請自行完成,電路圖,時序圖請自行操作

〇、Quartus軟件的安裝

https://blog.csdn.net/weixin_43862765/article/details/99305902

一、LED顯示

(1)按鍵K1作輸入,LED1作輸出;按下K1時,LED1亮,松開時滅。
(2)用文本輸入法,編寫VHDL文件,完成設計。

LIBRARY ieee;
USE ieee.std_logic_1164.all; 

LIBRARY work;

ENTITY demo011 IS 
	PORT
	(
		k1 :  IN  STD_LOGIC;
		led01 :  OUT  STD_LOGIC
	);
END demo011;

ARCHITECTURE bdf_type OF demo011 IS 



BEGIN 
led01 <= k1;



END bdf_type;

(3)用圖形輸入法,完成設計。

二、譯碼器設計

按鍵K3、K2、K1用作三位編碼地址輸入(A2、A1、A0),LED8---LED1用作8位譯碼輸出Y7--Y0;完成3—8譯碼器設計;按鍵按下時,輸入為邏輯‘0’,松開時,為邏輯‘1’,LED亮時,對應有效的譯碼輸出。完成設計。

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity decoder3to8 is
	port (
		A: in std_logic_vector (2 downto 0);
		Y: out bit_vector(7 downto 0)
	);
end decoder3to8;

architecture behave of decoder3to8 is
begin
	Y<="00000001" sll conv_integer(A);
end behave;

三、分頻器、計數器設計

SYS_CLK0用作時鍾源(40MHZ),分頻后,作1HZ方波信號。

思路:

SYS_CLK0用作時鍾源(40MHZ), 可以理解為每秒鍾按動40 000 000次按鈕。

因此要達到1秒閃一次的話,就是指定一個范圍讓其交替亮暗。

例如0 - 40 000 000讓 led1 輸出 0 ,開發板的led燈是低電平有效, 因此為0時會亮

40 000 000 - 80 000 000 輸出 1 , 就是不亮

當達到80 000 000時重置為0,形成了一個循環。

(1)驅動LED1閃爍。閃爍頻率為1HZ。

LIBRARY ieee;
USE ieee.std_logic_1164.all; 
use ieee.std_logic_unsigned.all;


ENTITY twinkle IS 
	PORT
	(
		clk :  IN  STD_LOGIC;
		led01 :  OUT  STD_LOGIC
	);
END twinkle;

ARCHITECTURE bdf_type OF twinkle IS 

signal count :integer range 0 to 80000001;


BEGIN 

	process(clk)
	
	begin
		--if num=40000000 then num:=0; 
		--end if;
		if (clk'event and clk='0') then
			if count=80000000 then 
				count <=0;
			else
				count <=count+1;
			end if;
			if count>40000000 then 
				led01 <='1';
			else
				led01 <='0';
			end if;
		end if;
		
	end process;
END bdf_type;

(2)跑馬燈顯示:LED1 - LED8循環顯示。

LIBRARY ieee;
USE ieee.std_logic_1164.all; 
use ieee.std_logic_unsigned.all;


ENTITY gall_led IS 
	PORT
	(
		clk :  IN  STD_LOGIC;
		led :  OUT  STD_LOGIC_vector(7 downto 0)
	);
END gall_led;

ARCHITECTURE bdf_type OF gall_led IS 

signal count :integer range 0 to 160000000;
signal tmp :STD_LOGIC_vector(7 downto 0);

BEGIN 

	process(clk)
	
	begin
		if (clk'event and clk='0') then 
			if count=160000000 then 
				count <=0;
			else
				count <=count+1;
			end if;			
			if count>=140000000 then 
			tmp <="01111111";
			elsif count>=120000000 then 
			tmp <="10111111";
			elsif count>=100000000 then 
			tmp <="11011111";
			elsif count>=80000000 then 
			tmp <="11101111";
			elsif count>=60000000 then 
			tmp <="11110111";
			elsif count>=40000000 then 
			tmp <="11111011";
			elsif count>=20000000 then 
			tmp <="11111101";
			else
			tmp <="11111110";
			end if;
			
		end if;
		led<=tmp;
	end process;
END bdf_type;

(3)十進制顯示,LED1閃爍十次,LED2閃爍一次,實現LED1對 LED1十分頻。

// 這個漏掉了,思路應該和之前的差不多, 大家自己嘗試嘗試。

LIBRARY ieee;
USE ieee.std_logic_1164.all; 

LIBRARY work;

ENTITY demo01 IS 
	PORT
	(
		clk :  IN  STD_LOGIC;
		led :  OUT  STD_LOGIC_VECTOR(0 to 1)
	);
END demo01;

ARCHITECTURE bdf_type OF demo01 IS 



BEGIN 

process(clk)
variable count1: integer :=0 ;
variable count2: integer :=0 ;
begin

if (clk'event and clk='0') then
	if count1 = 80000000 then
		count1 := 0;
		count2 := count2 + 1;
		if count2 = 10 then 
			count2 := 0;
		end if;
	else
		count1 :=count1+1;
	end if;
end if;

if count1 >=40000000 then led(0)<='1';
	else led(0)<='0';
end if;

if count2 = 9 and count1 <= 40000000 then led(1) <='0';
else led(1)<='1';
end if;
end process;



END bdf_type;

四、步進電機驅動

按下K1,步進電機正轉;松開,步進電機靜止,按下K2,步進電機反轉,松開,步進電機靜止。

先確認5V和其相鄰的引腳的跳線有沒有鏈接

MA (步進電機端) ---> 46 (FPGA端)

MB (步進電機端) ---> 45 (FPGA端)

MC (步進電機端) ---> 44 (FPGA端)

MD (步進電機端) ---> 43 (FPGA端)

FPGA端只要接到數字引腳就可以,在軟件PIN中配置對應的就可以了,

但最好和上面一致, 方便操作

代碼思路:

ABCD輪流高電平(例如A為'1' ,其它為'0'), 便能驅動電機

原理:

磁鐵同性相斥, 當A通電時和電機中心磁鐵相斥, 或者說產生了切向的安培力(我亂說的,大概是這樣。。。)

因此輪流高電平,就一直有切向的力!!!

LIBRARY ieee;
USE ieee.std_logic_1164.all; 
use ieee.std_logic_unsigned.all;


ENTITY stepper_motor2 IS 
	PORT
	(
		k1,k2: IN STD_LOGIC;
	
		clk :  IN  STD_LOGIC;
		
		led :  OUT  STD_LOGIC_vector(3 downto 0)
	);
END stepper_motor2;

ARCHITECTURE bdf_type OF stepper_motor2 IS 
-- 40 000 000   40000
signal count :integer range 0 to 4000000;
signal tmp :STD_LOGIC_vector(3 downto 0);

BEGIN 
	-- 注意每個if間是並行的, 但if里面的elsif是順序執行的(描述用的是軟件的想法)
	process(clk,k1,k2)
	-- 40 000 000 --> 4 000 000  10hz頻率
	begin
	if ( clk'event and clk='0'   ) then 
		if count=4000000 then 
				count <=0;
			else
				count <=count+1;
		end if;
			
		if count>=3000000 then 
            if (k1='0' and k2 = '1') then tmp <="0001";  --正轉
            elsif (k1='1' and k2 = '0') then tmp <="1000";  --反轉
            end if;
		elsif count>=2000000 then 
            if (k1='0' and k2 = '1') then tmp <="0010";
            elsif (k1='1' and k2 = '0') then tmp <="0100";
            end if;
		elsif count>=1000000 then 
            if (k1='0' and k2 = '1') then tmp <="0100";
            elsif (k1='1' and k2 = '0') then tmp <="0010";
            end if;
		else
            if (k1='0' and k2 = '1') then tmp <="1000";
            elsif (k1='1' and k2 = '0') then tmp <="0001";
            end if;
		end if;	

	end if;
	
    led<=tmp;
	
	end process;
	
	
END bdf_type;

五、EDA實驗課程設計
(1)學校統一模板。
(2)自主選題。

自己嘗試

FPGA芯片型號:Cyclone EP1C12Q240(6Q240兼容)
LED1 PIN_50 K1 PIN_121
LED2 PIN_53 K2 PIN_122
LED3 PIN_54 K3 PIN_123
LED4 PIN_55 K4 PIN_124
LED5 PIN_176 SYS_CLK0 PIN_28
LED6 PIN_47
LED7 PIN_48
LED8 PIN_49


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM