在VHDL中计算特定时间段内的边数
对于电动机的速度测量,我想计算10毫秒时间间隔内编码器输入的上升沿和下降沿的数量 为此,我为我的40 MHz时钟实现了一个时钟分频器,如下所示:在VHDL中计算特定时间段内的边数,vhdl,counter,reset,encoder,Vhdl,Counter,Reset,Encoder,对于电动机的速度测量,我想计算10毫秒时间间隔内编码器输入的上升沿和下降沿的数量 为此,我为我的40 MHz时钟实现了一个时钟分频器,如下所示: entity SpeedCLK is Port ( CLK : in STD_LOGIC; CLKspeed : out STD_LOGIC); end SpeedCLK; architecture Behavioral of SpeedCLK is signal CounterCLK : NATURAL
entity SpeedCLK is
Port ( CLK : in STD_LOGIC;
CLKspeed : out STD_LOGIC);
end SpeedCLK;
architecture Behavioral of SpeedCLK is
signal CounterCLK : NATURAL range 0 to 400001;
begin
SpeedCounter : process
begin
wait until rising_edge(CLK);
CounterCLK <= CounterCLK + 1;
if CounterCLK < 400000 then
CLKspeed <= '0';
else
CLKspeed <= '1';
CounterCLK <= 0;
end if;
end process SpeedCounter;
end Behavioral;
这应该每隔10毫秒将CountQEPA
的当前值写入我的变量edgesPerTime
,然后重置计数器。
信号速度
通过uart传输。不幸的是,每10毫秒重置一次CountQEPA
,我就会收到一个EdgesPerTime
的常量值0。如果我删除代码中的重置行,我可以收到EdgesPerTime
的递增值,直到达到整数的最大值(2^16),此时计数器重置为0
在VHDL中,在设定的时间段内计算上升沿和下降沿的正确实现是什么
非常感谢您的帮助,因为我对vhdl还是很陌生。您正在使用变量构建闩锁。您的合成工具推断出
countQEPA
的异步重置CLKSpeed
,并且由于您的闩锁EdgesPerTime
闩锁countQEPA
在其写入启用CLKSpeed
被断言时,您只会看到0
的重置countQEPA
值
您应该构建适当的同步逻辑:
signal CountQEPA : integer := 0;
signal detect : std_logic_vector(5 downto 0) := (others => '0');
begin
edge_cnt_p : process (
CLK
)
begin
if (rising_edge(CLK)) then
-- synchronous reset
if (CLKSpeed = '1') then
countQEPA <= 0;
elsif ((detect = "011111") or (detect = "100000")) then
countQEPA <= countQEPA + 1;
end if;
end if;
end process edge_cnt_p;
edge_debounce_p : process (
CLK
)
begin
if (rising_edge(CLK)) then
detect <= detect(detect'left downto 1) & QEPA;
end if;
end process edge_debounce_p;
count_register_p : process (
CLK
)
begin
if (rising_edge(CLK)) then
-- synchronous write enable
if (CLKSpeed = '1') then
Speed <= countQEPA;
end if;
end if;
end process count_register_p;
信号计数qepa:integer:=0;
信号检测:标准逻辑向量(5到0):=(其他=>“0”);
开始
边缘控制:过程(
时钟
)
开始
如果(上升沿(CLK)),则
--同步复位
如果(CLKSpeed='1'),则
countQEPA 2**16不是整数的最大值。非常感谢,该代码看起来干净多了。但我想知道这两个进程对CLKSpeed的反应到底会发生什么。On重置计数器,并将计数器值写入信号速度。是否保证在重置发生后不会写入速度?编码器是正交的,但atm i仅使用一条线进行速度测量。我也有过这样的印象:变量只能在流程内部使用,你似乎在架构部分声明过?对去盎司移位寄存器使用信号可以吗?变量是复制粘贴错误。现在都是信号了。寄存器是同步的。您的合成工具将确保时钟边缘几乎在同一时间到达,以便保持设置和保持时间。由于它现在是同步的,所以可以保证在复位前读取countQEPA。
signal CountQEPA : integer := 0;
signal detect : std_logic_vector(5 downto 0) := (others => '0');
begin
edge_cnt_p : process (
CLK
)
begin
if (rising_edge(CLK)) then
-- synchronous reset
if (CLKSpeed = '1') then
countQEPA <= 0;
elsif ((detect = "011111") or (detect = "100000")) then
countQEPA <= countQEPA + 1;
end if;
end if;
end process edge_cnt_p;
edge_debounce_p : process (
CLK
)
begin
if (rising_edge(CLK)) then
detect <= detect(detect'left downto 1) & QEPA;
end if;
end process edge_debounce_p;
count_register_p : process (
CLK
)
begin
if (rising_edge(CLK)) then
-- synchronous write enable
if (CLKSpeed = '1') then
Speed <= countQEPA;
end if;
end if;
end process count_register_p;
debounce QEPA
111111 1
111111 0 <-- glitch/bounce on encoder line; 25 ns wide
111110 1
111101 1
111011 1
110111 1
101111 1
011111 1 <-- fake edge detected