OpenCores
URL https://opencores.org/ocsvn/pwm_with_dithering/pwm_with_dithering/trunk

Subversion Repositories pwm_with_dithering

[/] [pwm_with_dithering/] [trunk/] [src/] [pwm.vhd] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 TeroS
----------------------------------------------------------------------------------
2
-- Company:              Aboa Space Research Oy (ASRO)
3
-- Engineer:             Tero Säntti
4
-- 
5
-- Create Date:    14:50:27 01/27/2021 
6
-- Design Name:          PWM
7
-- Module Name:    pwm - Behavioral 
8
-- Target Devices: None / non-specific
9
-- Tool versions:  None / non-specific
10
-- Description:          Dithered PWM. Simple implementation. Minimal size, but clock
11
--                                               frequency is not maximal. Also, may be sensitive to input 
12
--                                               changes during operation. (Input value jumps to a lower value
13
--                                               than current counter value.) Fixable with additional register
14
--                                               which loaded if counter @ max value. The fix is implemented 
15
--                                               in the pipelined version.
16
--
17
-- Revision: 
18
-- Revision 0.01 - File Created
19
-- Additional Comments: 
20
--
21
----------------------------------------------------------------------------------
22
library IEEE;
23
use IEEE.STD_LOGIC_1164.ALL;
24
use IEEE.NUMERIC_STD.ALL;
25
use IEEE.STD_LOGIC_UNSIGNED.ALL;
26
 
27
entity pwm is
28
    Generic (
29
                                bits: integer:=16;
30
                                dithering:integer:=5
31
                                );
32
    Port ( clk : in  STD_LOGIC;
33
           set : in  STD_LOGIC_VECTOR(bits-1 downto 0);
34
           o   : out  STD_LOGIC);
35
end pwm;
36
 
37
architecture Behavioral of pwm is
38
signal o_i:std_logic:='0';
39
signal cnt:std_logic_vector(bits-1 downto 0):=(others => '0');
40
signal zeros:std_logic_vector((bits-dithering)-1 downto 0):=(others => '0');
41
signal target:std_logic_vector((bits-dithering) downto 0);
42
signal reversed_cnt_top:std_logic_vector((dithering)-1 downto 0);
43
signal inc:std_logic;
44
 
45
function reverse_and_rebase_bit_order(a: in std_logic_vector)
46
return std_logic_vector is
47
  variable result: std_logic_vector(a'high-a'low downto 0);
48
begin
49
  for i in a'RANGE loop
50
    result(a'high-i) := a(i);
51
  end loop;
52
  return result;
53
end;
54
 
55
begin
56
 
57
-- output mapping:
58
o <= o_i;
59
 
60
zeros <= (others => '0');
61
 
62
normal: if dithering = 0  generate
63
doit:process(clk)
64
begin
65
if rising_edge(clk) then
66
        cnt <= cnt + 1;
67
        o_i <= o_i;
68
        if cnt=zeros then o_i <= '1'; end if;
69
        if cnt=set then o_i <= '0'; end if;
70
end if;
71
end process;
72
end generate;
73
 
74
dithered: if dithering > 0  generate
75
 
76
reversed_cnt_top <= reverse_and_rebase_bit_order(cnt(bits-1 downto bits-dithering));
77
inc <= '1' when (reversed_cnt_top < set(dithering-1 downto 0)) else '0';
78
target <= ('0' & set(bits-1 downto dithering)) + inc;
79
 
80
doit:process(clk)
81
begin
82
if rising_edge(clk) then
83
        cnt <= cnt + 1;
84
        o_i <= o_i;
85
        if cnt((bits-dithering)-1 downto 0) = zeros then o_i <= '1'; end if;
86
        if ('0' & cnt((bits-dithering)-1 downto 0)) = target then o_i <= '0'; end if;
87
end if;
88
end process;
89
end generate;
90
 
91
end Behavioral;
92
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.