Counter.vhd


1
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.numeric_std.all;
4
entity counter is 
5
  generic (  n_Bit              : natural:= 10);
6
           
7
  port( clk_in, n_reset       : in std_logic;
8
          Upwm0             : in std_logic_vector(n_Bit-6 downto 0);      -- 5 Bit for parallel load
9
    Upwm1             : in std_logic_vector(n_Bit-1 downto 4);     -- 5 Bit for parallel load
10
    Upwm0_out         : out std_logic
11
);          
12
        
13
end entity counter;
14
15
architecture counter_a of counter is
16
 signal  pwm0     :unsigned(n_Bit-6 downto 0):= (others=>'0');
17
 signal  pwm1     :unsigned(n_Bit-1 downto 4):= (others=>'0');
18
 signal  pwm_control  :std_logic:='0';
19
 signal  pwm    :std_logic:='0';
20
21
begin
22
  count: process(clk_in,n_reset)--sensitivity list
23
  begin
24
     if(n_reset = '0') then
25
          pwm0 <= unsigned(Upwm0);
26
    pwm_control <= '0';
27
28
    else
29
        if(rising_edge(clk_in)) then
30
                if(pwm0 < to_unsigned(31,n_Bit))  then
31
      pwm0 <= (pwm0 + 1);
32
      pwm_control <= '1';
33
    else if(pwm0 > to_unsigned(0,n_Bit))then
34
      pwm0 <= (pwm0 - 1);
35
      pwm_control <='0';
36
       end if;--pwm0 fall
37
    end if; -- pwm0 rise
38
          end if;   -- rising_edge              
39
      end if;       -- reset      
40
    end process count;
41
  
42
  pwm_logic:process(clk_in)
43
  begin
44
    if(rising_edge(clk_in)) then
45
      if(pwm0 => Upwm0) then
46
      pwm <= '1';
47
      else 
48
      pwm <= '0';
49
    end if;
50
    end if;
51
   end process pwm_logic;  
52
output:
53
Upwm0_out <=  std_logic(pwm);
54
55
56
end architecture counter_a;