1

I'm trying to code for a simple counter (0 to 9) displaying on a seven segment display. The way it increments is via a switch - going from a logic 0 to logic 1 which increments it by 1. There is also a rest capability which is meant to reset the counter to 0.

I have looked on here and got the majority of my code sorted. I have simulated this on ModelSim when compiling and it works as expected. However when I download the code to my DE0 board, it doens't work - I can't even describe what it does as it's so random and there's no discernible pattern (random LEDs will light up, random numbers will appear etc.).

The reset (which sets the counter to 0 and outputs a 0 to the 7-segment) works fine though!

Here's my code so far:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity Lab_1 is port (
  switch : in  std_logic;
  rst    : in  std_logic;
  sseg   : out std_logic_vector(7 downto 0));
end Lab_1;

architecture top of Lab_1 is
  signal counter : unsigned (3 downto 0) := "0000";
begin

  display : process(switch, rst) is
  begin
    if rst = '1' and rst'last_value = '0' then
      counter <= "0000";
    elsif switch = '1' and switch'last_value = '0' then
      counter <= counter + 1;
    end if;
    case counter is
      when "0000" => sseg <= "11000000";
      when "0001" => sseg <= "11111001";
      when "0010" => sseg <= "10100100";
      when "0011" => sseg <= "10110000";
      when "0100" => sseg <= "10011001";
      when "0101" => sseg <= "10010010";
      when "0110" => sseg <= "10000010";
      when "0111" => sseg <= "11111000";
      when "1000" => sseg <= "10000000";
      when "1001" => sseg <= "10010000";
      when others => sseg <= "11111111";
    end case;
  end process;

end top;

Any help would be appreciated as to what is going wrong?

EDIT:

This from an assignment I am doing for my university course. The question actually wants a way to keep score for two "teams" by way of pushing a button/switch and then resetting it. I figured if I could have got a simple counter working then I could easily (I hope!) by way of keeping score for 2 teams.

The DE0 board does have buttons which are debounced - but when I altered the code so that it used the buttons, the 7-segment would display a random value/pattern WHILE the button was pressed and then change to another random value/pattern when the button was let go.

4

1 回答 1

1

关于如何继续前进的一些建议:

  1. 设计中没有时钟,但出于以下几个原因需要时钟,因此请根据 DE0 参考板上的可用时钟在端口列表中添加时钟。

  2. 在时钟的上升沿进行时钟更新以更新计数器。

  3. 开关输入通常会产生一定量的弹跳,请参阅 触点弹跳 通常发生。因此,必须将去抖动器应用于联系人,请参阅 去抖动。deboundes 开关switch_db如下所述。时钟也用于此目的。

  4. 如代码中所写, VHDL 属性'last_value不能用于边缘检测。而是通过使用触发器制作一个将开关信号延迟一个周期的版本,然后检查当前和先前的值,例如(switch_db = '1') and (switch_db_delay_ = '0'). VHDL 属性'last_value通常是测试台功能,因此仅用于仿真。时钟也用于此目的。

  5. 转换为counter7 段值可以在单独的过程中进行,以避免敏感度列表处理问题,否则可能会在综合工具中生成警告。

于 2014-10-09T16:56:12.643 回答