VHDL中如何表示在一段时间内仅第一个信号有效?
2个回答
展开全部
一般消抖电路用多次采样,再比较的方法实现。下面给个例子,每1毫秒采样一次,把最后10次采样值存下来,如果都一致,再翻转。如果需要更长时间的消抖,可以加大采样间隔,比如每10毫秒采样一次,比较最后5次采样值就可以消除50ms的抖动。
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
entity debounce is
generic (
CLK_FREQ_MHz : integer := 20; --in MHz
BUTTON_PRESS_STATUS : std_logic := '0'
);
port (
reset_n : in std_logic;
clk : in std_logic;
btnIn : in std_logic;
btnPressed : out std_logic
);
end debounce;
architecture debounce_arch of debounce is
constant MAX_MS_CNT : integer := CLK_FREQ_MHz * 1000 - 1;
signal msCnt : integer range 0 to MAX_MS_CNT;
signal msClk : std_logic; --做一个毫秒脉冲,每1毫秒对按钮采样一次
signal btnIn_q : std_logic_vector(9 downto 0); --记住最后10次采样
signal btn : std_logic;
signal btn_q : std_logic;
begin
--产生毫秒脉冲
process(reset_n, clk)
begin
if reset_n = '0' then
msCnt <= 0;
msClk <= '0';
elsif rising_edge(clk) then
if msCnt >= MAX_MS_CNT then
msCnt <= 0;
msClk <= '1';
else
msCnt <= msCnt + 1;
msClk <= '0';
end if;
end if;
end process;
--记住最后10次采样
process(reset_n, clk)
begin
if reset_n = '0' then
btnIn_q <= (others => not BUTTON_PRESS_STATUS);
elsif rising_edge(clk) then
if msClk = '1' then
btnIn_q <= btnIn_q(btnIn_q'left-1 downto 0) & btnIn;
else
btnIn_q <= btnIn_q;
end if;
end if;
end process;
process(reset_n, clk)
variable all_samples_are_pressed : std_logic_vector(btnIn_q'left downto 0) := (others => BUTTON_PRESS_STATUS);
begin
if reset_n = '0' then
btn <= '0';
btn_q <= '0';
elsif rising_edge(clk) then
if btnIn_q = all_samples_are_pressed then
btn <= '1'; --最后10次采样都是按下状态,就确认按钮按下(10ms消抖)
elsif btnIn_q = not all_samples_are_pressed then
btn <= '0'; --最后10次采样都是抬起状态,就确认按钮抬起(10ms消抖)
else
btn <= btn; --否则保持不变
end if;
btn_q <= btn;
end if;
end process;
btnPressed <= '1' when btn = '1' and btn_q = '0' else '0'; --按钮按下上升沿检测
end debounce_arch;
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
entity debounce is
generic (
CLK_FREQ_MHz : integer := 20; --in MHz
BUTTON_PRESS_STATUS : std_logic := '0'
);
port (
reset_n : in std_logic;
clk : in std_logic;
btnIn : in std_logic;
btnPressed : out std_logic
);
end debounce;
architecture debounce_arch of debounce is
constant MAX_MS_CNT : integer := CLK_FREQ_MHz * 1000 - 1;
signal msCnt : integer range 0 to MAX_MS_CNT;
signal msClk : std_logic; --做一个毫秒脉冲,每1毫秒对按钮采样一次
signal btnIn_q : std_logic_vector(9 downto 0); --记住最后10次采样
signal btn : std_logic;
signal btn_q : std_logic;
begin
--产生毫秒脉冲
process(reset_n, clk)
begin
if reset_n = '0' then
msCnt <= 0;
msClk <= '0';
elsif rising_edge(clk) then
if msCnt >= MAX_MS_CNT then
msCnt <= 0;
msClk <= '1';
else
msCnt <= msCnt + 1;
msClk <= '0';
end if;
end if;
end process;
--记住最后10次采样
process(reset_n, clk)
begin
if reset_n = '0' then
btnIn_q <= (others => not BUTTON_PRESS_STATUS);
elsif rising_edge(clk) then
if msClk = '1' then
btnIn_q <= btnIn_q(btnIn_q'left-1 downto 0) & btnIn;
else
btnIn_q <= btnIn_q;
end if;
end if;
end process;
process(reset_n, clk)
variable all_samples_are_pressed : std_logic_vector(btnIn_q'left downto 0) := (others => BUTTON_PRESS_STATUS);
begin
if reset_n = '0' then
btn <= '0';
btn_q <= '0';
elsif rising_edge(clk) then
if btnIn_q = all_samples_are_pressed then
btn <= '1'; --最后10次采样都是按下状态,就确认按钮按下(10ms消抖)
elsif btnIn_q = not all_samples_are_pressed then
btn <= '0'; --最后10次采样都是抬起状态,就确认按钮抬起(10ms消抖)
else
btn <= btn; --否则保持不变
end if;
btn_q <= btn;
end if;
end process;
btnPressed <= '1' when btn = '1' and btn_q = '0' else '0'; --按钮按下上升沿检测
end debounce_arch;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询