📄 measure.vhd
字号:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
--测量部分;
entity measure is
Port (clk : in std_logic; --1Hz扫描频率;
clk10 : in std_logic; --1kHz扫描频率;
s df start : in std_logic; --使能信号;
dina : in std_logic; --停车信号;
dinb : in std_logic; --路程脉冲信号;
distance : out std_logic_vector(15 downto 0); --总行车路程;
stime : out std_logic_vector(15 downto 0); --总停车时间;
rtime : out std_logic_vector(15 downto 0); --总行车时间;
amoney: out std_logic_vector(15 downto 0)); --计费;
end measure;
architecture Behavioral of measure is
signal stime_all : std_logic_vector(15 downto 0);
signal rtime_all : std_logic_vector(15 downto 0);
signal distance_all : std_logic_vector(7 downto 0);
signal money_distance : std_logic_vector(15 downto 0);
signal money_stime : std_logic_vector(15 downto 0);
signal money_all : std_logic_vector(15 downto 0);
begin
--------------------------------------------------------------------------------------------------------
--- 路程测量
--------------------------------------------------------------------------------------------------------
process(dinb,start,dina)
variable cnt_bdistance : integer range 0 to 3;
begin
if start='1' then cnt_bdistance:=0;
money_distance<="0000000000000000";
distance_all<="00000000";
elsif dinb'event and dinb='1' then
if dina='0' then
if cnt_bdistance<2 then cnt_bdistance:=cnt_bdistance+1; --基本路程3公里;
else money_distance<=money_distance+15; --超过3公里,每公里按1.5元收费;
end if;
if distance_all(3 downto 0)="1001" then distance_all(3 downto 0)<="0000"; --总的行车路程;
if distance_all(7 downto 4)="1001" then distance_all(7 downto 4)<="0000";
else distance_all(7 downto 4)<=distance_all(7 downto 4)+1;
end if;
else distance_all(3 downto 0)<=distance_all(3 downto 0)+1;
end if;
end if;
end if;
end process;
distance<="11111111"&distance_all;
--------------------------------------------------------------------------------------------------------
--- 时间测量
--------------------------------------------------------------------------------------------------------
process(clk,start)
variable cnt_time1 : integer range 0 to 60;
variable cnt_time2 : integer range 0 to 60; --2分钟计时;
begin
if start='1' then cnt_time1:=0;cnt_time2:=0;
money_stime<="0000000000000000";
stime_all<="0000000000000000";
elsif clk'event and clk='1' then
if dina='1' then
if cnt_time2<59 then cnt_time2:=cnt_time2+1; --超过2分钟,每分钟按1.1元收费;
elsif cnt_time1<59 then cnt_time1:=cnt_time1+1;
else cnt_time1:=0;money_stime<=money_stime+11;
end if;
if stime_all(3 downto 0)="1001" then stime_all(3 downto 0)<="0000"; --停留的总时间(单位:秒);
if stime_all(7 downto 4)="1001" then stime_all(7 downto 4)<="0000";
if stime_all(11 downto 8)="1001" then stime_all(11 downto 8)<="0000";
if stime_all(15 downto 12)="1001" then stime_all(15 downto 12)<="0000";
else stime_all(15 downto 12)<=stime_all(15 downto 12)+1; end if;
else stime_all(11 downto 8)<=stime_all(11 downto 8)+1;end if;
else stime_all(7 downto 4)<=stime_all(7 downto 4)+1;end if;
else stime_all(3 downto 0)<=stime_all(3 downto 0)+1;end if;
end if;
end if;
end process;
stime<=stime_all;
process(start,clk)
begin
if start='1' then rtime_all<="0000000000000000";
elsif clk'event and clk='1' then
if rtime_all(3 downto 0)="1001" then rtime_all(3 downto 0)<="0000"; --总行车时间(单位:秒);
if rtime_all(7 downto 4)="1001" then rtime_all(7 downto 4)<="0000";
if rtime_all(11 downto 8)="1001" then rtime_all(11 downto 8)<="0000";
if rtime_all(15 downto 12)="1001" then rtime_all(15 downto 12)<="0000";
else rtime_all(15 downto 12)<=rtime_all(15 downto 12)+1; end if;
else rtime_all(11 downto 8)<=rtime_all(11 downto 8)+1;end if;
else rtime_all(7 downto 4)<=rtime_all(7 downto 4)+1;end if;
else rtime_all(3 downto 0)<=rtime_all(3 downto 0)+1;end if;
end if;
end process;
rtime<=rtime_all;
--------------------------------------------------------------------------------------------------------
--- 价格换算(用BCD码显示价格)
--------------------------------------------------------------------------------------------------------
money_all<=100 + money_distance + money_stime; --100为10.0元(基本收费);
process(start,clk10)
variable state : integer range 0 to 5;
variable mhh,mhl,mlh,mll : std_logic_vector(3 downto 0);
variable money : std_logic_vector(15 downto 0);
begin
if start='1' then state:=0;
amoney<="0000000000000000";
elsif clk10'event and clk10='1' then
case state is
when 0 =>
money:=money_all;
mhh:="0000";
mhl:="0000";
mlh:="0000";
mll:="0000";
state:=1;
when 1 =>
if money>999 then money:=money-1000;mhh:=mhh+1;
else state:=2;
end if;
when 2 =>
if money>99 then money:=money-100;mhl:=mhl+1;
else state:=3;
end if;
when 3 =>
if money>9 then money:=money-10;mlh:=mlh+1;
else state:=4;
end if;
when 4 =>
mll:=money(3 downto 0);
state:=5;
when 5 =>
amoney<=mhh&mhl&mlh&mll;
state:=0;
when others =>
state:=0;
end case;
end if;
end process;
end Behavioral;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -