Motion FPGA
Motion FPGA for the MachX02-7000HE Breakout Board
pwm.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 --! @file
3 --! @brief PWM module
4 -------------------------------------------------------------------------------
5 
6 --! Using IEEE library
7 LIBRARY ieee;
8 
9 --! Using IEEE standard logic components
10 USE ieee.std_logic_1164.ALL;
11 
12 --! Using IEE standard numeric components
13 USE ieee.numeric_std.ALL;
14 
15 --! @brief PWM entity
16 --!
17 --! @image html pwm_entity.png "PWM Entity"
18 --!
19 --! This entity is a configurable PWM generator. The 'count_max' determines
20 --! how high the PWM will count to before rolling over. The 'pwm_duty' input
21 --! determines when the PWM will turn off in the cycle.
22 --!
23 --! A pwm_duty of '0' causes the pwm to stay low all the time; where-as a
24 --! pwm_duty of 'count_max + 1' causes the pwm to stay high all the time.
25 ENTITY pwm IS
26  GENERIC (
27  bit_width : natural RANGE 2 TO 32 := 8 --! PWM width
28  );
29  PORT (
30  clk_in : IN std_logic; --! Clock
31  rst_in : IN std_logic; --! Asynchronous reset
32  pwm_adv_in : IN std_logic; --! PWM Advance flag
33  pwm_duty_in : IN std_logic_vector(bit_width - 1 DOWNTO 0); --! PWM duty cycle
34  pwm_out : OUT std_logic --! PWM output
35  );
36 END ENTITY pwm;
37 
38 --! Architecture rtl of pwm entity
39 ARCHITECTURE rtl OF pwm IS
40 
41  --! Maximum PWM count
42  CONSTANT c_count_max : natural := (2 ** bit_width) - 2;
43 
44  --! PWM count
45  SIGNAL count : unsigned(pwm_duty_in'range);
46 
47  --! PWM state
48  SIGNAL state : std_logic;
49 
50 BEGIN
51 
52  --! @brief Process for PWM generation
53  pr_pwm : PROCESS (clk_in, rst_in) IS
54  BEGIN
55 
56  IF (rst_in = '1') THEN
57  -- Reset state
58  count <= (OTHERS => '0');
59  state <= '0';
60  ELSIF (rising_edge(clk_in)) THEN
61  IF (pwm_adv_in = '1') THEN
62  -- Drive state
63  IF (count < unsigned(pwm_duty_in)) THEN
64  state <= '1';
65  ELSE
66  state <= '0';
67  END IF;
68 
69  -- Increment count
70  IF (count = c_count_max) THEN
71  count <= (OTHERS => '0');
72  ELSE
73  count <= count + 1;
74  END IF;
75  END IF;
76  END IF;
77 
78  END PROCESS pr_pwm;
79 
80  pwm_out <= state;
81 
82 END ARCHITECTURE rtl;
in rst_instd_logic
Asynchronous reset.
Definition: pwm.vhd:31
unsigned(pwm_duty_in'range ) count
PWM count.
Definition: pwm.vhd:45
PWM entity.
Definition: pwm.vhd:25
in pwm_duty_instd_logic_vector( bit_width- 1 DOWNTO 0)
PWM duty cycle.
Definition: pwm.vhd:33
_library_ ieeeieee
Using IEEE library.
Definition: level_filter.vhd:7
std_logic state
PWM state.
Definition: pwm.vhd:48
bit_widthnatural range 2 TO 32:= 8
PWM width.
Definition: pwm.vhd:28
in pwm_adv_instd_logic
PWM Advance flag.
Definition: pwm.vhd:32
in clk_instd_logic
Clock.
Definition: pwm.vhd:30
natural :=( 2** bit_width)- 2 c_count_max
Maximum PWM count.
Definition: pwm.vhd:42
out pwm_outstd_logic
PWM output.
Definition: pwm.vhd:35