【发布时间】:2014-09-29 12:09:29
【问题描述】:
我是 VHDL 新手,我想为编码器信号编写一个简单的计数器,每 100 个周期计数 (duh) 到 1000。我正在使用 Lattice ispMACH 4000ZE Pico DevKit 和软件 ispLEVER Classic Project Navigator 进行编程。
我的代码是:
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use IEEE.std_logic_unsigned.all;
entity Counter_10B is
port (
reset : in std_ulogic := '0';
updown : in std_ulogic := '0';
clk : in std_ulogic := '0';
cnt_out : out std_ulogic_vector(9 downto 0) := (others => '0')
);
end Counter_10B;
architecture behavior of Counter_10B is
signal int_cnt : unsigned(9 downto 0) := (others => '0');
begin
Counter : process(clk, updown, reset)
variable temp1 : unsigned(4 downto 0) := (others => '0');
variable temp2 : unsigned(9 downto 0) := (others => '0');
begin
if (reset = '0') then
temp2 := (others => '0');
end if;
if ((clk'event AND clk='1')) then
temp1 := temp1 + 1;
if(temp1 >= 100) then
if(updown = '0') then
if(temp2 >= 1000) then
temp2 := (others => '0');
end if;
temp2 := temp2 + 1;
elsif(updown = '1') then
if(temp2 = 0) then
temp2 := (others => '1');
end if;
temp2 := temp2 - 1;
end if ;
temp1 := 0;
end if;
int_cnt <= temp2;
end if;
end process;
cnt_out <= std_ulogic_vector(int_cnt);
end behavior;
我的问题是每次编译都没有编译错误,但是有很多警告,比如:
Counter.temp2(0) 的所有可达分配赋值为 '0';寄存器被优化移除
或
修剪 int_cnt(9 downto 0) 的寄存器位 9 到 1
但我最大的问题是:
输入 clk/reset/updown 未使用
我真的需要关于最后一个警告的帮助,我不明白为什么会出现这个错误。这没有任何意义,不是吗?
谢谢!
编辑 30.09
Counter : process(clk, updown, reset)
variable temp1 : natural range 0 to 100 := 0;
begin
if (reset = '0') then
int_cnt <= (others => '0');
elsif ((clk'event AND clk='1')) then
temp1 := temp1 + 1;
if(temp1 >= 100) then
if(updown = '0') then
if(int_cnt >= 1000) then
int_cnt <= (others => '0');
end if;
int_cnt <= int_cnt + 1;
elsif(updown = '1') then
if(int_cnt = 0) then
int_cnt <= 1000;
end if;
int_cnt <= int_cnt - 1;
end if ;
temp1 := 0;
end if;
end if;
end process;
cnt_out <= std_ulogic_vector(int_cnt);
【问题讨论】:
-
请注意,即使您修复了 temp2 没有足够宽的范围,您也会遇到逻辑错误。如果您希望翻转边界为 1000,则计数器下溢应该以 1000 的值结束,而不是 1023,因为
(others => '1')将产生。您还将变量视为信号 - 每次翻转时,您还将减去或添加一个意味着您从 1000 滚动到 1,而不是 1000 到 0,因为您多次分配变量。无论如何我都会在这里使用信号,但是如果你使用变量,你必须记住它们会随着每次赋值而改变。