Está en la página 1de 14

n tp PLD & ASIC

Bi 1: iu khin 8 LED sang dn v tt ht t tri sang phi entity BAI2_TR75 is Port ( CLR : in STD_LOGIC; CLK : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (07 downto 0)); end BAI2_TR75; architecture Behavioral of BAI2_TR75 is begin PROCESS (CLK,CLR) VARIABLE VARIABLE QT: STD_LOGIC_VECTOR (7 DOWNTO 0); DEM: INTEGER RANGE 0 TO 10;

end BAI3_TR75_C2; architecture Behavioral of BAI3_TR75_C2 is SIGNAL begin PROCESS(CLK,CLR,S) VARIABLE QT: STD_LOGIC_VECTOR(7 DOWNTO 0); BEGIN IF CLR='1' THEN QT:= X"00"; ELSIF CLK='1' AND CLK'EVENT THEN IF S='0' THEN D<= NOT QT(7); QT:=QT(6 DOWNTO 0)& D; ELSE D<=NOT QT(0); D: STD_LOGIC;

BEGIN IF CLR='1' THEN QT:= X"00";DEM:=0; ELSIF CLK='1' AND CLK'EVENT THEN QT:='1'& QT(7 DOWNTO 1); DEM:=DEM+1; IF DEM=9 THEN QT:=X"00";DEM:=0; END IF; END IF; Q<= QT; END PROCESS; end Behavioral; Bi 2: iu khin 8 LED sang dn v tt ht t phi sang tri, chn S la chn chiu dch entity BAI3_TR75_C2 is Port ( S : in STD_LOGIC; CLK : in STD_LOGIC; CLR : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (07 downto 0));

QT:= D& QT(7 DOWNTO 1); END IF; END IF; Q<=QT; END PROCESS; end Behavioral; Bi 3: 8 led sng dn ln v tt ht phi sang tri v ngc li entity BAI4_TRANG75 is Port ( CLK : in STD_LOGIC; CLR : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (07 downto 0)); end BAI4_TRANG75; architecture Behavioral of BAI4_TRANG75 is begin PROCESS(CLK,CLR) VARIABLE QT:STD_LOGIC_VECTOR (7 DOWNTO 0);

n tp PLD & ASIC

VARIABLE BEGIN

DEM: INTEGER RANGE 0 TO 32;

ELSIF CLK='1' AND CLK'EVENT THEN IF TAM='0' THEN

IF CLR='1' THEN QT:=X"00"; DEM:=0; ELSIF CLK='1' AND CLK'EVENT THEN IF DEM<16 THEN

QT:= QT(6 DOWNTO 0)& QT(7); IF QT(7)='1' THEN -- TAM= 1 TAM:='1'; END IF; ELSE

QT:= QT(0) & QT(7 DOWNTO 1); QT:= QT(6 DOWNTO 0) & NOT QT(7); IF QT(0)='1' THEN ELSE QT:=NOT QT(0)& QT(7 DOWNTO 1); END IF; DEM:=DEM+1; END IF; IF DEM=32 THEN DEM:=0; END IF; Q<=QT; END PROCESS; end Behavioral; Bi 4:1 im sng chy t tri sang phi sau do chy t phi sang tri IF DEM<8 entity BAI7_TRANG75 is Port ( CLK : in STD_LOGIC; P : in STD_LOGIC; RE : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (07 downto 0)); end BAI7_TRANG75; architecture Behavioral of BAI7_TRANG75 is begin Bi 5: iu khin 16 led n nh sau: PROCESS(CLK,RE,P) 1 im sng chy t phi sang tri sau chy t tri sang phi VARIABLE QT: STD_LOGIC_VECTOR(7 DOWNTO 0); Mch sng dn ln v tt dn t tri sang phi v ngc li VARIABLE TAM: STD_LOGIC; Sng dn t ngoi vo trong v t trong ra ngoi BEGIN IF P='0' THEN --nt pause: nu P=1 th khng lm g IF RE='0' THEN QT:="00000001";TAM:='0'; entity BAI8_TRANG76 is Port ( CLK : in STD_LOGIC; THEN IF; END IF; -- IF nt reset TAM:='0'; END IF; END

END IF; -- IF nt pause Q<=QT; END PROCESS; end Behavioral; Note:Ban u gn cho QT:="00000001"; dng bin TAM: TAM =0 th dch tri, sau khi dch xong cho TAM=1. C th lm theo cch m s xung. T xung 1 8:dch tri, t 8 15 dch phi, khi bng 14 th cho bin m =0.

QT:= QT(6 DOWNTO 0) & QT(7); DEM:= DEM + 1; ELSIF DEM >=8 AND DEM <15 THEN QT:= QT(0) & QT(6 DOWNTO 0); DEM:= DEM + 1; END IF; IF DEM=15 THEN DEM:= 0; END IF;

n tp PLD & ASIC

RE : in STD_LOGIC; P : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (015 downto 0)); end BAI8_TRANG76; architecture Behavioral of BAI8_TRANG76 is BEGIN PROCESS(CLK,RE,P) VARIABLE VARIABLE VARIABLE BEGIN IF P='0' THEN IF RE='0' THEN QT:=X"0001";TAM:='0';DEM:=0;DEM2:=0; ELSIF CLK='1' AND CLK'EVENT THEN IF DEM2<32 THEN mt led chy IF TAM='0' THEN QT: STD_LOGIC_VECTOR(15 DOWNTO 0); TAM: STD_LOGIC; DEM, DEM2: INTEGER RANGE 0 TO 100;

DEM:=DEM+1;

-- sng tt dn tri sang phi

ELSIF DEM>=64 AND DEM<80 THEN ngoi vo QT(15 DOWNTO 8):= QT(14 DOWNTO 8) & NOT QT(15); QT(7 DOWNTO 0):= NOT QT(0)& QT(7 DOWNTO 1); DEM:=DEM+1; ELSIF DEM>=80 AND DEM<96 THEN trong ra QT(15 DOWNTO 8):=NOT QT(8)& QT(15 DOWNTO 9) ; QT(7 DOWNTO 0):= QT(6 DOWNTO 0)& NOT QT(7); DEM:=DEM+1; END IF; IF DEM=96 THEN DEM:=0; DEM2:=0;QT:=X"0001"; END IF; END IF; END IF; END IF; Q<=QT; END PROCESS; end Behavioral; Bi 6:iu khin 16 LED sng tt dn tri phi v ngc li. Dng phng php m s xung. entity BAI4_TRANG75_16LED is Port ( CLK : in STD_LOGIC; TAM:='0';QT:=X"0000" CLR : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (015 downto 0)); end BAI4_TRANG75_16LED; architecture Behavioral of BAI4_TRANG75_16LED is begin PROCESS(CLK,CLR) VARIABLE VARIABLE BEGIN QT:STD_LOGIC_VECTOR (15 DOWNTO 0); DEM: INTEGER RANGE 0 TO 64; --IF ca bin DEM2 --IF ca bin RE --IF ca bin P

QT:= QT(14 DOWNTO 0)& QT(15); DEM2:=DEM2+1; IF QT(15)='1' THEN TAM:='1'; END IF; ELSE QT:= QT(0) & QT(15 DOWNTO 1); DEM2:=DEM2+1; IF QT(0)='1' THEN END IF; END IF; ELSE -- sng tt dn phi sang tri

IF DEM<32 THEN QT:= QT(14 DOWNTO 0) & NOT QT(15); DEM:=DEM+1; ELSIF DEM>=32 AND DEM<64 THEN QT:= NOT QT(0)& QT(15 DOWNTO 1);

n tp PLD & ASIC

IF CLR='1' THEN QT:=X"0000"; DEM:=0; ELSIF CLK='1' AND CLK'EVENT THEN IF DEM<32 THEN sng dn phi tri QT:= QT(14 DOWNTO 0) & NOT QT(15); DEM:=DEM+1; ELSE sng dn tri phi

IF DEM<16 THEN QT1:= QT1(6 DOWNTO 0) & NOT QT1(7); DEM:=DEM+1; ELSIF DEM>16 AND DEM<32 THEN QT2:= QT2(6 DOWNTO 0) & NOT QT2(7); DEM:=DEM+1; ELSIF DEM>32 AND DEM<64 THEN QT1:= NOT QT1(0)& QT1(7 DOWNTO 1) ; DEM:=DEM+1; ELSE QT2:= NOT QT2(0)& QT2(7 DOWNTO 1) ; DEM:=DEM+1; END IF; IF DEM=64 THEN DEM:=0; END IF; END IF; Q<=QT2&QT1;

QT:= NOT QT(0)& QT(15 DOWNTO 1); DEM:=DEM+1; END IF; IF DEM=64 THEN DEM:=0; END IF; END IF; Q<=QT; END PROCESS; end Behavioral; Bi 7: iu khin 30led. entity BAI4_TRANG75_30LED Port ( CLK : in STD_LOGIC; CLR : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (15 downto 0)); end BAI4_TRANG75_30LED; architecture Behavioral of BAI4_TRANG75_30LED is BEGIN PROCESS(CLK,CLR) VARIABLE VARIABLE VARIABLE BEGIN IF CLR='1' THEN QT1:=X"00"; DEM:=0;QT2:=X"00"; ELSIF CLK='1' AND CLK'EVENT THEN QT1:STD_LOGIC_VECTOR (7 DOWNTO 0); QT2:STD_LOGIC_VECTOR (7 DOWNTO 0); DEM: INTEGER RANGE 0 TO 64; is

END PROCESS; end Behavioral; Bi 8:iu khin 16 LED sng tt dn tri phi v ngc li. Dng nt nhn. Nhn ln 1: sng tt dn tri sang phi. Nhn ln 2 :sng tt dn t phi sang tri. entity nut_nhan is

Port (BUTTON: in STD_LOGIC; CLK : in STD_LOGIC; CLR : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (15 downto 0)); end nut_nhan; architecture Behavioral of nut_nhan begin PROCESS(BUTTON,CLK,CLR) is

n tp PLD & ASIC

VARIABLE VARIABLE BEGIN

QT:STD_LOGIC_VECTOR (15 DOWNTO 0); TAM: INTEGER RANGE 0 TO 10;

E: IN STD_LOGIC; Q: IN STD_LOGIC_VECTOR(3 DOWNTO 0)); END COMPONENT;

IF BUTTON=1 AND BUTTONEVENT TAM:=TAM + 1; IF TAM=2 THEN TAM:=0; END IF; END IF;

THEN

SIGNAL BEGIN U5:

S0,S1,S2,S3:

IN STD_LOGIC;

GM2_4 PORTMAP(I(0)=>A(2),I(1)=>A(3),E=>EN, Q(0) =>S0, Q(1) =>S1, Q(2) =>S2, Q(3) =>S3); GM2_4 PORTMAP(I(0)=>A(0),I(1)=>A(1),E=>S0, Q(0) =>O(0), Q(1) =>O(1), Q(2) =>O(2), Q(3) =>O(3)); GM2_4 PORTMAP(I(0)=>A(0),I(1)=>A(1),E=>S1, Q(0) =>O(4), Q(1) =>O(5), Q(2) =>O(6), Q(3) =>O(7)); GM2_4 PORTMAP(I(0)=>A(0),I(1)=>A(1),E=>S2, Q(0) =>O(8), Q(1) =>O(9), Q(2) =>O(10), Q(3) =>O(11)); GM2_4 PORTMAP(I(0)=>A(0),I(1)=>A(1),E=>S3, Q(0) =>O(12), Q(1) =>O(13), Q(2) =>O(14), Q(3) =>O(5));

U4: IF CLR='1' THEN QT:=X"0000"; ELSIF CLK='1' AND CLK'EVENT THEN IF TAM=0 THEN sng dn phi tri QT:= QT(14 DOWNTO 0) & NOT QT(15); DEM:=DEM+1; ELSE sng dn tri phi U4: U4: U3:

QT:= NOT QT(0)& QT(15 DOWNTO 1); DEM:=DEM+1; END IF; END IF; Q<=QT; END PROCESS; end Behavioral; Bi 9: thit k mch gii m 4-16 t 4 mch gii m 2-4. Entity GM4_16 IS

END BEHAVIORAL; Bi 10: vit chng trnh iu khin 20 led nh sau:sng tt dn t phi sang tri v ngc li. sng tt dn t trong ra ngoi v ngc li.chn E (mc cao) chn P (mc cao). ENTITY DK_LED IS

PORT ( CLR,CLK,E,P: IN STD_LOGIC; Q: OUT END DK_LED; ARCHITECTURE BEHAVIORAL OF DK_LED IS BEGIN PROCESS (CLR,CLK,E,P) VARIABLE: VARIABLE: BEGIN IF P=1 THEN IF CLR=0 THEN QT:=(OTHERS=> 0);DEM:=0; DEM INTEGER RANGE 0 TO 200; QT STD_LOGIC_VECTOR(19 DOWNTO 0); STD_LOGIC_VECTOR(19 DOWNTO 0));

PORT ( A: IN STD_LOGIC_VECTOR(3 DOWNTO 0); EN: IN STD_LOGIC; O: OUT STD_LOGIC_VECTOR(15 DOWNTO 0)); END GM4_16; ARCHITECTURE BEHAVIORAL OF GM4_16 IS COMPONENT GM2_4 PORT ( I: IN STD_LOGIC_VECTOR(1 DOWNTO 0);

n tp PLD & ASIC

ELSIF CLK=1

AND

CLKEVENT THEN

IF P=1 THEN IF CLR=0 THEN QT1:=X0000000001; QT2:=X1000000000; AND CLKEVENT THEN

IF DEM< 40 THEN QT:= QT(18 DOWNTO 0) & QT(19);

ELSIF CLK=1 ELSIF DEM>=40 AND DEM<80 THEN QT:= QT(0) & QT(19 DOWNTO 0);

QT1:= QT(8 DOWNTO 0) & QT(9); QT2:= QT(0) & QT(8 DOWNTO 0); ELSIF DEM>=80 AND DEM<100 THEN END IF; QT(9 DOWNTO 0):= QT(0)&QT(8 DOWNTO 0); END IF; QT(19DOWNTO10):=QT(18DOWNTO10)&QT(19); Q<=QT2 & QT1; ELSIF DEM>=100 AND DEM<120 THEN END PROCESS; QT(9 DOWNTO 0):= QT(8 DOWNTO 0)&QT(9); END BEHAVIORAL; QT(19DOWNTO10):=QT(19)&QT(18DOWNTO10); END IF; DEM:=DEM+1; IF DEM=120 THEN DEM:=0; END IF; END IF; END IF; Q<=QT; END PROCESS; END BEHAVIORAL; Bi 11: iu khin 2 led chy t 2 pha vo va xuyn qua nhau ENTITY DK_LED IS entity DEMTONGHOP is Port ( CLK,CLR,E: in STD_LOGIC; S: IN STD_LOGIC_VECTOR(1 DOWTO 0); Bi 12: m t mch m c cc chc nng sau: m ln nh phn 4bit, m xung nh phn 4 bit, BCD m ln, BCD m xung

Q : out STD_LOGIC_VECTOR (03 downto 0)); end DEMTONGHOP; architecture Behavioral of DEMTONGHOP is begin PROCESS(E,S,CLK,CLR) VARIABLE BEGIN IF CLR='0' THEN QT:="0000"; ELSIF E=1 THEN IF CLK='1' AND CLK'EVENT THEN CASE S IS QT:STD_LOGIC_VECTOR( 3 DOWNTO 0);

PORT ( CLR,CLK,E,P: IN STD_LOGIC; Q: OUT END DK_LED; ARCHITECTURE BEHAVIORAL OF DK_LED IS BEGIN PROCESS (CLR,CLK,E,P) VARIABLE: QT1,QT2 STD_LOGIC_VECTOR(9DOWNTO 0); BEGIN STD_LOGIC_VECTOR(19 DOWNTO 0));

WHEN 00 => QT:=QT+1; WHEN 01 => QT:=QT-1; WHEN 10 => IF QT=1010THEN QT:=0000;

n tp PLD & ASIC

ELSE QT:=QT+1; END IF; WHEN 11 => IF QT=1010THEN QT:=1001; ELSE QT:=QT-1; END IF; WHEN OTHERS => NULL; END CASE; END IF; END IF; Q<=QT; END PROCESS; end Behavioral; Bi 13: thit k chng trnh m ln xung 4bit, bit UD iu khin m (0:up, 1:down),chn CLK,CLR entity DEMUP_DOWN_4BIT is

END PROCESS; end Behavioral; Bi 14: m BCD_gii m led entity BCD_LED7SEG is Port ( CLK : in STD_LOGIC; CLR : in STD_LOGIC; P : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (07 downto 0)); end BCD_LED7SEG; architecture Behavioral of BCD_LED7SEG is begin PROCESS(CLK,CLR,P) VARIABLE QT: STD_LOGIC_VECTOR(3 DOWNTO 0);

Port ( CLK,CLR,UD : in STD_LOGIC; BEGIN Q : out STD_LOGIC_VECTOR (03 downto 0)); IF CLR='1' THEN QT:="0000"; end DEMUP_DOWN_4BIT; ELSIF CLK='1' AND CLK'EVENT THEN architecture Behavioral of DEMUP_DOWN_4BIT is IF P='1' THEN QT:=QT+1; begin IF QT="1010" THEN QT:="0000"; END IF; PROCESS(UD,CLK,CLR) END IF; VARIABLE BEGIN CASE QT IS IF CLR='1' THEN QT:="0000"; WHEN "0000" => Q<= X"C0"; ELSIF CLK='1' AND CLK'EVENT THEN WHEN "0001" => Q<= X"F9"; IF UD='0' THEN QT:=QT+1; WHEN "0010" => Q<= X"A4"; IF QT=1111THEN QT:=0000; END IF; WHEN "0011" => Q<= X"B0"; ELSE QT:= QT-1; WHEN "0100" => Q<= X"99"; IF QT=0000THEN QT:=1111; END IF; WHEN "0101" => Q<= X"92"; END IF; WHEN "0110" => Q<= X"82"; END IF; WHEN "0111" => Q<= X"F8"; Q<=QT; QT:STD_LOGIC_VECTOR( 3 DOWNTO 0); END IF;

n tp PLD & ASIC

WHEN "1000" => Q<= X"80"; WHEN "1001" => Q<= X"90"; WHEN OTHERS => Q<= X"FF"; END CASE; END PROCESS; end Behavioral; Note: vi cc bi m gii m led th tng t cc bi m nhng thm phn gii m 7on cho bin QT,QCH,QDVdng hm Case gii m. Tuy nhin nn dng Function hay Package gii m gip chng trnh s gn gng hn. Bi 15: m 00_123, dng Function gii m entity DEM00_123 is Port ( CLR : in STD_LOGIC; CLK : in STD_LOGIC; P : in STD_LOGIC; QTR,QCH,QDV:out STD_LOGIC_VECTOR (07 downto 0)); end DEM00_123; architecture Behavioral of DEM00_123 is FUNCTION GIAIMA (BCD:IN INTEGER) RETURN STD_LOGIC_VECTORIS VARIABLE SEG: STD_LOGIC_VECTOR(7 DOWNTO 0); BEGIN CASE BCD IS gii m dng Function

WHEN 8 => SEG:= X"80"; WHEN 9 => SEG:= X"90"; WHEN OTHERS => SEG:= X"FF"; END CASE; RETURN(SEG); END GIAIMA; begin PROCESS(CLK,CLR,P) VARIABLE BEGIN IF CLR='0' THEN TR:=0;CH:=0;DV:=0; ELSIF CLK='1'AND IF P='1' CLK'EVENT THEN TR,CH,DV: INTEGER RANGE 0 TO 10;

THEN DV:=DV+1;

IF DV=10 THEN DV:=0;CH:=CH+1; IF CH=10THEN CH:=0;TR:=TR+1;END IF; END IF; END IF; END IF; IF TR=1 AND CH=2 AND DV=4 THEN TR:=0; CH:=0; DV:=0; END IF; QTR<=GIAIMA(TR); QCH<=GIAIMA(CH); QDV<=GIAIMA(DV); END PROCESS; end Behavioral;

WHEN 0 => SEG:= X"C0"; WHEN 1 => SEG:= X"F9"; WHEN 2 => SEG:= X"A4"; WHEN 3 => SEG:= X"B0"; WHEN 4 => SEG:= X"99"; WHEN 5 => SEG:= X"92"; WHEN 6 => SEG:= X"82"; WHEN 7 => SEG:= X"F8";

T bi ny v sau dng function (gii m anod chung) gii m, s dng chng trnh in nghing trn

n tp PLD & ASIC

Bi 16:m hin th trn 3 led 7on, khi reset 3led hin 000, c xung led1 m 0 9, sau ln lt led2,3. m ti 999 th v 000. entity DEM3LED_999 is

Bi 17: Cho tn s ng vo f= 1KHz, vit chng trnh ng ra vi 5bit m ln nh phn vi f= 1Hz, 8bit m vng Johnson tn s 10Hz. ENTITY CHIA_XUNG IS IN STD_LOGIC;

Port ( CLR,CLK,P : in STD_LOGIC; PORT( CLR,CLK,E: QTR,QCH,QDV: out STD_LOGIC_VECTOR(07 downto 0)); Q: OUT end DEM3LED_999; P: OUT architecture Behavioral of DEM3LED_999 is END FUNCTION begin BEGIN PROCESS(CLK,CLR,P) PROCESS(CLK,CLR,E) VARIABLE BEGIN VARIABLE QT: IF CLR='0' THEN TR:=0;CH:=0;DV:=0;TAM:=0; VARIABLE PT: ELSIF CLK='1' IF P='1' AND CLK'EVENT THEN BEGIN THEN THEN DV:=DV+1; IF CLR=1 THEN F1:=0;F2:=0;QT:=00000;PT:=0000000; ELSIF E=1 THEN IF CLK=1 AND CLKEVENT THEN IF TAM=0 STD_LOGIC_VECTOR(7DOWNTO 0); STD_LOGIC_VECTOR(5DOWNTO 0); TAM,TR,CH,DV: INTEGER RANGE 0 TO 10; VARIABLE F1,F2:INTEGER RANGE 0 TO 1000; GIAIMA:S DNG CHNG TRNH TRN ARCHITECTURE BEHAVIORAL OF CHIA_XUNG IS CHIA_XUNG; STD_LOGIC_VECTOR(7 DOWNTO 0)); STD_LOGIC_VECTOR(4 DOWNTO 0);

IF DV=9 THEN TAM:=1;END IF; ELSIF TAM=1THEN CH:=CH+1; IF CH=9 THEN TAM:=2; END IF; ELSIF TAM=2THEN TR:=TR+1; IF TR=10 THEN TR:=0;CH:=0;DV:=0;TAM:=0;END IF; END IF; END IF; END IF; QTR<=GIAIMA(TR); QCH<=GIAIMA(CH); QDV<=GIAIMA(DV); END PROCESS; end Behavioral;

F1:=F1+1;F2:=F2+1; CHIA XUNG IF F1=1000 THEN F1:=0;QT:=QT+1;END IF; IF F2=100 THEN F2:=0;

PT:=PT(6 DOWNTO 0) & NOT(PT(7));END IF; END IF; END IF; P<=PT; Q<=QT; END PROCESS; END BEHAVIORAL;

n tp PLD & ASIC

BI TP M BNG CCH CHIA XUNG S DNG FUNCTION GII M HIN TH BNG PHNG PHP QUT
Bi 1:m 00-99 hin th trn 2led 7on,fvo= 10 KHz. Thng dng tn s qut l 1KHz entity dem00_99 is Port ( CLK,CLR : in STD_LOGIC; MA7 : out STD_LOGIC_VECTOR (07 downto 0); MAQ : out STD_LOGIC_VECTOR (03 downto 0)); end dem00_99; architecture Behavioral of dem00_99 is FUNCTION begin PROCESS(CLK,CLR) VARIABLEQTDV,QTCH:STD_LOGIC_VECTOR(3DOWNTO 0); VARIABLE FDEM,F: INTEGER RANGE 0 TO 100000; VARIABLE FQUET: STD_LOGIC_VECTOR(1 DOWNTO 0); BEGIN CHIA XUNG M V QUT GIAIMA(ON CHNG TRNH GIAIMA)

END IF; END IF; CASE FQUET IS QUT 2 LED

WHEN "00" => MA7 <= GIAIMA(QTDV); MAQ <="1110"; WHEN "01" => MA7 <= GIAIMA(QTCH); MAQ <="1101"; WHEN OTHERS => MA7<=GIAIMA(QTCH); MAQ<="1100; END CASE; END PROCESS; end Behavioral; Bi 2:m 00_25 ri quay ngc li 25-00 entity dem00_25 is Port ( CLK,CLR : in STD_LOGIC; MA7 : out STD_LOGIC_VECTOR (07 downto 0); MAQ : out STD_LOGIC_VECTOR (03 downto 0)); end dem00_25; architecture Behavioral of dem00_25 is FUNCTION begin PROCESS(CLK,CLR) VARIABLE QTDV,QTCH:STD_LOGIC_VECTOR(3DOWNTO 0); VARIABLE TAM,FDEM,F: INTEGER RANGE 0 TO 100000; GIAIMA(ON CHNG TRNH GIAIMA)

IF CLR=1 THEN QTCH:="0000";QTDV:="0000";FDEM:=0;F:=0;FQUET:="10"; ELSIF CLK='1' AND CLK'EVENT THEN F:=F+1; IF F=10 THEN FQUET:=FQUET+1; -- FQUT=1 KHz

VARIABLE FQUET :STD_LOGIC_VECTOR(1 DOWNTO 0); BEGIN IF CLR='0' THEN QTCH:="0000";QTDV:="0000";

IF FQUET="10" THEN FQUET:="00"; END IF; FDEM:=FDEM+1; F:=0; IF FDEM=1000 THEN FDEM:=0; QTDV:=QTDV+1; IF QTDV="1010THEN QTDV:="0000";QTCH:=QTCH+1;ENDIF; IF QTCH="1010" THEN QTCH:="0000";QTDV:="0000"; END IF; END IF; -- FM= 1Hz

FDEM:=0;F:=0;FQUET:="10";TAM:=0; ELSIF CLK='1' AND CLK'EVENT THEN F:=F+1; IF F=10 THEN FQUET:=FQUET+1; IF FQUET="10" THEN FQUET:="00"; END IF;

n tp PLD & ASIC

F:=0; FDEM:=FDEM+1; IF FDEM=500 THEN FDEM:=0;

VARIABLE FDEM,F,DVG,CHG,DVP,CHP:INTEGER RANGE 0 TO 100000; VARIABLE FQUET :INTEGER RANGE 0 TO 10;

IF TAM=0 THEN QTDV:=QTDV+1; BEGIN IF QTDV="1010"THEN QTDV:="0000";QTCH:=QTCH+1;ENDIF; IF CLR='0' THEN CHG:=0;DVG:=0;DVP:=0;CHP:=0; IF QTCH="0010" AND QTDV="0101"THEN TAM:=1; END IF; FDEM:=0;F:=0; MAQ<="0000";MA7<=X"FF"; ELSIF TAM=1 THEN QTDV:=QTDV-1; IF QTDV="1111" THEN QTDV:="1001";QTCH:=QTCH-1;END IF; IF QTCH="0000" AND QTDV="0000" THEN TAM:=0; END IF; END IF; END IF; END IF; END IF; CASE FQUET IS WHEN "00" => MA7 <= GIAIMA(QTDV); MAQ <="1110"; WHEN "01" => MA7 <= GIAIMA(QTCH); MAQ <="1101"; WHEN OTHERS => MA7<=GIAIMA(QTCH); MAQ<="1100"; END CASE; END IF; END PROCESS; END IF; end Behavioral; END IF; Bi 3: qut pht giy trn 4 led 7 on entity PHUT_GIAY is Port ( CLK.CLR : in STD_LOGIC; MA7 : out STD_LOGIC_VECTOR (07 downto 0); MAQ : out STD_LOGIC_VECTOR (03 downto 0)); end PHUT_GIAY; architecture Behavioral of PHUT_GIAY is FUNCTION begin PROCESS(CLK,CLR) GIAIMA(ON CHNG TRNH GIAIMA) END IF; CASE FQUET IS WHEN 0 => MA7 <= GIAIMA(DVG); MAQ <="1110"; WHEN 1 => MA7 <= GIAIMA(CHG); MAQ <="1101"; WHEN 2 => MA7<=GIAIMA(DVP); MAQ<="1011"; WHEN 3 => MA7<=GIAIMA(CHP); MAQ<="0111"; WHEN OTHERS => NULL; END CASE; END IF; END PROCESS; end Behavioral; ELSIF CLK='1' AND CLK'EVENT THEN F:=F+1; IF F=10 THEN FQUET:=FQUET+1; IF FQUET=4 THEN FQUET:=0; END IF; FDEM:=FDEM+1; F:=0; IF FDEM=1000 THEN FDEM:=0; DVG:=DVG+1; IF DVG=10 THEN DVG:=0; CHG:=CHG+1; IF CHG=6 THEN CHG:=0; DVP:=DVP+1; IF DVP=10 THEN DVP:=0; CHP:=CHP+1; IF CHP=6 THEN CHP:=0; END IF; END IF;

n tp PLD & ASIC

Bi 4: n giao thng: CLR=0 th xa, Fvo=100Hz. X1=X2=15S; V1=V2=5S;1=2= 20S. entity DENGIAOTHONG is Port ( CLK,CLR : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (5 downto 0)); end DENGIAOTHONG; architecture Behavioral of DENGIAOTHONG is begin PROCESS(CLK,CLR) VARIABLE F,DEM: INTEGER; BEGIN IF CLR ='0' THEN F:=0; DEM:=0;Q<= "000000"; ELSIF CLK='1' AND CK'EVENT THEN F:= F+1; IF F = 100 THEN F :=0; DEM := DEM +1; IF DEM = 30 THEN DEM:=0; END IF; END IF; END IF; IF (0 <= DEM) AND ( DEM <11) Q <= "100100"; THEN

Bi 5: m chn l theo th t sau:0,2,414 1,3,515 0 entity DEM_CHANLE is Port ( CLK,CLR : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (3 downto 0)); end DEM_CHANLE; architecture Behavioral of DEM_CHANLE is begin PROCESS(CLK,CLR) VARIABLE QT: STD_LOGIC_VECTOR(3 DOWNTO 0); VARIABLE TAM: INTEGER RANGE 0 TO 1; BEGIN IF CLR='0' THEN QT:= "0000"; TAM:=0; ELSIF CLK='1' AND CLK'EVENT THEN IF TAM = 0 THEN IF QT = "1110" THEN TAM:=1;QT:= "0001"; ELSE QT:= QT + 2; END IF; ELSIF TAM=1 THEN IF QT ="1111" THEN TAM:=0; QT:= "0001"; ELSE QT:= QT + 2;END IF; END IF; END IF; Q <= QT; END PROCESS; end Behavioral; Bi 6: m 2 knh. Knh 1 m ln. Knh 2 m xung. dng sw chn ch entity BAITHI_TAM is

Port ( CLK,CLR,SW1,SW2 : in STD_LOGIC; MA7 : out STD_LOGIC_VECTOR (03 downto 0); MAQ : out STD_LOGIC_VECTOR (07 downto 0)); end BAITHI_TAM; architecture Behavioral of BAITHI_TAM is SIGNAL S : STD_LOGIC_VECTOR(1 DOWNTO 0); begin PROCESS(CLK,CLR,S) VARIABLE QDV1,QCH1,QDV2,QCH2:STD_LOGIC_VECTOR(3 DOWNTO 0); VARIABLE TAM,FDEM,F,DEM: INTEGER RANGE 0TO 100000;

-- XANH 1= DO2 = ON

ELSIF (11<= DEM) AND ( DEM <15) THEN Q <= "010100"; -- VANG 1= DO 2 = ON

ELSIF (15<= DEM) AND ( DEM <25) THEN Q <= "001001"; -- DO 1 = XANH 2 = ON

ELSIF (25<= DEM) AND ( DEM <30) THEN Q <= "001010"; END IF; END PROCESS; -- DO 1 = VANG 2 = ON

VARIABLE FQUET :STD_LOGIC_VECTOR(1 DOWNTO 0); end Behavioral; BEGIN S<= SW1&SW2;

n tp PLD & ASIC

CASE (S) IS WHEN "00" => TAM:= 0; WHEN "01" => TAM:= 1; WHEN "10" => TAM:= 2; WHEN "11" => TAM:= 3; WHEN OTHERS => NULL; END CASE; IF CLK='1' AND CLK'EVENT THEN F:=F+1; IF F=10 THEN FQUET:=FQUET+1;FDEM:=FDEM+1; F:=0; IF FDEM=100 THEN FDEM:=0; IF TAM=0 THEN QDV1:="0000";QDV2:="0000"; QCH1:="0000";QCH2:="0000"; ELSIF TAM=1 THEN QDV1:=QDV1+1; IF QDV1="1010"THEN QDV1:="0000"; QCH1:=QCH1+1; IF QCH1="1010" THEN QCH1:="0000"; END IF; END IF; ELSIF TAM=2 THEN QDV2:=QDV2-1; IF QDV2="1111" THEN QDV2:= "1001"; QCH2:=QCH2-1; IF QCH2="1111" THEN QCH2:="1001"; END IF; END IF; ELSIF TAM=3 THEN QDV1:=QDV1+1; IF QDV1="1010" THEN QDV1:="0000"; QCH1:=QCH1+1; IF QCH1="1010" THEN QCH1:="0000"; END IF; END IF; QDV2:=QDV2-1;

IF QDV2="1111" THEN QDV2:= "1001"; QCH2:=QCH2-1; IF QCH2="1111" THEN QCH2:="1001"; END IF; END IF; END IF; END IF; END IF; END IF; CASE FQUET IS WHEN "00" => MAQ <= GIAI_MA(QDV1); MA7 <="1110"; WHEN "01" => MAQ <= GIAI_MA(QCH1); MA7 <="1101"; WHEN "10" => MAQ <= GIAI_MA(QDV2); MA7 <="1011"; WHEN "11" => MAQ <= GIAI_MA(QCH2); MA7 <="0111"; WHEN OTHERS => NULL; END CASE; END PROCESS; end Behavioral; Bi 7: mch m nh phn 4bit theo th t sau 115314 lp li entity DEMTONGHOP is Port ( CLR,CLK : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (03 downto 0)); end DEMTONGHOP; architecture Behavioral of DEMTONGHOP begin PROCESS(CLK,CLR) VARIABLE QT: STD_LOGIC_VECTOR(3 DOWNTO 0); VARIABLE BEGIN IF CLR='0' THEN QT:= "0000"; TAM:=0; TAM: INTEGER RANGE 0 TO 2; is

n tp PLD & ASIC

ELSIF CLK='1' AND CLK'EVENT THEN IF TAM = 0 THEN

IF QT = "1111" THEN TAM:=1; ELSE QT:= QT + 1; END IF; ELSIF TAM=1 THEN QT:= QT - 1; IF QT ="0011" THEN TAM:=2; END IF; ELSIF TAM=2 THEN QT:= QT + 1; IF QT ="1111" THEN TAM:=0; END IF; END IF; END IF; Q <= QT; END PROCESS; end Behavioral;