Stop Watch Coding (VHDL)
Stop Watch Coding (VHDL)
MACHINE(STMCHINE):library IEEE;
use IEEE.std_logic_1164.all;
entity stmchine is
port(CLK, RESET, STRTSTOP : in STD_LOGIC;
CLKEN, RST : out STD_LOGIC
);
end stmchine;
architecture inside of stmchine is
type stmchine_state is (clear, zero, start, counting, stop, stopped);
-- attribute syn_encoding : string;
-- attribute syn_encoding of stmchine_state : type is "binary";
signal current_state, next_state : stmchine_state;
-- attribute state_machine : boolean;
-- attribute state_machine of current_state : signal is true;
begin
process(STRTSTOP, current_state)
begin
-- Assign defaults, so as not set them in every state
CLKEN <= '0';
RST <= '0';
case current_state is
when clear => next_state <= zero;
RST <= '1';
XCOUNTER(TENTHS):library IEEE;
use IEEE.std_logic_1164.all;
entity tenths is
port(
CLK_EN: IN std_logic;
CLOCK: IN std_logic;
ASYNC_CTRL: IN std_logic;
Q_OUT: OUT std_logic_vector(9 DOWNTO 0);
TERM_CNT: OUT std_logic
);
end tenths;
architecture inside of tenths is
type state_type is (s0,s1,s2,s3,s4,s5,s6,s7,s8,s9);
attribute enum_encoding: string;
attribute enum_encoding of state_type: type is "0000000001 0000000010
0000000100 0000001000 0000010000 0000100000 0001000000
0010000000 0100000000 1000000000";
signal cs: state_type;
signal ns: state_type;
begin
process(CLK_EN,CLOCK,ASYNC_CTRL)
begin
if(ASYNC_CTRL='1') then
cs <= s0;
elsif(CLK_EN='1') then
if(CLOCK'event) then
cs <= ns;
end if;
end if;
end process;
process(cs)
begin
case cs is
when s0=>
ns<=s1;
Q_OUT<="0000000001";
when s1=>
ns<=s2;
Q_OUT<="0000000010";
when s2=>
ns<=s3;
Q_OUT<="0000000100";
when s3=>
ns<=s4;
Q_OUT<="0000001000";
when s4=>
ns<=s5;
Q_OUT<="0000010000";
when s5=>
ns<=s6;
Q_OUT<="0000100000";
when s6=>
ns<=s7;
Q_OUT<="0001000000";
when s7=>
ns<=s8;
Q_OUT<="0010000000";
when s8=>
ns<=s9;
Q_OUT<="0100000000";
when s9=>
ns<=s0;
Q_OUT<="1000000000";
end case;
end process;
process(cs)
begin
if(cs=s9) then
TERM_CNT<='1';
else
TERM_CNT<='0';
end if;
end process;
end inside;
SIXTY(CNT60):library ieee;
use ieee.std_logic_1164.all;
entity CNT60 is
port (CE, CLK, CLR : in std_logic;
LSBSEC, MSBSEC : out std_logic_vector(3 downto 0));
end CNT60;
architecture XILINX of CNT60 is
component SMALLCNTR
port (CE, CLK, CLR : in std_logic;
QOUT : out std_logic_vector(3 downto 0));
end component;
signal LSBOUT, MSBOUT : std_logic_vector(3 downto 0);
signal MSBCE, LSBTC, MSBCLR, MSBTC : std_logic;
begin
LSBCOUNT : SMALLCNTR
port map(CE => CE, CLK => CLK, CLR => CLR, QOUT => LSBOUT);
MSBCOUNT : SMALLCNTR
port map(CE => MSBCE, CLK => CLK, CLR => MSBCLR, QOUT =>
MSBOUT);
LSBTC <= '1' when (LSBOUT = "1001") else '0';
MSBTC <= '1' when (MSBOUT = "0110") else '0';
MSBCE <= CE and LSBTC;
MSBCLR <= CLR or MSBTC;
LSBSEC <= LSBOUT;
MSBSEC <= MSBOUT;
end XILINX;
MAIN PROJECT
library IEEE;
use IEEE.std_logic_1164.all;
library unisim;
use unisim.vcomponents.all;
entity watch is
port (RESET, CDRST,STRTSTOP : in STD_LOGIC;
CLK : in STD_LOGIC;
TENSOUT, ONESOUT : out STD_LOGIC_VECTOR(6 downto 0);
TENTHSOUT : out STD_LOGIC_VECTOR(9 downto 0));
end watch;
architecture XILINX of watch is
component CLK_DIV16R port (
CLKIN : in std_logic;
CDRST : in std_logic;
CLKDV : out std_logic
);
end component;
component stmchine
port (CLK, RESET, STRTSTOP : in STD_LOGIC;
CLKEN, RST : out STD_LOGIC);
end component;
component tenths
port (CLOCK, CLK_EN, ASYNC_CTRL : in STD_LOGIC;
TERM_CNT : out STD_LOGIC;
Q_OUT : out STD_LOGIC_VECTOR(9 downto 0));
end component;
component cnt60
port (CE, CLK, CLR : in STD_LOGIC;
LSBSEC, MSBSEC : out STD_LOGIC_VECTOR(3 downto 0));
end component;
component hex2led
port (HEX : in STD_LOGIC_VECTOR(3 downto 0);
LED : out STD_LOGIC_VECTOR(6 downto 0));
end component;
signal clkint, clkenable : STD_LOGIC;
signal strtstopinv, rstint, xtermcnt, cnt60enable : STD_LOGIC;
signal xcountout : STD_LOGIC_VECTOR(9 downto 0);
signal lsbcnt, msbcnt : STD_LOGIC_VECTOR(3 downto 0);
begin
MACHINE : stmchine
port map(CLK => clkint, RESET => RESET, STRTSTOP => strtstopinv,
CLKEN => clkenable, RST => rstint);
XCOUNTER : tenths
SCHEMATIC DIAGRAM-
* HEX2LED -HDL-based macro. This macro decodes the ones and tens
digit values
from hexadecimal to 7-segment display format.
SIMULATION:
Requires the following simulation libraries:
Unisims
Simprims
Behavioural and RTL Simulation done using Testbench Waveform
file(watch_tb.tbw).
---------------------------------------------------------------------------------------------------