【问题标题】:VHDL: up/down counter with two buttonsVHDL:带有两个按钮的加/减计数器
【发布时间】:2016-05-21 14:41:24
【问题描述】:

我正在尝试制作一个四位向上/向下模10 计数器。 Button1 - 向上计数,Button2 - 向下计数。我正在尝试使用rising_edge 命令来执行此操作,但是对于我无法使用按钮定义的两个信号被按下。所以在下一个版本的程序中,我想使用 if 语句检测按钮。

library IEEE;
use IEEE.std_logic_1164.all, IEEE.numeric_std.all;

ENTITY counter is
generic (n:natural :=4);
port(
button1:        in std_logic;
button2:        in std_logic;
clear:  in std_logic;
C                               : out std_logic;
OUT1                            : out std_logic_vector(n-1 downto 0)
);
END counter;

ARCHITECTURE beh of counter is
begin

p0:process (button1, clear) is
variable count: unsigned (n-1 downto 0);
begin
        if clear = '1' then
        count:= (others=>'0');
                elsif button1='1' then
                count:=count+1;
                        elsif count=10 then
                                count:=(others=>'0');
                                C<='1';
                                else C<='0';
                        end if;

            if button2='1' then
                  count:=count-1;
                  if count=0 then
                        count:=(others=>'1');
                        C<='1';
                        else C<='0';
                  end if;
                  end if;
        OUT1<=std_logic_vector(count);
end process p0;
end architecture beh;

在 Quartus 程序中编译没有错误,但在仿真中不起作用。 我会非常乐意提供帮助。 :)

【问题讨论】:

  • 尝试在您的敏感度列表中添加button2
  • 打算用这个来编程和FPGA吗?如果是这样,那么你离你需要的地方还有很长的路要走。
  • 我没有 FPGA 开发套件,所以虽然我现在想运行仿真。我将 button2 添加到灵敏度列表中,但模拟仍然不起作用。
  • 您永远无法使用此代码对 FPGA 进行编程。计数器是一个顺序电路:任何硬件计数器都需要一个时钟。因此,您拥有的是 VHDL 中的一些任意“程序”,您可能会或可能无法开始工作。但是 VHDL 是一种HW 描述语言,而不是一种软件编程语言,所以我猜你在某些时候是针对硬件的。所以,我建议你谷歌“vhdl counter”看看它是怎么做的,也看看我的回答here

标签: vhdl counter quartus


【解决方案1】:

你应该使用时钟信号来使用rising_edge,我在你的实体中创建了一个时钟信号:

clock : in std_ulogic;

在此之后,您应该在您的进程中放入对 CLOCK 信号和 button2 信号敏感的进程,如下所示:

p0:process (button1, button2, clear, clock) is

我在这种情况下的模拟工作正常,当我按下按钮 1 时计数会增加,当我按下按钮 2 时计数会减少。

完整的架构:

ARCHITECTURE beh of counter is
begin

p0:process (button1, button2, clear, clock) is
variable count: unsigned (n-1 downto 0);
begin
        if rising_edge(clock) then
            if clear = '1' then
                count:= (others=>'0');
            end if;
            if (button1='1') then
                count:=count+1;
            elsif (count=10) then
                count:=(others=>'0');
                C<='1';
            else 
                C<='0';
            end if;

            if (button2='1') then
                  count:=count-1;
            if count=0 then
                  count:=(others=>'1');
                  C<='1';
            else 
            C<='0';
            end if;
            end if;
         end if;
        OUT1<=std_logic_vector(count);
end process p0;
end architecture beh;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多