与其解决你的作业,我想给你一个想法。大多数设计人员倾向于使用逐位翻转来实现此时钟(一些数字将从 9-0 翻转,其他数字从 5-0 翻转)。我想提出一些不同的建议。
总体思路是:将您的时间值以秒为单位保持为整数。这将极大地方便递增和递减的任务。然后,您只需实现一个转换函数,它返回小时数、分钟数和秒数,给定一个整数秒数。
您的时钟实体将如下所示:
library ieee;
use ieee.std_logic_1164.all;
use work.clock_pkg.all;
entity clock is
port (
clock: in std_logic;
n_reset: in std_logic;
increment: in std_logic;
decrement: in std_logic;
hours: out natural range 0 to 99;
minutes: out natural range 0 to 59;
seconds: out natural range 0 to 59
);
end;
architecture rtl of clock is
signal time_in_seconds: natural range 0 to 359999;
begin
process (clock, n_reset) begin
if rising_edge(clock) then
if n_reset = '0' then
time_in_seconds <= 0;
elsif increment then
time_in_seconds <= time_in_seconds + 1;
elsif decrement then
time_in_seconds <= time_in_seconds - 1;
end if;
end if;
end process;
process (time_in_seconds) begin
(hours, minutes, seconds) <= seconds_to_time_type(time_in_seconds);
end process;
end;
可以想象,这个解决方案的主力是seconds_to_time_type()
函数。你可以像这样实现它:
package clock_pkg is
type time_type is record
hours: natural range 0 to 99;
minutes, seconds: natural range 0 to 59;
end record;
function seconds_to_time_type(seconds: in natural) return time_type;
end;
package body clock_pkg is
function seconds_to_time_type(seconds: in natural) return time_type is
variable hh: natural range 0 to 99;
variable mm: natural range 0 to 119;
variable ss: natural range 0 to 119;
begin
hh := seconds / 3600;
mm := (seconds mod 3600) / 60;
ss := (seconds mod 3600) mod 60;
return (hh, mm, ss);
end;
end;
现在,您有了一个实体,它输出小时、分钟和秒的单独整数值。将这些值从整数转换为 BCD,并在显示器上显示这些值作为练习留给读者。