|
发现一些同仁提出上升沿和下降沿计数的问题,工作中也碰到一些同事问及此问题。现在我把我多年来一直采用的办法奉上,但愿对初学者有所帮助。
以一个最简单的计数器为例:
|
Port( clock:in std_logic; pulse:in std_logic; q: out std_logic_vector(3 downto 0) );
--q输出为对pulse跳变沿的递增计数。clock为系统高速时钟。
Process(clock) begin if rising_edge(clock) then dly1pul <= pulse; dly2pul <= dly1pul; end if; End process;
en <= dly1pul and not dly2pul; --上升沿 --en <= not dly1pul and dly2pul;--下降沿 --en <= dly1pul xor dly2pul; --上升沿和下降沿
Process(clock) begin if rising_edge(clock) then if en = '1' then cnt <= cnt + 1; end if; end if; End process;
q <= cnt; |
单对于此小问题,当然采用倍频实现双沿计数也是可行的,但是我们不要忘记,倍频器在很多CPLD或FPGA中是不支持的,即便支持其资源也是很宝贵的。
我看到的一些设计中,动辄采用某一信号作为时钟,应该说这种做法是欠妥的。因为不是全局时钟的时钟信号最大扇出是有限的,其很难保证时钟延时应小于信号延时的基本要求。当遇到要对某个信号的跳变沿处理时,建议采用上述小例子中en信号的处理办法。
|