I am designing an up down counter that has a modulo value that can be programmed so that value is the highest it can reach to count up to or down from. I want it so that only if I reset the code the modulo value is loaded. So if I change it and don't reset the old modulo value will remain. Also with my counter code I want to implement a clock divider that I have been given and need to use. Here are my codes: This is the code of the counter with the clock divider as a component within it:
1 | library IEEE; |
2 | use IEEE.STD_LOGIC_1164.ALL; |
3 | use IEEE.STD_LOGIC_ARITH.ALL; |
4 | use IEEE.STD_LOGIC_UNSIGNED.ALL; |
5 | |
6 | entity counter4bit_modproc is |
7 | GENERIC(LIMIT : integer := 2); |
8 | port( |
9 | mod_value: in std_logic_vector(3 downto 0); |
10 | clock: in std_logic; |
11 | enable_count: in std_logic; |
12 | reset: in std_logic; |
13 | direction: in std_logic; |
14 | output_counter: out std_logic_vector(3 downto 0) ); |
15 | end counter4bit_modproc; |
16 | |
17 | architecture behavioral of counter4bit_modproc is |
18 | component Clock_Divider is |
19 | generic ( LIMIT : integer := 2 ); |
20 | port ( |
21 | Clock : in std_logic; |
22 | Reset : in std_logic; |
23 | Clk_Div : out std_logic); |
24 | end component; |
25 | |
26 | signal dig: std_logic_vector(3 downto 0); |
27 | signal clk: std_LOGIC; |
28 | begin
|
29 | |
30 | clkdiv: Clock_Divider generic map (LIMIT => 50000000) |
31 | port map ( |
32 | clock => Clock, |
33 | Reset => Reset, |
34 | clk_Div => clk); |
35 | |
36 | process(clk, Reset) |
37 | begin
|
38 | if Reset='1' then |
39 | dig <= "0000"; |
40 | elsif ( rising_edge(clk)) then |
41 | if (enable_count = '1') then |
42 | if (direction = '0') then |
43 | if (dig <= mod_value) then |
44 | dig <= "0000"; |
45 | --dig <= (others =>'0');
|
46 | else
|
47 | dig <= dig + 1; |
48 | end if; |
49 | elsif (dig = "0000") then |
50 | dig <= mod_value; |
51 | else
|
52 | dig <= dig - 1; |
53 | end if; |
54 | end if; |
55 | end if; |
56 | end process; |
57 | output_counter <= dig; |
58 | end behavioral; |
here is the code for the clock divider:
1 | library IEEE; |
2 | use IEEE.STD_LOGIC_1164.ALL; |
3 | use IEEE.STD_LOGIC_UNSIGNED.ALL; |
4 | |
5 | entity Clock_Divider is |
6 | GENERIC(LIMIT : integer := 2); |
7 | PORT ( |
8 | Clock : IN STD_LOGIC; |
9 | Reset : IN STD_LOGIC; |
10 | Clk_Div : OUT STD_LOGIC |
11 | );
|
12 | end Clock_Divider; |
13 | |
14 | architecture Behavioral of Clock_Divider is |
15 | signal cnt : STD_LOGIC_VECTOR(31 downto 0); |
16 | begin
|
17 | |
18 | PC: process (Clock, Reset) |
19 | begin
|
20 | if rising_edge(Clock) then |
21 | if Reset = '1' then |
22 | cnt <= (others => '0'); |
23 | Clk_Div <= '0'; |
24 | elsif cnt = LIMIT-1 then |
25 | cnt <= (others => '0'); |
26 | Clk_Div <= '1'; |
27 | else
|
28 | cnt <= cnt+1; |
29 | Clk_Div <= '0'; |
30 | end if; |
31 | end if; |
32 | end process; |
33 | |
34 | end Behavioral; |