以一个计数器为例:
1
--------------------------------------------------------------------------------
2 -- Created by : Vorx Ltd.com
3 -- Filename : edge.vhd
4 -- Author : ChenYong
5 -- Created On : 2010 - 11 - 02 13 : 17
6 -- Last Modified : 2010 - 11 - 02 13 : 51
7 -- Update Count : 2010 - 11 - 02 13 : 17
8 -- Description : 关于上升沿和下降沿触发的讨论
9 -- q 输出为对 pulse 跳变沿的递增计数。clock 为系统高速时钟。
10 --
11 --------------------------------------------------------------------------------
12
13 library ieee;
14 use ieee.std_logic_1164. all ;
15 use ieee.std_logic_arith. all ;
16 use ieee.std_logic_unsigned. all ;
17
18 entity edge is
19 port
20 (
21 clock: in std_logic ;
22 pulse: in std_logic ;
23 q: out std_logic_vector ( 3 downto 0 )
24 );
25 end prot;
26
27 architecture arc of edge is
28
29 signal dly1pul : std_logic ;
30 signal dly2pul : std_logic ;
31 signal en : std_logic ;
32 signal cnt : std_logic_vector ( 3 downto 0 ): = ( others => ' 0 ' );
33
34 begin
35 Process(clock) begin
36 if rising_edge (clock) then
37 dly1pul <= pulse;
38 dly2pul <= dly1pul;
39 end if ;
40 End process ;
41
42 en <= dly1pul and not dly2pul; -- 上升沿
43 -- en <= not dly1pul and dly2pul; -- 下降沿
44 -- en <= dly1pul xor dly2pul; -- 上升沿和下降沿
45
46 Process(clock) begin
47 if rising_edge (clock) then
48 if en = ' 1 ' then
49 cnt <= cnt + 1 ;
50 end if ;
51 end if ;
52 End process ;
53
54 q <= cnt;
55
56 end arc;
57
2 -- Created by : Vorx Ltd.com
3 -- Filename : edge.vhd
4 -- Author : ChenYong
5 -- Created On : 2010 - 11 - 02 13 : 17
6 -- Last Modified : 2010 - 11 - 02 13 : 51
7 -- Update Count : 2010 - 11 - 02 13 : 17
8 -- Description : 关于上升沿和下降沿触发的讨论
9 -- q 输出为对 pulse 跳变沿的递增计数。clock 为系统高速时钟。
10 --
11 --------------------------------------------------------------------------------
12
13 library ieee;
14 use ieee.std_logic_1164. all ;
15 use ieee.std_logic_arith. all ;
16 use ieee.std_logic_unsigned. all ;
17
18 entity edge is
19 port
20 (
21 clock: in std_logic ;
22 pulse: in std_logic ;
23 q: out std_logic_vector ( 3 downto 0 )
24 );
25 end prot;
26
27 architecture arc of edge is
28
29 signal dly1pul : std_logic ;
30 signal dly2pul : std_logic ;
31 signal en : std_logic ;
32 signal cnt : std_logic_vector ( 3 downto 0 ): = ( others => ' 0 ' );
33
34 begin
35 Process(clock) begin
36 if rising_edge (clock) then
37 dly1pul <= pulse;
38 dly2pul <= dly1pul;
39 end if ;
40 End process ;
41
42 en <= dly1pul and not dly2pul; -- 上升沿
43 -- en <= not dly1pul and dly2pul; -- 下降沿
44 -- en <= dly1pul xor dly2pul; -- 上升沿和下降沿
45
46 Process(clock) begin
47 if rising_edge (clock) then
48 if en = ' 1 ' then
49 cnt <= cnt + 1 ;
50 end if ;
51 end if ;
52 End process ;
53
54 q <= cnt;
55
56 end arc;
57
一些设计中,动辄采用某一信号作为时钟,应该说这种做法是欠妥的。因为不是全局时钟的时钟信号最大扇出是有限的,其很难保证时钟延时应小于信号延时的基本要求。当遇到要对某个信号的跳变沿处理时,建议采用上述小例子中 en 信号的处理办法。