我的代码中有以下按钮按下逻辑。我尝试过使用等待延迟来消除它的抖动,但是编译器不允许这样做。我的FPGA上有四个按钮,下面的"key“数组反映了这些按钮:
process(clock)
begin
if rising_edge(clock) then
if(key(3)/='1' or key(2)/='1' or key(1)/='1' or key(0)/='1') then --MY ATTEMPT AT DEBOUNCING
wait for 200 ns; ----MY ATTEMPT AT DEBOUNCING
if (key(3)='1' and key(2)='1' and key(1)='0' and last_key_state="1111" and key(0)='1') then
...
elsif (key(3)='1' and key(2)='1' and key(1)='1' and key(0)='0' and last_key_state="1111") then
...
elsif (key(3)='0' and key(2)='1' and key(1)='1' and key(0)='1' and last_key_state="1111") then
...
elsif (key(3)='1' and key(2)='0' and key(1)='1' and key(0)='1' and last_key_state="1111") then
...
end if;
last_key_state<=key;
end if;
end if;
end process;有没有人能给出一些非常简单的示例代码来说明我如何揭穿像上面这样的设置?
发布于 2013-01-30 16:46:07
你得到的错误是因为等待...等待是不可合成的。
我会用一个简单的计数器来做。因此,您可以通过调整计数器,对不同的时钟速度使用相同的代码。
-- adjust the counter to you special needs
-- depending on how good your buttons are hardware debounced
-- you can easily think in ms
signal counter : std_logic_vector(7 DOWNTO 0) := "10000000";
process
begin
if rising_edge(clock) then --You're clock
if(key(3) = '0') or (key(2) = '0') or (key(1) = '0') or (key(0) = '0') then
start_debouncing <= '1';
key_vector_out <= key(3) & key(2) & key(1) & key(0);
end if;
if(start_debouncing = '1') then
key_vector_out <= "0000";
counter <= std_logic_vector(unsigned(counter) - 1);
end if;
if(counter = "00000000") then
counter <= "10000000";
start_debouncing <= '0';
end if;
end process;您的代码可能会产生另一个问题。如果您的按钮被释放,那么您的输入将会发生什么..key = "0000“..对,你永远不会得到你的输出。也许100次中有99次是有效的,但你可能会得到一个很难找到的错误。
发布于 2013-01-30 08:52:05
嗯,如果你考虑如何用真正的电子设备来做这件事,你可能会使用电容器。它有一个充电时间。同样的想法也适用于这里,只要计算出开关跳动的时间(通常是时钟速度的函数),然后实际设置寄存器即可。
Simple Example With a 4-Bit Shift Register
所以你可以把它放在你的交换机和其他逻辑模块之间
process
begin
if rising_edge(clock) then --You're clock
SHIFT_PB(2 Downto 0) <= SHIFT_PB(3 Downto 1); --Shifting each cycle
SHIFT_PB(3) <= NOT PB; --PB is the pre-bounced signal
If SHIFT_PB(3 Downto 0)="0000" THEN --once the bounce has settled set the debounced value
PB_DEBOUNCED <= '1';
ELSE
PB_DEBOUNCED <= '0';
End if;
end process;它基本上延迟了你的信号4个时钟周期(这就是你想要做的等待)。
发布于 2013-01-31 00:33:49
其他人已经用计数器指明了方向……在将信号馈送到计数器之前,您还需要将信号与时钟同步,否则,信号偶尔会在不同的时间到达计数器的不同部分,计数器将无法正确计数。
这是否重要取决于应用程序-如果正确的操作很重要,那么正确的同步也很重要!
https://stackoverflow.com/questions/14594845
复制相似问题