VHDL中的BCD定时器

时间:2018-05-26 20:11:59

标签: timer vhdl counter bcd

出于好奇,不久前就开始使用VHDL了。

所以我试图在斯巴达3板上写BCD计时器和

不知怎的,我无法找出为什么它一直表现出来的意外。错误。

所以如果我想拥有图片链接所示的功能, 如何修改代码?任何帮助都会感激不尽。

my initial sketch_picture link to imgur
(点击)

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;


entity w3 is
    Port ( clk : in  STD_LOGIC;
           rst : in  STD_LOGIC;
           stp : in  STD_LOGIC;
           an : out  STD_LOGIC_VECTOR (3 downto 0);
           c : out  STD_LOGIC_VECTOR (6 downto 0));
end w3;

architecture timer of w3 is
signal div1 : integer range 0 to 499999 :=0; -- 100Hz
signal ck100hz : std_logic; -- 100Hz output
signal div2 : integer range 0 to 249999 :=0; -- 200Hz
signal ck200hz : std_logic; -- 200Hz output
signal div3 : integer range 0 to 124999 :=0; -- 400Hz
signal ck400hz : std_logic; -- 400Hz output
signal index : integer range 0 to 9 :=0;
signal scan : std_logic_vector (3 downto 0);
signal S : std_logic;
signal disp : std_logic_vector (3 downto 0);

begin
process begin
wait until rising_edge(clk);
if div1 < 499999 then
    ck100hz <= '0';
    div1 <= div1+1;
else 
    ck100hz <= '1';
    div1 <= 0;
end if;
if div2 < 249999 then
    ck200hz <= '0';
    div2 <= div2+1;
else
    ck200hz <= '1';
    div2 <= 0;
end if;
if div3 < 124999 then
    ck400hz <= '0';
    div3 <= div3+1;
else
    ck400hz <= '1';
    div3 <= 0;
end if;
end process;

process begin
wait until rising_edge(clk);
if rst = '1' then 
    index <= 0; 
end if;
if stp = '1' then 
    index <= index; 
end if;

if ck100hz = '1' then
    if index < 3 then index <= index+1;
        else index <= 0;
        if index < 4 and index > 7 then index <= index+1;
            else index <= 0;
            if index < 8 and index > 11 then index <= index+1;
                else index <= 0;
                if index < 12 and index > 15 then index <= index+1;
                    else index <= 0;
   end if;
      end if;
          end if;
             end if;
end if;
end process;

process begin
wait until rising_edge(clk);
if ck400hz = '1' then
    With scan select -- error unexpected With
        an <= an(0) when "00",
              an(1) when "01",
              an(2) when "10",
              an(3) when others;
end if;
end process;

process begin
wait until rising_edge(clk);
if ck200hz = '1' then
    With S select   -- error unexpected With
    disp <= index integer range 0 to 3 when "00",
            index integer range 4 to 7 when "01",
            index integer range 8 to 11 when "10",
            index integer range 12 to 15 when others;
end if;
end process;




with index select
    C <= "1000000" when 0, 
          "1111001" when 1, 
          "0100100" when 2, 
          "0110000" when 3, 
          "0011001" when 4, 
          "0010010" when 5, 
          "0000010" when 6,
          "1111000" when 7,       
          "0000000" when 8,
          "0011000" when 9;

end timer;

1 个答案:

答案 0 :(得分:1)

对于低效率:
如果0111111,您应该将C分配给index = 0。您必须启用几乎所有细分。现在你的内部计算将是高活跃的。由于PCB布局,显示器本身是低有效的,因此在将其分配给C端口之前,您应该反转整个Cathode_n向量:Cathode_n <= not C;注意,我使用{{1指定此端口的低活动行为。

旧代码:

_n

这应该是编写纯粹高效的代码时的目标:

with index select
  C <= "1000000" when 0, 
       "1111001" when 1,
       -- ...
       "0011000" when 9;

高活动意味着:如果某位为高(with index select C <= "0111111" when 0, "0000110" when 1, -- ... "1100111" when 9; Cathode_n <= not C; ),那么事物就会激活。在您的情况下,7段显示器的LED被激活。根据PCB设计,您必须驱动低电平(1)才能激活灯光。这是低活跃的,因为低值会激活某些东西。

select语句需要将位置分配给应该被启发的0,而不是要关闭的位置。应将更多低于低有效信号标记为代码以表示存在不同的行为。只有顶级元件才能将高有效信号转换为低有效信号,反之亦然。这确保了您设计的内部部分纯粹是高度活跃的。

对于没有司机:
没有1S的作业。这两个信号分别为scan'U'

编辑:
每个信号分配都会在信号上创建驱动器。目前,您的代码从未向"UUUU"S分配任何值。因此scanS的初始值成为信号的驱动值。您应该运行模拟并在波形中看到很多scan

综合工具可能会报告:信号U已被读取,但从未被分配。

使用S

我重新格式化了你可怕的if-then-else结构:

elsif

现在我们看到,您的代码无法使用 elsif ,因为您在下一个 if 语句之前在 else 分支中进行了分配。另一方面,if ck100hz = '1' then if index < 3 then index <= index+1; else index <= 0; if index < 4 and index > 7 then index <= index+1; else index <= 0; if index < 8 and index > 11 then index <= index+1; else index <= 0; if index < 12 and index > 15 then index <= index+1; else index <= 0; end if; end if; end if; end if; end if; 分配是多余的,可以删除:

index <= 0;

现在我们可以将其转换为使用 elsif 分支:

if ck100hz = '1' then
  if index < 3 then
    index <= index+1;
  else
    if index < 4 and index > 7 then
      index <= index+1;
    else
      if index < 8 and index > 11 then
        index <= index+1;
      else
        if index < 12 and index > 15 then
          index <= index+1;
        else
          index <= 0;
        end if;
      end if;
    end if;
  end if;
end if;

更具可读性,对吧?

接下来,让我们检查一下该语句中的表达式:

if ck100hz = '1' then
  if index < 3 then
    index <= index + 1;
  elsif index < 4 and index > 7 then
    index <= index + 1;
  elsif index < 8 and index > 11 then
    index <= index + 1;
  elsif index < 12 and index > 15 then
    index <= index+1;
  else
    index <= 0;
  end if;
end if;

elsif index < 4 and index > 7 then 同时不能小于4且大于7。因此,让我们进行综合并优化远离不可达的分支:

index

好的,代码中的其他问题:

if ck100hz = '1' then
  if index < 3 then
    index <= index + 1;
  else
    index <= 0;
  end if;
end if;

重置应始终具有最高优先级。在您的情况下,例如if rst = '1' then index <= 0; end if; if stp = '1' then index <= index; end if; if ck100hz = '1' then -- ... end if; 具有更高的优先级。在一个好的情况下,你只是浪费FPGA资源,在一个糟糕的情况下,综合不能将你的代码转换为FPGA中的原语。例如。具有匹配复位行为的触发器。

正确实施:

stp

我认为现在,您有足够的输入来修复您的代码。