Top Banner
序序序序序序序序序序序序 VHDL 序序序序序序序序序序序 序序序序 -UE301
103

序向邏輯電路與狀態機設計

Jan 04, 2016

Download

Documents

flavia-pittman

序向邏輯電路與狀態機設計. VHDL 數位電路實習與專題設計 文魁資訊 -UE301. 內容大綱. 3-1 順序性敘述的使用 3-2 狀態機電路設計 2-3 VHDL 的指定敘述與基本語法 單元4:算術邏輯( ALU) 運算單元實習 單元5:除頻器與計數器實習 單元6:多工掃描式七段顯示器實習. 3-1順序性敘述的使用. If ( 條件1) Then -- 若(條件1)情況發生時,則執行… 指令敘述; Elsif ( 條件2) Then -- 否則若是(條件2)情況發生時,則執行… 指令敘述; : Else -- 否則則執行….. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 序向邏輯電路與狀態機設計

第第 三三 章章

序向邏輯電路與狀態機設計

VHDL 數位電路實習與專題設計文魁資訊 -UE301

Page 2: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 2---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

內容大綱

3-1 順序性敘述的使用 3-2 狀態機電路設計 2-3 VHDL 的指定敘述與基本語法 單元 4 :算術邏輯 (ALU) 運算單元實習 單元 5 :除頻器與計數器實習 單元 6 :多工掃描式七段顯示器實習

Page 3: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 3---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1 順序性敘述的使用

Page 4: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 4---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1-1 If-Then-Else 敘述

順序性敘述指令必須要放在 PROCESS 程式主體內使用,關於 If-Then-Else 敘述的語法如下:

If ( 條件 1) Then -- 若 ( 條件 1) 情況發生時,則執行…指令敘述 ;Elsif ( 條件 2) Then -- 否則若是 ( 條件 2) 情況發生時,則執行…指令敘述 ;:Else -- 否則則執行… ..指令敘述 ;End If; -- 宣告結束

Page 5: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 5---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

D 型正反器

3-1-1 If-Then-Else 敘述

D

CLK

QCLK D Q(t+1)

0 X Q(t)

1 X Q(t)

↑ 1 1

↑ 0 0

LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL; ENTITY dff_v isPORT( CLK,D : IN STD_LOGIC;

Q : OUTSTD_LOGIC );END dff_v; ARCHITECTURE a OF dff_v ISBEGIN

PROCESS (CLK)BEGIN

IF CLK'event AND CLK='1' THEN Q <= D; END IF;

END PROCESS;END a;

clk

event

Page 6: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 6---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1-2 計數器與除頻電路 計數器設計

上數計數器:計數值會隨著時脈訊號(正緣或負緣觸發)的發生而自動加‘1’

下數計數器:在每次的觸發時計數值減一

PROCESS (CLK) BEGINIF CLK'event AND CLK='1' THENQN <= QN+1; -- 計數值加一END IF;END PROCESS;

PROCESS (CLK) BEGINIF CLK'event AND CLK='1' THENQN <= QN-1; -- 計數值減一END IF;END PROCESS;

Page 7: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 7---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1-2 計數器與除頻電路 上下數計數器:將上數和下數的功能合併,再使用一個控制訊號 DIR 來決定其

上下數的動作

1 LIBRARY IEEE;2 USE IEEE.STD_LOGIC_1164.ALL;3 USE IEEE.STD_LOGIC_UNSIGNED.ALL;45 ENTITY UD_COUNTER is6 PORT( CLK,DIR: IN STD_LOGIC;7 Q : OUTSTD_LOGIC_VECTOR(3 DOWNTO 0));8 END UD_COUNTER;910 ARCHITECTURE a OF UD_COUNTER IS11 SIGNAL QN : STD_LOGIC_VECTOR(3 DOWNTO 0);

12 BEGIN

13 PROCESS (CLK)

14 BEGIN

15 IF CLK'event AND CLK='1' THEN

16 IF DIR='0' THEN QN <= QN+1;

17 ELSE QN <= QN-1;

18 END IF;

19 END IF;

20 END PROCESS;

21

22 Q<=QN;

23 END a;

Page 8: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 8---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1-2 計數器與除頻電路 輸出為對稱方波形狀的除頻器設計

在 FPT-3 實驗板上的石英振盪器之振盪頻率是 1.8432MHz (一秒振盪1.8432x106次),如果我們想要設計一個 1/(1.8432x106) 的除頻電路來得到輸出波形為對稱方波的 1Hz 輸出頻率時,我們可以宣告一個數值為振盪頻率值一半的整數 ( 即 (1.8432x106)/2) ,每當該數值數完一次時就令其對應的輸出訊號波形發生轉態,那麼當計數完振盪頻率的數值時輸出訊號恰完成一個週期的正反變化,如此一來我們便能輕易地得到 1Hz的方波訊號了。

1.8432x106

Counter value(1.8432x106/2) Counter value( 1.8432x106/2)

PULSE

Page 9: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 9---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1-2 計數器與除頻電路

1 library ieee;2 use ieee.std_logic_1164.all;3 use ieee.std_logic_unsigned.all;45 ENTITY slowCLK18432 IS 6 PORT ( clockIN : IN std_logic ; 7 clockOUT : OUT std_logic); 8 END SLOWCLK18432; 910 ARCHITECTURE behavioral OF slowCLK18432 IS 11 SIGNAL PULSE : std_logic := '0'; 12 BEGIN 13 PROCESS (clockIN) 14 VARIABLE Counter : INTEGER RANGE 0 TO 1843200/2 := 1843200/2;

輸出為對稱方波形狀的除頻器設計 ( 續 )

Page 10: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 10---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1-2 計數器與除頻電路

輸出為對稱方波形狀的除頻器設計 ( 續 )

15 BEGIN

16 IF (clockIN'EVENT AND clockIN='1') THEN

17 Counter := Counter - 1;

18 IF (Counter = 0) THEN

19 IF PULSE = '1' THEN

20 PULSE <= '0';

21 ELSE PULSE <= '1';

22 END IF;

23 Counter := 1843200/2;

24 END IF;

25 END IF;

26 END PROCESS;

27 clockOUT <= PULSE;

28 END behavioral;

Page 11: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 11---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1-3 case-when 敘述

Case 選擇訊號 IS

When 選擇訊號 1 =>

敘述命令 1;

When 選擇訊號 2 =>

敘述命令 2;

:

When Others =>

敘述命令 N;

End Case;

Case-when 指令本身是敘述組合邏輯的,但它卻必須在 Process 指令中執行,因此它是序向邏輯指令之一 , 其語法如下

Page 12: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 12---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1-3 case-when 敘述

C

0

0

0

0

1

1

1

1

S1

0

0

1

1

0

0

1

1

S0

0

1

0

1

0

1

0

1

Operation

F<=A;

F<=A and B;

F<=A or B;

F<=Not A;

F<=A+B;

F<=A-B;

F<=A+1;

F<=A-1;

library IEEE;use IEEE.STD_LOGIC_1164.all;use IEEE.std_logic_unsigned.all; ENTITY alu ISPORT ( C : in std_logic; S : IN STD_LOGIC_VECTOR(1 downto 0); A,B : IN STD_LOGIC_VECTOR(7 downto 0); F : OUT STD_LOGIC_VECTOR(7 downto 0) );END alu;

ALU 功能電路

Page 13: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 13---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1-3 case-when 敘述

ALU 功能電路 ( 續 )

ARCHITECTURE a OF alu ISBEGIN process(s,A,B) Begin IF C='0' then -- 當 C=’0’ 時執行邏輯運算 Case s IS When "00"=> F<=A; When "01"=> F<=A and B; --AND 邏輯運算 When "10"=> F<=A or B; --OR 邏輯運算 When others=> F<=Not A; --NOT 邏輯運算 END case;

ELSE -- 當 C=’1’ 時執行算術運算 Case s IS When "00"=> F<=A+B; -- 加法運算 When "01"=> F<=A-B; -- 減法運算 When "10"=> F<=A+1; -- 遞增 1 When others=> F<=A-1; -- 遞減 1 End case; end if; End Process;END a;

Page 14: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 14---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1-4 For-Loop 敘述

A(0)

B(1)

A(1)

B(2)

A(2)

B(0)

F(0)

F(1)

F(2)

Process(A, B)

Begin

LP1: for I in 0 to 2 Loop

F(I)<=A(I) AND B(2-I);

end Loop LP1;

end Process;

FOR-LOOP 敘述VHDL 語言使用 For-Loop 敘述來描述重複性的電路動作,其語法如下: FOR I IN 開始值 to 結束值 LOOP -- 以遞增的方式從開始值執行到結束值為止 命令敘述End Loop;

Page 15: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 15---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-1-4 For-Loop 敘述

四位元移位暫存器 architecture a of shift4 issignal temp : std_logic_vector(3 downto 0);beginprocess(clk)begin IF clk'event and clk='1' then temp(3)<=data; For I IN 1 to 3 LOOP temp(3-I)<=temp(4-I); END LOOP; END IF;end process ;Q<=temp;end a;

Library IEEE;use IEEE.std_logic_1164.all;

entity shift4 ISport( data,clk :in std_logic; Q :out std_logic_vector(3 downto 0));end shift4;

Page 16: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 16---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-2 狀態機電路設計

Page 17: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 17---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-2-1 Moore 狀態機

Moore 狀態機的輸出與狀態有關,與輸入無關。

S0/1

S3/0 S1/0

S2/1

0

11

0

11

0

0Moore 狀態機對應之真值表

現態 次態

  X=0 X=1 輸出

S0 S0 S1 1

S1 S3 S2 0

S2 S2 S3 1

S3 S3 S0 0

Page 18: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 18---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-2-1 Moore 狀態機

ARCHITECTURE a OF moore ISTYPE State IS (s0,s1,s2,s3);SIGNAL present_state : State;SIGNAL next_state: State;BEGIN

state_comp: PROCESS(present_state)BEGIN

CASE present_state ISWHEN s0 =>

IF X = '0' THENnext_state <= s0;

ELSEnext_state <= s1;

END IF;OP <= '1';

WHEN s1 =>IF X = '0' THEN

next_state <= s3;ELSE

next_state <= s2;END IF;OP <= '0';

WHEN s2 =>IF X = '0' THEN

next_state <= s2;ELSE

next_state <= s3;END IF;OP <= '1';WHEN s3 =>IF X = '0' THEN

next_state <= s3;ELSE

next_state <= s0;END IF;

OP <= '0'; END CASE;

END PROCESS state_comp; 

state_clocking: PROCESS (CLK)BEGIN

IF CLK'EVENT AND CLK = '1' THENpresent_state <= next_state;

END IF;END PROCESS state_clocking;

END a;

Page 19: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 19---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-2-2 Mealy 狀態機

Mealy 狀態機的輸出與輸入和狀態均有關。

S0

S3 S1

S2

0/0

1/01/1

0/1

1/11/1

0/0

0/0

現態 次態 輸出

  X=0 X=1 X=0 X=1

S0 S0 S1 0 1

S1 S3 S2 1 1

S2 S2 S3 0 1

S3 S3 S0 0 0

Page 20: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 20---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-2-2 Mealy 狀態機ARCHITECTURE a OF mealy ISTYPE State IS (s0,s1,s2,s3);SIGNAL present_state : State;SIGNAL next_state: State; BEGIN

state_comp: PROCESS(present_state)

BEGINCASE present_state IS

WHEN s0 =>IF X = '0' THEN

next_state <= s0;ELSE

next_state <= s1;END IF;IF X = '0' THEN

op <= '0';ELSE

op <= '1';END IF;

WHEN s1 =>IF X = '0' THEN

next_state <= s3;ELSE

next_state <= s2;END IF;IF X = '0' THEN

op <= '1';ELSE

op <= '1';END IF;

WHEN s2 =>IF X = '0' THEN

next_state <= s2;ELSE

next_state <= s3;END IF;IF X = '0' THEN

op <= '0';ELSE

op <= '1';END IF;

Page 21: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 21---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

3-2-2 Mealy 狀態機

WHEN s3 =>IF X = '0' THEN

next_state <= s3;ELSE

next_state <= s0;END IF;IF X = '0' THEN

op <= '0';ELSE

op <= '0';END IF;

END CASE;END PROCESS state_comp;

 state_clocking: PROCESS (CLK)BEGIN

IF CLK'EVENT AND CLK = '1' THENpresent_state <= next_state;

END IF;END PROCESS state_clocking;

END a;

Page 22: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 22---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 4 :算術邏輯 (ALU) 運算單元實習

Page 23: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 23---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 4 :算術邏輯 (ALU) 運算單元實習

相關知識

M S1 S0 Operation

0 0 0 F<=A;

0 0 1 F<=A and B;

0 1 0 F<=A or B;

0 1 1 F<=Not A;

1 0 0 F<=A+B;

1 0 1 F<=A-B;

1 1 0 F<=A+1;

1 1 1 F<=A-1;

Page 24: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 24---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 4 :算術邏輯 (ALU) 運算單元實習 實驗電路圖

SW DIP-8

F0 A0

SW1

F1

VCC

S0

F2

M

B1B0

U5

EPM7064S/LCC44

456

2931333436

37

1442

I/OI/OI/O

I/OI/OI/OI/OI/O

I/O

I/OI/OI/O

A2A1

B2

VCC

S1

VCC

Page 25: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 25---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 4 :算術邏輯 (ALU) 運算單元實習

程式與說明 7 library IEEE;8 use IEEE.STD_LOGIC_1164.all;9 use IEEE.std_logic_unsigned.all;1011 ENTITY alu IS12 PORT ( M : in std_logic;13 s : IN STD_LOGIC_VECTOR(1 downto 0);14 A,B:IN STD_LOGIC_VECTOR(2 downto 0);15 F :OUT STD_LOGIC_VECTOR(2 downto 0) );16 END alu;17 18 ARCHITECTURE a OF alu IS19 BEGIN20 process(s,A,B)21 Begin

22 IF M='0' then23 Case s IS24 When "00"=>25 F<=A;26 When "01"=>27 F<=A and B;28 When "10"=>29 F<=A or B;30 When others=>31 F<=Not A;32 END case;

33 ELSE34 Case s IS35 When "00"=>36 F<=A+B;37 When "01"=>38 F<=A-B;39 When "10"=>40 F<=A+1;41 When others=>42 F<=A-1;43 End case;44 end if;45 End Process;46 END a;

Page 26: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 26---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 4 :算術邏輯 (ALU) 運算單元實習

功能模擬與 CPLD 下載驗證 燒錄於力浦電子 FPT-3 實驗板

輸入 腳位 輸出 腳位

M 37(SW1) F(2) 4 ( LED1)

S(1) 29(DIP1) F(1) 5 ( LED2)

S(0) 31(DIP2) F(0) 6 ( LED3)

A(2) 33(DIP3)    

A(1) 34(DIP4)    

A(0) 36(DIP5)    

B(2) 1(DIP6)    

B(1) 44(DIP7)    

B(0) 2(DIP8)    

M

A2~A0 B2~B0

F2~F0

S1~S0

Page 27: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 27---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 4 :算術邏輯 (ALU) 運算單元實習

功能模擬與 CPLD 下載驗證 燒錄於力浦電子 LP-2900 實驗板 ( 實驗電路圖 )

Page 28: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 28---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 4 :算術邏輯 (ALU) 運算單元實習

功能模擬與 CPLD 下載驗證 燒錄於力浦電子 LP-2900 實驗板 ( 續 )

輸入 腳位 輸出 腳位

A(2) 49 ( SW3)

F(2) 18 ( L10 )

A(1) 51 ( SW4)

F(1) 19 ( L11 )

A(0) 59 ( SW5)

F(0) 20 ( L12 )

B(2) 60 ( SW6)

LED_COM 141

B(1) 62 ( SW7)

   

B(0) 63 ( SW8)

   

M 64 ( SW9)

   

S(1) 65 ( SW10)

   

S(0) 67 ( SW11)

   

Page 29: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 29---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 4 :算術邏輯 (ALU) 運算單元實習

功能模擬與 CPLD 下載驗證 燒錄於力浦電子 LP-2900 實驗板 ( 續 )

Page 30: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 30---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5 :除頻器與計數器實習

Page 31: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 31---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-1 產生 1 、 2 、 4 、 8Hz 不同頻率之除頻器設計 相關知識

將 1843200Hz 除頻得到 8Hz 頻率輸出

1.8432x106

divisor(230400) divisor(230400)

CN2

divisor/2 divisor/2

以自由計數器得到 4Hz 、 2Hz 和 1Hz 的信號頻率

Page 32: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 32---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-1 產生 1 、 2 、 4 、 8Hz 不同頻率之除頻器設計 實驗電路圖

VCC

clk_out4Hz

CLK_IN

clk_out1Hzclk_out2Hz

U1

EPM7064S/LCC44

4568

43

I/OI/OI/OI/O

I/GCLK1

clk_out8Hz

Page 33: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 33---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-1 產生 1 、 2 、 4 、 8Hz 不同頻率之除頻器設計

 

程式與說明7 library ieee;8 use ieee.std_logic_1164.all;9 use ieee.std_logic_unsigned.all;10 use ieee.std_logic_arith.all;1112 entity clk_div is13 generic(divisor:integer:=230400);14 port(15 clk_in : in std_logic;16 clk_out8Hz: buffer std_logic;17 clk_out4Hz: buffer std_logic;18 clk_out2Hz: buffer std_logic;19 clk_out1Hz: buffer std_logic20 ); 21 end clk_div;22

23 architecture arch of clk_div is24 signal cnt2 : std_logic;25 begin26 ---------- clk divider ----------27 process(clk_in)28 variable cnt1,divisor2 : integer range 0 to divisor;29 begin30 divisor2:=divisor/2;31 ----- up counter -----32 if (clk_in'event and clk_in='1') then33 if cnt1 = divisor then34 cnt1 := 1;35 else36 cnt1 := cnt1 + 1;37 end if;38 end if;

Page 34: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 34---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-1 產生 1 、 2 、 4 、 8Hz 不同頻率之除頻器設計 程式與說明 (續 )

39 ----- clk_out register clk generator -----40 if (clk_in'event and clk_in='1') then41 if (( cnt1 = divisor2) or (cnt1 = divisor))then42 cnt2 <= not cnt2 ;43 end if;44 end if;45 clk_out8Hz <= cnt2 ;46 end process;4748 process(clk_out8Hz)49 begin50 if clk_out8Hz'event and clk_out8Hz='1' then51 clk_out4Hz <= not(clk_out4Hz);52 end if;53 end process;

5455 process(clk_out4Hz)56 begin57 if clk_out4Hz'event and clk_out4Hz='1' then58 clk_out2Hz <= not(clk_out2Hz);59 end if;60 end process;6162 process(clk_out2Hz)63 begin64 if clk_out2Hz'event and clk_out2Hz='1' then65 clk_out1Hz <= not(clk_out1Hz);66 end if;67 end process;6869 end arch;

Page 35: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 35---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-1 產生 1 、 2 、 4 、 8Hz 不同頻率之除頻器設計 功能模擬與 CPLD 下載驗證

燒錄於力浦電子 FPT-3 實驗板

 

輸入

clk_in

 

 

 

腳位

43

 

 

 

輸出

clk_out8Hz

clk_out4Hz

clk_out2Hz

clk_out1Hz

腳位

4

5

6

8

clk_out8Hz

clk_out4Hz

clk_out2Hz

clk_out1Hz

Page 36: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 36---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-1 產生 1 、 2 、 4 、 8Hz 不同頻率之除頻器設計 功能模擬與 CPLD 下載驗證

燒錄於力浦電子 LP-2900 實驗板 更改除頻程式如下 :

腳位配置圖 :

12 entity clk_div_LP2900 is13 generic(divisor:integer:= 1250000);14 port( …);

輸入 腳位 輸出 腳位clk_in 55 clk_out8Hz 17 ( L9 )

  

clk_out4Hz 18 ( L10 )

  

clk_out2Hz 19 ( L11 )

  

clk_out1Hz 20 ( L12 )

  

LED_COM 141

Page 37: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 37---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-1 產生 1 、 2 、 4 、 8Hz 不同頻率之除頻器設計 功能模擬與 CPLD 下載驗證

燒錄於力浦電子 LP-2900 實驗板 ( 續 ) LP-2900 實驗板元件規劃 :

Page 38: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 38---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-2 計數器設計與跑馬燈顯示

相關知識 計數器設計與跑馬燈顯示架構圖

0111

1011

1101

1110

Clk1

Clk2

Clk3

Clk4

Clk_in

頻率產生器 counter

clk cnt

3bin

3 to 8decoder

Bin2led.vhdCounter.vhdClk_div.vhd sel4

可選擇輸入頻率的計數器電路符號 0111

1011

1101

1110

Clk1

Clk2

Clk3

Clk4counter

clk cnt

3

Counter.vhdsel4

Page 39: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 39---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-2 計數器設計與跑馬燈顯示

實驗電路圖

LED4

LED7

CLK

SEL2

U1

EPM7064S/LCC44

45689

111214

36

43

1442

I/OI/OI/OI/OI/OI/OI/OI/O

I/O

I/GCLK1

I/OI/OI/O

SEL0

LED2

VCC

LED0LED1

LED3

LED5

SW DIP-8

LED6

SEL3

VCC

SEL1

Page 40: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 40---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-2 計數器設計與跑馬燈顯示 程式與說明

由 1.8432MHz 產生 1Hz 、 2Hz 、 4Hz 和 8Hz 輸出頻率之除頻電路程式碼

7 library ieee;8 use ieee.std_logic_1164.all;9 use ieee.std_logic_unsigned.all;10 use ieee.std_logic_arith.all;1112 entity clk_div is13 generic(divisor:integer:=230400);14 port(15 clk_in : in std_logic;16 clk_out8Hz: buffer std_logic;17 clk_out4Hz: buffer std_logic;18 clk_out2Hz: buffer std_logic;19 clk_out1Hz: buffer std_logic20 ); 21 end clk_div;2223 architecture arch of clk_div is24 signal cnt2 : std_logic;25 begin

26 ---------- clk divider ----------27 process(clk_in)28 variable cnt1,divisor2 : integer range 0 to divisor;29 begin30 divisor2:=divisor/2;31 ----- up counter -----32 if (clk_in'event and clk_in='1') then 33 if cnt1 = divisor then34 cnt1 := 1;35 else36 cnt1 := cnt1 + 1;37 end if;38 end if;39 ----- clk_out register clk generator -----40 if (clk_in'event and clk_in='1') then41 if (( cnt1 = divisor2) or (cnt1 = divisor))then42 cnt2 <= not cnt2 ;43 end if;44 end if;45 clk_out8Hz <= cnt2 ;46 end process;

Page 41: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 41---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-2 計數器設計與跑馬燈顯示

47 48 process(clk_out8Hz)49 begin50 if clk_out8Hz'event and clk_out8Hz='1' then51 clk_out4Hz <= not(clk_out4Hz);52 end if;53 end process;5455 process(clk_out4Hz)56 begin57 if clk_out4Hz'event and clk_out4Hz='1' then58 clk_out2Hz <= not(clk_out2Hz);59 end if;60 end process;61

62 process(clk_out2Hz)63 begin64 if clk_out2Hz'event and clk_out2Hz='1' then65 clk_out1Hz <= not(clk_out1Hz);66 end if;67 end process;6869 end arch;

程式與說明 ( 續 ) 由 1.8432MHz 產生 1Hz 、 2Hz 、 4Hz 和 8Hz 輸出頻率之除頻電路程式

Page 42: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 42---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-2 計數器設計與跑馬燈顯示 程式與說明

可選擇四個不同輸入頻率之計數器電路程式碼 7 library ieee;8 use ieee.std_logic_1164.all;9 use ieee.std_logic_unsigned.all;10 use ieee.std_logic_arith.all;1112 entity counter is13 port (14 clk1 : in std_logic;15 clk2 : in std_logic;16 clk3 : in std_logic;17 clk4 : in std_logic;18 sel : in std_logic_vector(3 downto 0);19 cnt : out std_logic_vector (2 downto 0) 20 );• end counter;23 architecture arch of counter is24 signal clk: std_logic;25 signal cnt1 : std_logic_vector(2 downto 0);26 begin

28 ------PROGRAM BODY------------------------------29 process (sel,clk1,clk2,clk3,clk4)30 begin31 case sel is32 when "0111" => clk <= clk1; --33 when "1011" => clk <= clk2; --34 when "1101" => clk <= clk3; --35 when "1110" => clk <= clk4; --36 when others => clk <= '0';37 end case;38 end process;40 process(clk)41 begin42 if clk'event and clk='1' then43 cnt1<=cnt1+1;44 end if;45 end process;46 cnt<=cnt1;47 end arch;

Page 43: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 43---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-2 計數器設計與跑馬燈顯示 程式與說明

輸出為低電位動作之 3 to 8解碼器電路程式碼

7 library ieee;8 use ieee.std_logic_1164.all;910 ------------------------------------------------------11 entity bin2led is12 port (13 bin : in std_logic_vector (2 downto 0); --3 bits14 led : out std_logic_vector (7 downto 0) --8 leds15 );16 end bin2led;1718 architecture arch of bin2led is19 begin20

21 -------PROGRAM BODY------------------------------22 process (bin)23 begin24 case bin is25 when "000" => led <= "11111110"; -- 0 active low26 when "001" => led <= "11111101"; -- 127 when "010" => led <= "11111011"; -- 228 when "011" => led <= "11110111"; -- 329 when "100" => led <= "11101111"; -- 430 when "101" => led <= "11011111"; -- 531 when "110" => led <= "10111111"; -- 632 when "111" => led <= "01111111"; -- 733 when others => led <= "11111111"; 34 end case;35 end process;36 37 end arch;

Page 44: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 44---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-2 計數器設計與跑馬燈顯示 程式與說明

主程式程式碼

7 library ieee;8 use ieee.std_logic_1164.all;9 use ieee.std_logic_unsigned.all;10 entity clk_div_led is11 port (12 clk : in std_logic; -- 1.8432 MHz13 sel : in std_logic_vector(3 downto 0);14 led : out std_logic_vector(7 downto 0)15 );16 end clk_div_led;1718 architecture arch of clk_div_led is1920 -----COMPONENT DECLARED-------------2122 component clk_div

23 generic(divisor:integer:=230400);24 port( 25 clk_in : in std_logic;26 clk_out8Hz: buffer std_logic;27 clk_out4Hz: buffer std_logic;28 clk_out2Hz: buffer std_logic;29 clk_out1Hz: buffer std_logic30 );31 end component;3233 component counter34 port (35 clk1 : in std_logic;36 clk2 : in std_logic;37 clk3 : in std_logic;38 clk4 : in std_logic;39 sel : in std_logic_vector(3 downto 0);40 cnt : out std_logic_vector (2 downto 0) 41 );42 end component;43

Page 45: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 45---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-2 計數器設計與跑馬燈顯示 程式與說明

主程式程式碼 (續 )

44 component bin2led45 port (46 bin : in std_logic_vector (2 downto 0); --3 bits47 led : out std_logic_vector (7 downto 0) --8 leds48 );49 end component;50 ---------SIGNAL DECLARED-------------------------------51 signal clk1,clk2,clk3,clk4 : std_logic;52 signal bin: std_logic_vector(2 downto 0);5354 begin5556 u1: clk_div port map(clk,clk1,clk2,clk3,clk4);57 u2: counter port map(clk1,clk2,clk3,clk4,sel,bin);58 u3: bin2led port map(bin,led);5960 end arch;

Page 46: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 46---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-2 計數器設計與跑馬燈顯示

功能模擬與 CPLD 下載驗證 燒錄於力浦電子 FPT-3 實驗板

輸入 腳位 輸出 腳位

CLK 43 LED(0) 4 ( LED1 )

Sel(3) 36(DIP5) LED(1) 5 ( LED2 )

Sel(2) 1(DIP6) LED(2) 6 ( LED3 )

Sel(1) 44(DIP7) LED(3) 8 ( LED4 )

Sel(0) 2(DIP8) LED(4) 9 ( LED5 )

    LED(5) 11 ( LED6)

    LED(6) 12 ( LED7)

    LED(7) 14 ( LED8)

SEL3~SEL0

LED0~LED7

Page 47: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 47---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-2 計數器設計與跑馬燈顯示

功能模擬與 CPLD 下載驗證 燒錄於力浦電子 LP-2900 實驗板

Page 48: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 48---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-2 計數器設計與跑馬燈顯示

功能模擬與 CPLD 下載驗證 燒錄於力浦電子 LP-2900 實驗板 ( 續 )

Page 49: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 49---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計

相關知識 當電磁式喇叭的線圈通過電流脈波時,就會因為薄膜振動而發

出聲音,但是一個單一的脈波送到喇叭卻只能發出一個 click 的聲響,若要產生音樂聲的音調就必需讓喇叭持續的振動。一個音調的高低取決於發聲振動頻率的大小,而音量的高低則是由電流量的大小來決定。

薄膜放 薄膜吸

低頻 -> 低音調

高頻 -> 高音調

一週期

Page 50: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 50---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計

Page 51: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 51---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計

實驗功能 限於 FPT-3 實驗板上的 EPM7064SLC44-10這顆晶片僅有 64 個 LCs 的容量,因此實驗中我們僅實驗 Do 和 Rai兩個音:

SW1( 指撥開關設定為 01)->DoSW2( 指撥開關設定為 10)->Rai

至於力浦 LP-2900 、 Altera UP1 X 實驗板或其他 SRAM製程技術的 CPLD晶片(如 FLEX 10K系列)的元件一般來說可規劃的容量均甚大,在進行實驗時應較不需要如此特別留意容量限制的問題。

Page 52: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 52---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計

實驗電路圖

CLK

VCCVCC

SP1

BUZZER

SW DIP-8

U1

EPM7064S/LCC44

28

43

442

I/O

I/GCLK1

I/OI/O SEL1

SEL0

Page 53: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 53---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計

程式與說明 簡易電子琴實習除頻器程式碼:

8 library ieee;9 use ieee.std_logic_1164.all;10 use ieee.std_logic_unsigned.all;11 use ieee.std_logic_arith.all;1213 entity clk_div_music is14 generic(divisor:integer:=31);15 port(16 clk_in : in std_logic;17 clk_out: out std_logic18 ); 19 end clk_div_music;2021 architecture arch of clk_div_music is22 signal cnt2 : std_logic;23 begin

24 ---------- clk divider ----------25 process(clk_in)26 variable cnt1,divisor2 : integer range 0 to divisor;27 begin28 divisor2:=divisor/2;29 ----- up counter -----30 if (clk_in'event and clk_in='1') then31 if cnt1 = divisor then32 cnt1 := 1;33 else34 cnt1 := cnt1 + 1;35 end if;36 end if;

Page 54: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 54---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計

程式與說明 簡易電子琴實習除頻器程式碼 ( 續 ) :

37 ----- clk_out register clk generator -----38 if (clk_in'event and clk_in='1') then39 if (( cnt1 = divisor2) or (cnt1 = divisor))then40 cnt2 <= not cnt2 ;41 end if;42 end if;43 clk_out <= cnt2 ;44 end process;45 end arch;46

Page 55: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 55---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計

程式與說明 簡易電子琴主程式碼

8 library ieee;9 use ieee.std_logic_1164.all;10 use ieee.std_logic_unsigned.all;11 use ieee.std_logic_arith.all;1213 entity music is14 port(15 clk : in std_logic;16 sel : in std_logic_vector(1 downto 0);17 music_out : out std_logic18 ); 19 end music;2021 architecture arch of music is22

23 component clk_div_music24 generic(divisor:integer:=8);25 port(26 clk_in : in std_logic;27 clk_out: out std_logic28 ); 29 end component;3031 signal clk1,clk2: std_logic;3233 begin3435 u1: clk_div_music36 generic map(7045) --Do 261.6Hz 1.8432M->7045 37 port map(clk,clk1);38 u2: clk_div_music

Page 56: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 56---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計

程式與說明 簡易電子琴主程式碼 ( 續 )

39 generic map(6275) --Rai 293.7Hz 1.8432M->6275 40 port map(clk,clk2);4142 ---------- process ---------43 process (sel,clk1,clk2)44 begin45 case sel is46 when "01" => music_out <= clk1; --SW1 -> Do47 when "10" => music_out <= clk2; --SW2 -> Rai48 when others => music_out <= '0';49 end case;50 end process;51 52 end arch;

Page 57: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 57---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計 功能模擬與CPLD下載驗證

燒錄於力浦電子FPT-3實驗板

輸入 腳位 輸出 腳位

clk 43 Music_out 28(SP1)

Sel(1) 44(DIP7)    

Sel(0) 2(DIP8)    

       

Music_outSEL1~SEL0

Page 58: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 58---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計

功能模擬與 CPLD 下載驗證 燒錄於力浦電子 LP-2900 實驗板 由於 LP-2900 實驗板上的 EPF10K10TC144-4晶片容量較大,因此我們可以更改程式以實現較多的音調頻率( LP-2900上之振盪頻率為 10MHz )。

33 u1: clk_div_music34 generic map(38226) --Do 261.6Hz 35 port map(clk,clk1);36 u2: clk_div_music37 generic map(34048) --Rai 293.7Hz 38 port map(clk,clk2);39 u3: clk_div_music40 generic map(30339) --Mi 329.6Hz 41 port map(clk,clk3);42 u4: clk_div_music43 generic map(28638) --Fa 349.1Hz 44 port map(clk,clk4);

45 u5: clk_div_music46 generic map(25510) --so 392.0Hz 47 port map(clk,clk5);48 u6: clk_div_music49 generic map(22727) --La 440.0Hz 50 port map(clk,clk6);51 u7: clk_div_music52 generic map(20275) --si 493.2Hz 53 port map(clk,clk7);54 u8: clk_div_music55 generic map(19109) --Do 523.3Hz 56 port map(clk,clk8);57

Page 59: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 59---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計

功能模擬與 CPLD 下載驗證燒錄於力浦電子 LP-2900 實驗板 ( 續 )

輸入 腳位 輸出 腳位

clk 55 Music_out 46 ( BUZZER )

Sel(7) 47(SW1)    

Sel(6) 48(SW2)    

Sel(5) 49(SW3)    

Sel(4) 51(SW4)    

Sel(3) 59(SW5)    

Sel(2) 60(SW6)    

Sel(1) 62(SW7)    

Sel(0) 63(SW8)    

Page 60: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 60---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-3 簡易電子琴設計

功能模擬與 CPLD 下載驗證燒錄於力浦電子 LP-2900 實驗板 ( 續 )

Music_out

Page 61: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 61---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-4 Moore 狀態機與電子音樂歌曲的編輯製作

相關知識

0000

01xxxxxx

狀態機計數器

0000

0001

0010

0011

0100

0101

0110

蜂鳴器

多工器

0111

4986.4Hz

261.6Hz

293.7Hz

329.6Hz

349.2Hz

392Hz

440Hz

493.2Hz

音調產生器0

10Mhz

Do

Rei

Mi

Fa

So

La

Si

clk

Page 62: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 62---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-4 Moore 狀態機與電子音樂歌曲的編輯製作

實驗電路圖

VCC

CLK

SP1

BUZZER

EPF10K10/LCC

4655 I/O

CLK

Page 63: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 63---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-4 Moore 狀態機與電子音樂歌曲的編輯製作 程式與說明

除頻器設計

12 entity clk_div_music is13 generic(divisor:integer:= 31);14 port(15 clk_in : in std_logic;16 clk_out: out std_logic17 ); 18 end clk_div_music;19 20 architecture arch of clk_div_music is21 signal cnt2 : std_logic;22 begin23 ---------- clk divider ----------24 process(clk_in)25 variable cnt1,divisor2 : integer range 0 to divisor;26 begin27 divisor2:=divisor/2;28 ----- up counter -----29 if (clk_in'event and clk_in='1') then30 if cnt1 = divisor then31 cnt1 := 1;

32 else33 cnt1 := cnt1 + 1;34 end if;35 end if;36 ----- clk_out register clk generator -----37 if (clk_in'event and clk_in='1') then38 if (( cnt1 = divisor2) or (cnt1 = divisor))then39 cnt2 <= not cnt2 ;40 end if;41 end if;42 clk_out <= cnt2 ;43 end process;44 end arch;

Page 64: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 64---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-4 Moore 狀態機與電子音樂歌曲的編輯製作 程式與說明

音調產生器

12 entity music_gene is13 port(14 clk : in std_logic;15 clk1,clk2,clk3,clk4,clk5, clk6,clk7,clk8 : out std_logic16 ); 17 end music_gene;18 19 architecture arch of music_gene is2021 component clk_div_music22 generic(divisor:integer:=8);23 port(24 clk_in : in std_logic;25 clk_out: out std_logic26 ); 27 end component;

282930 begin3132 u1: clk_div_music33 generic map(38226) --Do 261.6Hz 34 port map(clk,clk1);35 u2: clk_div_music36 generic map(34048) --Rai 293.7Hz 37 port map(clk,clk2);38 u3: clk_div_music39 generic map(30339) --Mi 329.6Hz 40 port map(clk,clk3);41 u4: clk_div_music42 generic map(28638) --Fa 349.1Hz 43 port map(clk,clk4);44 u5: clk_div_music45 generic map(25510) --so 392.0Hz 46 port map(clk,clk5

47 u6: clk_div_music48 generic map(22727) --La 440.0Hz 48 port map(clk,clk6);

Page 65: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 65---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-4 Moore 狀態機與電子音樂歌曲的編輯製作

50 u7: clk_div_music51 generic map(20275) --si 493.2Hz 52 port map(clk,clk7);53 u8: clk_div_music54 generic map(19109) --Do 523.3Hz 55 port map(clk,clk8);56 57 58 end arch;

程式與說明 音調產生器 ( 續 )

Page 66: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 66---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-4 Moore 狀態機與電子音樂歌曲的編輯製作 程式與說明

狀態機計數器設計 (moore 狀態機 )

S0

0101

S1

0011

S2

0011

S3

0100

S4

0010

S5

0010

S6

0001

S11

0101

S10

0101

S9

0100

S8

0011

S7

0010

S12

0101

Page 67: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 67---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-4 Moore 狀態機與電子音樂歌曲的編輯製作 程式與說明

多工器電路

頻率產生器 十模計數器

頻率產生器

Page 68: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 68---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-4 Moore 狀態機與電子音樂歌曲的編輯製作 程式與說明

主程式電路連線 (electmusic.gdf)

Page 69: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 69---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-4 Moore 狀態機與電子音樂歌曲的編輯製作

功能模擬與CPLD下載驗證 (力浦電子LP-2900實驗板 )

輸入 腳位 輸出 腳位

10Mhz 55 Music_out 46 (連接蜂鳴器)

       

Page 70: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 70---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-5 移位暫存器與電子音樂歌曲的編輯製作

相關知識 000

001

010

011

100

101

110

Do

Rei

Mi

Fa 蜂鳴器多工器So

La

Si 111

3

0 1 1 0 0 1 00

261.6Hz

293.7Hz

329.6Hz

349.2Hz

392Hz

440Hz

493.2Hz音調產生器

0

10MHz

5Hz 移位暫存器

Page 71: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 71---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-5 移位暫存器與電子音樂歌曲的編輯製作

實驗電路圖

CLK

SP1

BUZZER

SW1

VCC

VCC

EPF10K10/LCC

47

46

55

I/O

I/O

CLK

Page 72: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 72---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-5 移位暫存器與電子音樂歌曲的編輯製作

程式與說明 除頻器設計 (略 ) 音調產生器 (略 ) 移位暫存器設計

12 entity ring is13 port(CLK: in std_logic;14 ringen : in std_logic;15 ring : out std_logic_vector(2 downto 0));16 end ring;1718 architecture arcpt_ring of ring is19 signal rings: std_logic_vector(2 downto 0) ;20 signal ringdata : std_logic_vector(104 downto 0);21 begin2223 musicp : process(clk,ringen)24 begin

25 if clk = '1' and clk'EVENT then26 if ringen = '1' then27 rings <= ringdata(104 downto 102);28 ringdata(2 downto 0) <= ringdata(104 downto 102);29 ringdata(104 downto 3) <= ringdata(101 downto 0);30 else31 ringdata <= "00101001110010100001100100010 11001000100001000000100010001 00011011001000011101001 011100011100011001010000";32 rings <= "000";33 end if;34 end if;35 end process musicp;36 ring<=rings;373839 end arcpt_ring;40

Page 73: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 73---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-5 移位暫存器與電子音樂歌曲的編輯製作

程式與說明 ( 續 ) 多工器電路

頻率產生器 十模計數器

頻率產生器

Page 74: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 74---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-5 移位暫存器與電子音樂歌曲的編輯製作

程式與說明 ( 續 ) 主程式電路連線 (elect_ring_main.gdf) 與腳位規劃

Page 75: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 75---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 5-5 移位暫存器與電子音樂歌曲的編輯製作

功能模擬與CPLD下載驗證

輸入 腳位 輸出 腳位Clk_in55 55 Bee_out46 46 (連接蜂鳴器)SW_in47 47 ( SW1 )    

Page 76: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 76---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6 :多工掃描式七段顯示器實習

Page 77: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 77---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 相關知識

本實驗我們將設計一組兩位數的 BCD 計數器,並將計數動作以多工掃描的方式顯示在一個二合一的七段顯示器元件上。

使用多工掃描顯示時,我們一次只讓一個七段顯示器的共同點得到驅動電壓而點亮,也就是說同時間內只有一個七段顯示器可以顯示;但當掃描速度夠快時,由於人類視覺暫留的原理,我們肉眼所看到的現象卻是所有的七段顯示器都穩定而且非閃爍的顯示(每一個位數的七段顯示器之輸入掃描頻率至少要大於人眼的視覺暫留頻率 24Hz )。

ea b c d f g

C2 C1

Page 78: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 78---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計

相關知識 ( 續 ) 多工掃描電路動作示意圖

C2 C1

M0

M1

0

1

2 to 1Mux

1 to 2decoder

Sel=’0’

7

7

7IN1

IN2

High

Low

C2 C1

M0

M1

0

1

2 to 1Mux

1 to 2decoder

Sel=’1’

7

7

7IN1

IN2

High

Low

Page 79: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 79---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 相關知識 ( 續 )

多工掃描式兩位數 BCD 計數器電路架構圖

C2 C1

M0

M1

0

1

2 to 1Mux

1 to 2decoder

4

4

4七段顯示器解

碼電路

7

兩位數BCD 計數器

clk

1Hz

64Hz

頻率產生器sel

sel

Page 80: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 80---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 相關知識 ( 續 )

本實驗中所設計之兩位數 BCD 計數器電路方塊連線關係圖

Clk_1Hz

Clk_in

Up_counter4_10

clk

4bin

4 to 8decoder

Bin2led

Clk_div_1_64

Clk_64Hz

4

4

7

Bin2seg0_scan

clk

q_one

q_ten

Seg-out

Seg-one-en

Seg-ten-en

Seg-en

ceRst-n

ceRst_nSeg_en

Clk_out1

Clk_out2

clk

Led(7)

Led(6)Led(5)Led(4)Led(3)Led(2)Led(1)

Led(0)

Led_en Led_en

q_one

q_ten

Page 81: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 81---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計

實驗電路圖

b

bafgCOM1

COM2cedotd

7SEGx2

VCC

LED0

Led_en

a

LED5

e

SW1

Seg_ten_en

LED3

LED1

ce

Seg_out0

SW2SW3

U7

EPM7064S/LCC44

45689

111214

1617181920212425

2627

3739

4140

43

I/OI/OI/OI/OI/OI/OI/OI/O

I/OI/OI/OI/OI/OI/OI/OI/O

I/OI/O

I/OI/O

I/OI/O

I/GCLK1

d

Seg_out6

Seg_en

CLK

fSeg_out5LED2

Seg_one_en

g

C2

VCC

Seg_out1

Seg_out3

C1

Seg_out4

SW4

Seg_out2

Rst_n

LED4

LED7

c

dot

LED6

Page 82: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 82---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 程式與說明

產生計數器計數時脈與七段顯示器掃描頻率之除頻電路程式碼

11 ----------------------------------------------12 entity clk_div_1_64 is13 port( 14 clk_in : in std_logic;15 clk_out1: out std_logic;16 clk_out2: out std_logic17 );18 end clk_div_1_64;19 20 architecture a of clk_div_1_64 is2122 ------SIGNAL DECLARED-------------• signal cnt : std_logic_vector(20 downto 0); --** counter24 signal reset: std_logic; --reset25 begin26

27 ------PROGRAM BODY--------------------28 process (clk_in)29 begin30 if reset='1' then31 cnt<="000000000000000000000";32 elsif clk_in'event and clk_in='1' then33 cnt<=cnt+1;34 end if;35 end process;36 • reset<='1' when cnt=1843200 else '0'; --** divisor 184320038 clk_out1<=cnt(20); 39 clk_out2<=cnt(14); 4041 end a;

Page 83: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 83---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 程式與說明

BCD 上數計數器程式碼

12 entity up_counter4_10 is13 port(14 clk : in std_logic; --system clock15 rst_n : in std_logic; --reset16 ce : in std_logic; --chip enable17 q_one : out std_logic_vector(3 downto 0); --counter one output18 q_ten : out std_logic_vector(3 downto 0) --counter ten output19 );20 end up_counter4_10;21 22 architecture a of up_counter4_10 is23 24 ---------SIGNAL DECLARED------------------------25 signal q_one_temp : std_logic_vector(3 downto 0); --temp q_one26 signal q_ten_temp : std_logic_vector(3 downto 0); --temp q_ten27 begin28

Page 84: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 84---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 程式與說明

BCD 上數計數器程式碼 ( 續 )

29-----------PROGRAM BODY--------------------------30 process (clk)31 begin32 if (ce='0' or rst_n='0') then33 q_one_temp<="0000";34 q_ten_temp<="0000";35 elsif clk'event and clk='1' then36 if (q_one_temp>=9) then --carry in (one)37 q_one_temp<="0000";38 if (q_ten_temp>=9) then --carry in (ten)39 q_ten_temp<="0000";40 else41 q_ten_temp<=q_ten_temp+1;42 end if;43 else44 q_one_temp<=q_one_temp+1;45 end if;46 end if;47 end process;

48

49 q_one<=q_one_temp;

50 q_ten<=q_ten_temp;

51

52 end a;

Page 85: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 85---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 程式與說明

多工掃描顯示之七段顯示器解碼電路設計程式碼 23 entity bin2seg0_scan is24 port (25 clk : in std_logic; --64hz clock26 seg_en: in std_logic; --segment enable27 q_one: in std_logic_vector(3 downto 0); --binary input of one28 q_ten: in std_logic_vector(3 downto 0); --binary input of ten29 seg_out : out std_logic_vector(6 downto 0); --7segment output30 seg_one_en: out std_logic; --7segment one enable31 seg_ten_en: out std_logic --7segment ten enable32 );33 end bin2seg0_scan;34 35 architecture arch of bin2seg0_scan is3637-------SIGNAL DECLARED-------------------------------------------38 signal bin: std_logic_vector(3 downto 0); --binary code39 signal seg: std_logic_vector(6 downto 0); --segment code40 signal sel: integer range 0 to 1; --scan coutnter41 begin

Page 86: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 86---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 程式與說明

多工掃描顯示之七段顯示器解碼電路設計程式碼 ( 續 )

4243 ---------PROGRAM BODY--------------44 ----------scan and signal assign---45 process (clk, seg_en)46 begin47 if clk'event and clk='1' then48 if (seg_en='0') then49 seg_one_en<='0';50 seg_ten_en<='0';51 sel<=0;52 else

53 sel<=sel+1;54 case sel is55 when 0 =>56 bin<=q_one;57 seg_out<=seg;58 seg_one_en<='0';59 seg_ten_en<='1';

60 when 1 =>61 bin<=q_ten;62 seg_out<=seg;63 seg_one_en<='1';64 seg_ten_en<='0';65 when others =>66 null;67 end case;68 end if;69 end if;70 end process;71

Page 87: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 87---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 程式與說明

多工掃描顯示之七段顯示器解碼電路設計程式碼 ( 續 )

72 ------binary to seven segment decoder-----73 process (bin)74 begin75 case bin is75 when "0000" => seg <= "1000000"; -- 0 active low '0'77 when "0001" => seg <= "1111001"; -- 178 when "0010" => seg <= "0100100"; -- 279 when "0011" => seg <= "0110000"; -- 380 when "0100" => seg <= "0011001"; -- 481 when "0101" => seg <= "0010010"; -- 582 when "0110" => seg <= "0000010"; -- 683 when "0111" => seg <= "1111000"; -- 7

84 when "1000" => seg <= "0000000"; -- 885 when "1001" => seg <= "0010000"; -- 986 when others => seg <= "1111111"; 87 end case;88 end process;89 90 end arch;

Page 88: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 88---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 程式與說明

低電位動作之四對八解碼電路程式碼 (略 ) 掃描式多工顯示之兩位數 BCD 計數器主程式碼

11 entity up_scan_top is12 port (13 clk : in std_logic; -- 1.8432 MHz14 rst_n : in std_logic;15 ce : in std_logic;16 led_en : in std_logic;17 seg_en : in std_logic;18 led : out std_logic_vector(7 downto 0);19 seg_out : out std_logic_vector(6 downto 0);20 seg_one_en: out std_logic;21 seg_ten_en: out std_logic22 );23 end up_scan_top;24 25 architecture arch of up_scan_top is26

27 -------COMPONENT DECLARED---------------------28 ---------clk_div 1hz 64hz component----------29 component clk_div_1_6430 port( 31 clk_in : in std_logic;32 clk_out1: out std_logic;33 clk_out2: out std_logic34 );35 end component;36 37 ---------up_counter_4_10 component----------38 component up_counter4_1039 port(40 clk : in std_logic; --system clock41 rst_n : in std_logic; --reset42 ce : in std_logic; --chip enable46 end

component;

Page 89: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 89---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 程式與說明

掃描式多工顯示之兩位數 BCD 計數器主程式碼 ( 續 )

43 q_one : out std_logic_vector(3 downto 0); --counter one output44 q_ten : out std_logic_vector(3 downto 0) --counter ten output45 );46 end component;47 48 ---------binary to 7segment decoder---------49 component bin2seg0_scan50 port (51 clk : in std_logic; --64hz clock52 seg_en : in std_logic; --segment enable53 q_one : in std_logic_vector(3 downto 0); 54 q_ten : in std_logic_vector(3 downto 0);55 seg_out : out std_logic_vector(6 downto 0);56 seg_one_en: out std_logic;57 seg_ten_en: out std_logic58 );59 end component;60

Page 90: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 90---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 程式與說明

掃描式多工顯示之兩位數 BCD 計數器主程式碼 ( 續 )61 ---------binary to led8 decoder---------62 component bin2led0_1063 port (64 led_en : in std_logic;65 bin : in std_logic_vector (3 downto 0);

led : out std_logic_vector (7 downto 0)67 );68 end component;6970 ---------SIGNAL DECLARED----------------------71 signal clk_1hz : std_logic;72 signal clk_64hz: std_logic;73 signal q_one: std_logic_vector(3 downto

0);74 signal q_ten: std_logic_vector(3 downto

0);75 76 begin77

78 ----------Frequency divider----------79 u1: clk_div_1_64 port map (clk, clk_1hz, clk_64hz);80 81 ----------4 bit up counter----------82 u2: up_counter4_10 port map (clk_1hz, rst_n, ce, q_one, q_ten);8384 ----------binary to LED decoder----------85 u3: bin2led0_10 port map (led_en, q_one, led);86 87 ----------binary to seven segment decoder----------• u4: bin2seg0_scan port map (clk_64hz, seg_en, q_one, q_ten, seg_out,89 seg_one_en, seg_ten_en);90 • end arch;

Page 91: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 91---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 程式與說明

掃描式多工顯示之兩位數 BCD 計數器主程式碼 電路結構 ( 續 )

Page 92: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 92---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-1 二位數 BCD 計數器設計 功能模擬與CPLD下載驗證

輸出 腳位Seg_out(0) 16 ( a )

Seg_out(1) 17 ( b )

Seg_out(2) 18 ( c )

Seg_out(3) 19 ( d )

Seg_out(4) 20 ( e )

Seg_out(5) 21 ( f )

Seg_out(6) 24 ( g )

Seg_one_en 27(7SEGIO2)

Seg_ten_en 26(7SEGIO1)

Led(7) 4 ( LED1 )

Led(6) 5 ( LED2 )

Led(5) 6 ( LED3 )

Led(4) 8 ( LED4 )

Led(3) 9 ( LED5 )

Led(2) 11 ( LED6 )

Led(1) 12 ( LED7 )

輸入clk

Rst_n

ce

Seg_en

Led_en

 

 

 

 

 

 

 

 

 

 

 

 

腳位43

37(SW1)

39(SW2)

40(SW3)

41(SW4)

 

 

 

 

 

 

 

 

 

 

 

  Led(0) 14 ( LED8 )

Rst_n

ce

Seg_en

Led_en

Led7~Led0

7 Segment

Page 93: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 93---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-2 四位數 BCD 計數器設計與頻率控制 相關知識 本實驗我們將設計一組四位數的 BCD 計數器,並將計數動作以多工掃描的方式顯示在力浦

電子 LP-2900 實驗板上的六合一七段顯示器元件(高電位驅動)上。

ea b c d f g

DE3 DE1DE2

為了節省 CPLD I/O 腳位的使用, LP-2900 實驗板上已經用一顆 74138 解碼器連接六個七段顯示器的共同點 C1~C6 ,因此我們僅需以 CPLD 來控制74138 解碼器的 DE3 、 DE2 和 DE1三個輸入端即能選擇所欲點亮的七段顯示器。

Page 94: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 94---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-2 四位數 BCD 計數器設計與頻率控制 實驗功能

當 Enable=’0’ 時,計數器不動作; Enable=’1’ 開始執行計數動作。 Enable鍵連接至實驗板上的 SW6這個開關。

當 clear=’1’ 時,計數器被重置為 0 。 Clear鍵連接至實驗板上的 SW5這個開關。

SW1 和 SW2這兩個開關決定掃描頻率。 SW3 和 SW4這兩個開關決定計數頻率。

Page 95: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 95---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-2 四位數 BCD 計數器設計與頻率控制 實驗電路圖

DE3

Y1

Y4

Y0

DE1

CLK_IN

DE2

Y3

SW6

bafg

DE1DE2cedotd DE3

7SEGx6

SW1SW5

EPF10K10TC144-4

23

474849

60

51

2627282930

33363759

31

55

I/O

I/OI/OI/O

I/O

I/O

I/OI/OI/OI/OI/O

I/OI/OI/OI/O

I/O

CLK

SW4

Clear

SW3

counter_clk_sel0counter_clk_sel1

fe

aY6bY5c

Scan_freq_sel0

SW2

Enable

gScan_freq_sel1

Y2

VCC

d

Page 96: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 96---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-2 四位數 BCD 計數器設計與頻率控制 程式與說明

十模計數器 (略 ) 頻率產生器

Page 97: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 97---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-2 四位數 BCD 計數器設計與頻率控制 四位數 BCD 上數計數器程式碼

11 entity BCD4_counter is1213 port(clk,clr,en : in std_logic;14 bcd3,bcd2,bcd1,bcd0:buffer std_logic_vector(3 downto 0));15 end BCD4_counter;1617 architecture a of BCD4_counter is18 begin19 process (clk)20 begin21 if clk'event and clk = '1' then22 if clr = '1' then23 bcd3<= "0000" ;bcd2<="0000"; bcd1<="0000"; bcd0<="0000";

24 elsif en = '1'then25 if bcd0="1001"then26 bcd0<="0000";27 if bcd1="1001"then28 bcd1<="0000";29 if bcd2="1001"then30 bcd2<="0000";31 if bcd3="1001"then32 bcd3<="0000";33 else34 bcd3<= bcd3+'1';35 end if;36 else37 bcd2<= bcd2+'1';38 end if;39 else40 bcd1<= bcd1+'1';41 end if;42 else43 bcd0<= bcd0+'1';44 end if;45 end if;46 end if;47 end process;48 end a;

Page 98: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 98---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-2 四位數 BCD 計數器設計與頻率控制 多工掃描顯示之七段顯示器解碼電路設計程式碼

10 entity sevseg_lp2900 is11 port( x:in std_logic_vector(3 downto 0);12 y:out std_logic_vector(6 downto 0));13 end sevseg_lp2900;1415 architecture a of sevseg_lp2900 is16 begin17 with x select18 y <= "1111110" when "0000",19 "0110000" when "0001",20 "1101101" when "0010",21 "1111001" when "0011",22 "0110011" when "0100",23 "1011011" when "0101",

24 "1011111" when "0110",25 "1110000" when "0111",26 "1111111" when "1000",27 "1111011" when "1001",28 "1110111" when "1010",29 "0011111" when "1011",30 "1001110" when "1100",31 "0111101" when "1101",32 "1001111" when "1110",33 "1000111" when "1111",34 "0000000" when others;35 end a;

Page 99: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 99---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-2 四位數 BCD 計數器設計與頻率控制 四對一多工器 (略 )

四位元寬位之四對一多工器 (略 )

二位元計數器 (略 )

Page 100: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 100---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-2 四位數 BCD 計數器設計與頻率控制 主程式碼的腳位連結結構圖

Page 101: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 101---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-2 四位數 BCD 計數器設計與頻率控制 功能模擬與CPLD下載驗證 (LP-2900實驗板 )

輸入 腳位 輸出 腳位

Clk_in 55 Y(6) 23(a)

Clear 59(SW5) Y(5) 26(b)

Enable 60(SW6) Y(4) 27(c)

Scan_freq_sel(1) 47(SW1) Y(3) 28(d)

Scan_freq_sel(0) 48(SW2) Y(2) 29(e)

counter_clk_sel(1) 49(SW3) Y(1) 30(f)

counter_clk_sel(0) 51(SW4) Y(0) 31(g)

    DE3 37

    DE2 36

    DE1 33

Page 102: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 102---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

單元 6-2 四位數 BCD 計數器設計與頻率控制 功能模擬與 CPLD 下載驗證 (LP-2900 實驗板 )( 續 )

Page 103: 序向邏輯電路與狀態機設計

VHDL數位電路實習與專題設計 103---陳慶逸、林柏辰編著 文魁資訊

第三章 序向邏輯電路與狀態機設計

勘誤 3-8 圖 3-4裡的文字 Counter value 變成

Counter ue 3-18 46列 IF沒有粗體