- -- Download from: www.pld.com.cn & www.fpga.com.cn
- LIBRARY IEEE;
- USE IEEE.std_logic_1164.ALL;
- USE IEEE.std_logic_unsigned.all;
- USE IEEE.std_logic_arith.all;
- ENTITY dccount IS
- port (
- clk : IN STD_LOGIC;
- AI : IN STD_LOGIC_VECTOR(3 DOWNTO 0);
- CO : out STD_LOGIC_VECTOR(3 DOWNTO 0);
- pulse: IN STD_LOGIC;
- driverA,driverB: OUT STD_LOGIC;
- S : OUT STD_LOGIC_VECTOR(6 DOWNTO 0);
- P : OUT STD_LOGIC_VECTOR(5 DOWNTO 0);
- sp : out STD_LOGIC);
- END dccount;
- ARCHITECTURE a OF dccount IS
- SIGNAL f: STD_LOGIC_VECTOR(5 downto 0);
- SIGNAL f_hz: STD_LOGIC;
- SIGNAL OSC: STD_LOGIC;
- SIGNAL OSC1: STD_LOGIC;
- SIGNAL C: STD_LOGIC_VECTOR(3 DOWNTO 0);
- SIGNAL A: STD_LOGIC_VECTOR(3 DOWNTO 0);
- SIGNAL CODE: STD_LOGIC_VECTOR(2 DOWNTO 0);
- SIGNAL bcd:STD_LOGIC_VECTOR(23 downto 0);
- SIGNAL COUNT:STD_LOGIC_VECTOR(23 downto 0);
- SIGNAL SUM: STD_LOGIC_VECTOR(4 DOWNTO 0);
- SIGNAL D: STD_LOGIC_VECTOR(3 DOWNTO 0):="0000";
- SIGNAL ind_coil: STD_LOGIC_VECTOR(3 downto 0) := "0001";
- SIGNAL Hz:STD_LOGIC;
- SIGNAL spo: STD_LOGIC;
- SIGNAL SW:STD_LOGIC;
- SIGNAL KEY:STD_LOGIC;
- SIGNAL DRA,DRB:STD_LOGIC;
- BEGIN
- DRIVERA<=DRA;
- DRIVERB<=DRB;
- p(5 downto 0) <= f(5 downto 0);
- CO(3 DOWNTO 0) <=C(3 DOWNTO 0);
- A (3 DOWNTO 0)<=AI(3 DOWNTO 0);
- SP<=SPO;
- -----------------div------------------------
- PROCESS(OSC)
- VARIABLE d_ff: STD_LOGIC_VECTOR(20 downto 0);
- VARIABLE DELAY: STD_LOGIC_VECTOR(23 DOWNTO 0);
- BEGIN
- WAIT UNTIL clk = '1';
- IF d_ff(20 downto 0) >= 2E6 THEN
- d_ff(20 downto 0) := "000000000000000000000";
- ELSE
- d_ff(20 downto 0) := d_ff + 1;
- END IF;
- f_hz <= not d_ff(6);
- OSC <= not d_ff(10);
- OSC1 <= not d_ff(17);
- IF DELAY>=500000 THEN
- DELAY:="000000000000000000000000";HZ<=NOT Hz;
- ELSE DELAY:=DELAY+1;
- END IF;
- END PROCESS ;
- ----------------------------------------------
- PROCESS (F_HZ,OSC,OSC1,HZ,PULSE)
- VARIABLE B: STD_LOGIC;
- VARIABLE INT: STD_LOGIC;
- VARIABLE Data: STD_LOGIC_VECTOR(3 DOWNTO 0);
- VARIABLE UP: STD_LOGIC_VECTOR(2 DOWNTO 0);
- VARIABLE ST:STD_LOGIC_VECTOR(2 DOWNTO 0);
- VARIABLE seq: STD_LOGIC_VECTOR(1 DOWNTO 0);
- VARIABLE sound: STD_LOGIC_VECTOR(1 DOWNTO 0);
- VARIABLE STOP: STD_LOGIC;
- VARIABLE direction: STD_LOGIC;
- BEGIN
- ---------------7seg scan-------------------------------
- IF (f_HZ='1' AND F_HZ'EVENT) THEN
- IF ST(2 DOWNTO 0)= "101" THEN
- ST(2 DOWNTO 0):="000" ;
- ELSE ST(2 DOWNTO 0):=ST(2 DOWNTO 0)+1;
- END IF;
- END IF;
- -----------------------------------------------
- IF (OSC = '1' and OSC'EVENT) THEN
- if a="1111" then
- IF seq="11" then seq:="00" ;
- else seq:=seq+1;
- end if;
- else seq:=seq;
- end if;
- IF seq="00" then C(3 DOWNTO 0)<="1110" ;
- elsif seq="01" then C(3 DOWNTO 0)<="1101" ;
- elsif seq="10" then C(3 DOWNTO 0)<="1011" ;
- elsif seq="11" then C(3 DOWNTO 0)<="0111" ;
- end if;
- end if;
- IF A="1111" THEN INT:='0';
- ELSE INT:='1';
- END IF;
- IF (HZ'EVENT AND HZ='1') THEN
- IF STOP='1' THEN UP:=UP+1;
- ELSE UP:="000";
- END IF;
- END IF;
- IF KEY='1' AND COUNT/=0 THEN
- IF DIRECTION='0' THEN DRA<='1';DRB<='0';STOP:='0';
- ELSE DRA<='0';DRB<='1';STOP:='0';
- END IF;
- ELSIF KEY='1' AND COUNT=0 THEN STOP:='1';
- ELSIF STOP='1' AND UP<"110" THEN DRA<=NOT DRA ;DRB<=NOT DRB;
- ELSE DRA<='0';DRB<='0';STOP:='0';
- END IF;
- iF (OSC1'EVENT AND OSC1='1') THEN B:=(INT and (b and INT) );
- ----------------direction choice-----------------------
- IF SW='0' AND B='1' AND Data(3 DOWNTO 0)="1010" THEN
- BCD(23 DOWNTO 0)<=BCD(23 DOWNTO 0);B:='0';direction:='1';
- ELSIF SW='0' AND B='1' AND Data(3 DOWNTO 0)="1011" THEN
- BCD(23 DOWNTO 0)<=BCD(23 DOWNTO 0);B:='0';direction:='0';
- ------------------KEY LOCK ON/OFF------------------------------------
- ELSIF B='1' AND Data(3 DOWNTO 0)="1100" THEN
- BCD<=BCD;B:='0';SW<=NOT SW;
- -------------------BACK-------------------------------
- ELSIF SW='0' AND B='1' AND Data(3 DOWNTO 0)="1101" THEN
- BCD(23 DOWNTO 0)<=("0000" & bcd(23 downto 4) );B:='0';
- -------------------CLEAR---------------------------------
- ELSIF B='1' AND Data(3 DOWNTO 0)="1110" THEN
- BCD<="000000000000000000000000" ;B:='0';SW<='0';
- ------------------DOWNCOUNT 0 ------------------------------------
- ELSIF BCD=0 AND KEY='1' THEN SW<='0';KEY<='0';BCD<=BCD;
- ------------------------------------------------------------------
- ELSIF SW='1' AND KEY='1' AND DATA(3 DOWNTO 0)/="1110" THEN BCD<=COUNT;
- ELSIF SW='1' AND DATA(3 DOWNTO 0)="1110" THEN SW<='0';
- BCD<="000000000000000000000000";KEY<='0';
- -------------------ENTER------------------------------
- ELSIF SW='0' AND B='1' AND Data(3 DOWNTO 0)="1111" AND BCD/=0 THEN
- B:='0';SW<='1';KEY<='1';
- ELSIF SW='0' AND B='1' AND Data(3 DOWNTO 0)="1111" AND BCD=0 THEN
- BCD<=BCD;B:='0';SW<='0';KEY<='0';
- ----------------------NUMBER SHIFT---------------------------
- ELSIF SW='0' AND B='1' THEN bcd(23 downto 0)<= bcd(19 downto 0) & DATA(3 DOWNTO 0);B:='0';
- elsif INT='0' then bcd<=bcd;
- b:='1';
- END IF;
- END IF;
- ------------------------------------------------------
- SUM(1 DOWNTO 0)<= seq;
- sum(4 downto 2)<=code;
- ---------------------SOUND-----------------------------
- if (osc'event and osc='1') then
- if INT='1' AND SW='0' then sound:=sound+1;spo<=sound(1) ;
- else spo<='0';
- end if;
- end if;
- -------------------------------------------------------
- ---------------------------DOWNCOUNT--------------------
- IF SW='0' THEN COUNT<=BCD;
- ELSE
- If (PULSE = '1' and PULSE'EVENT) THEN
- IF KEY='1' AND COUNT/=0 THEN
- ----------------------------1---------------------------------------------------
- IF COUNT(3 DOWNTO 0)= 0 AND COUNT(23 DOWNTO 4)>=1 THEN
- COUNT(3 DOWNTO 0) <="1001" ;
- ELSE COUNT(3 DOWNTO 0) <= COUNT(3 DOWNTO 0)-1;
- END IF;
- ----------------------------2---------------------------------------------------------
- IF COUNT(7 DOWNTO 4)= 0 AND COUNT(23 DOWNTO 8)>=1 AND COUNT(3 DOWNTO 0)=0 THEN
- COUNT(7 DOWNTO 4) <= "1001" ;
- ELSIF COUNT(3 DOWNTO 0)=0 AND COUNT(23 DOWNTO 4)>=1 THEN
- COUNT(7 DOWNTO 4) <=COUNT(7 DOWNTO 4 )-1;
- END IF;
- ----------------------------3-------------------------------------------------------
- IF COUNT(11 DOWNTO 8)= 0 AND COUNT(23 DOWNTO 12)>=1 AND COUNT(7 DOWNTO 0)=0 THEN
- COUNT(11 DOWNTO 8) <="1001";
- ELSIF COUNT(7 DOWNTO 0)=0 AND COUNT(23 DOWNTO 8)>=1 THEN
- COUNT(11 DOWNTO 8) <=COUNT(11 DOWNTO 8 )-1;
- END IF;
- ---------------------------4--------------------------------------------------------------
- IF COUNT(15 DOWNTO 12)= 0 AND COUNT(23 DOWNTO 16)>=1 AND COUNT(11 DOWNTO 0)=0 THEN
- COUNT(15 DOWNTO 12) <="1001";
- ELSIF COUNT(11 DOWNTO 8)=0 AND COUNT(23 DOWNTO 12)>=1 AND COUNT(7 DOWNTO 0)=0 THEN
- COUNT(15 DOWNTO 12) <=COUNT(15 DOWNTO 12 )-1;
- END IF;
- -------------------------5------------------------------------------------------------
- IF COUNT(19 DOWNTO 16)= 0 AND COUNT(23 DOWNTO 20)>=1 AND COUNT(15 DOWNTO 0)=0 THEN
- COUNT(19 DOWNTO 16) <="1001";
- ELSIF COUNT(15 DOWNTO 12)=0 AND COUNT(23 DOWNTO 16)>=1 AND COUNT(11 DOWNTO 0)=0 THEN
- COUNT(19 DOWNTO 16) <=COUNT(19 DOWNTO 16 )-1;
- END IF;
- -------------------------6-------------------------------------------------
- IF COUNT(23 DOWNTO 20)= 0 THEN
- COUNT(23 DOWNTO 20) <="0000";
- ELSIF COUNT(19 DOWNTO 16)=0 AND COUNT(23 DOWNTO 20 )>=1 AND COUNT(15 DOWNTO 0)=0 THEN
- COUNT(23 DOWNTO 20) <=COUNT(23 DOWNTO 20 )-1;
- END IF;
- END IF;
- END IF;
- END IF;
- ------------------7seg scan---------------------------
- CASE ST IS --