VHDL:带两个按钮的向上/向下计数器
我正在尝试制作一个四位上/下模10计数器。按钮1-向上计数,按钮2-向下计数。我试着用上升沿命令来做,但对于两个我无法定义的信号,我按下了按钮。所以在下一版本的程序中,我希望使用if语句检测按钮VHDL:带两个按钮的向上/向下计数器,vhdl,counter,quartus,Vhdl,Counter,Quartus,我正在尝试制作一个四位上/下模10计数器。按钮1-向上计数,按钮2-向下计数。我试着用上升沿命令来做,但对于两个我无法定义的信号,我按下了按钮。所以在下一版本的程序中,我希望使用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
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;
IEEE库;
使用IEEE.std_logic_1164.all、IEEE.numeric_std.all;
实体计数器为
一般(n:自然:=4);
港口(
按钮1:在标准逻辑中;
按钮2:在标准逻辑中;
清除:在标准逻辑中;
C:输出标准逻辑;
OUT1:out标准逻辑向量(n-1向下至0)
);
末端计数器;
计数器的架构是
开始
p0:过程(按钮1,清除)为
变量计数:无符号(n-1到0);
开始
如果清除='1',则
计数:=(其他=>'0');
elsif按钮1='1'然后
计数:=计数+1;
如果计数=10,则
计数:=(其他=>'0');
C您应该使用时钟信号来使用上升沿,我在您的实体中创建了一个时钟信号:
clock : in std_ulogic;
在此之后,您应将时钟信号和按钮2信号输入过程灵敏度,如下所示:
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;
计数器的架构是
开始
p0:进程(按钮1、按钮2、清除、时钟)为
变量计数:无符号(n-1到0);
开始
如果上升沿(时钟),则
如果清除='1',则
计数:=(其他=>'0');
如果结束;
如果(按钮1='1'),则
计数:=计数+1;
elsif(计数=10)则
计数:=(其他=>'0');
C您应该使用时钟信号来使用上升沿,我在您的实体中创建了一个时钟信号:
clock : in std_ulogic;
在此之后,您应将时钟信号和按钮2信号输入过程灵敏度,如下所示:
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;
计数器的架构是
开始
p0:进程(按钮1、按钮2、清除、时钟)为
变量计数:无符号(n-1到0);
开始
如果上升沿(时钟),则
如果清除='1',则
计数:=(其他=>'0');
如果结束;
如果(按钮1='1'),则
计数:=计数+1;
elsif(计数=10)则
计数:=(其他=>'0');
CTry在灵敏度列表中添加按钮2
。是否打算使用此按钮编程和FPGA?如果是这样的话,那么你离你需要的地方还有很长的路要走。我没有FPGA开发工具包,所以尽管我现在想运行模拟。我将按钮2添加到灵敏度列表中,但模拟仍然不起作用。您永远无法使用此代码编程FPGA。计数器是一个时序电路:任何硬件计数器都需要一个时钟。所以,您拥有的是一些VHDL中的任意“程序”,您可能无法工作,也可能无法工作。但是VHDL是一种硬件描述语言,而不是软件编程语言,所以我想你的目标是在某一点上实现硬件。所以,我建议你用谷歌搜索“vhdl计数器”,看看它是如何实现的,同时也看看我的答案。下面是我实现它的方法:尝试在灵敏度列表中添加按钮2
。你打算用它来编程和FPGA吗?如果是这样的话,那么你离你需要的地方还有很长的路要走。我没有FPGA开发工具包,所以尽管我现在想运行模拟。我将按钮2添加到灵敏度列表中,但模拟仍然不起作用。您永远无法使用此代码编程FPGA。计数器是一个时序电路:任何硬件计数器都需要一个时钟。所以,您拥有的是一些VHDL中的任意“程序”,您可能无法工作,也可能无法工作。但是VHDL是一种硬件描述语言,而不是软件编程语言,所以我想你的目标是在某一点上实现硬件。所以,我建议你用谷歌搜索“vhdl计数器”,看看它是如何实现的,同时看看我的答案。下面是我如何实现它的: