-- file "distance.vhd" --------------------------------------------------------------------- -- calculates the distance (in micrometres) of the wall given the -- number of pulses from a 12.5 MHz clock -- written by Shaun Luong, Clifton Yeung, Jon Paul Kansky -- and Patrick Asiedu-Ampem, University of Alberta -- November 1998 ---------------------------------------------------------------------- library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity distance is generic ( distance_width: positive:= 38; pulse_width: positive:= 19 ); port ( clock, reset: in std_logic; pulse_count: in std_logic_vector(pulse_width-1 downto 0); distance: out std_logic_vector(distance_width-1 downto 0); done: out std_logic ); end distance; architecture structural of distance is constant velocity: positive:= 7; -- change this constant for different clock speeds constant max_dist: positive:= 3000000; -- max. distance = 3 m begin process(reset, clock) variable duration: std_logic_vector(pulse_width-1 downto 0); variable dist_max: std_logic_vector(distance_width-1 downto 0); variable dist: std_logic_vector(distance_width-1 downto 0); begin -- convert the integer 'velocity' to a binary vector 'duration' duration := conv_std_logic_vector(velocity, pulse_width); -- convert the integer 'max_dist' to a binary vector 'dist_max' dist_max := conv_std_logic_vector(max_dist, distance_width); if reset = '1' then -- active high done <= '0'; elsif rising_edge(clock) then dist := pulse_count*duration; if dist > dist_max then -- set limit on distance dist := dist_max; end if; done <= '1'; -- finished calculating end if; distance <= dist; end process; end structural;