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

Subversion Repositories pwm_with_dithering

[/] [pwm_with_dithering/] [trunk/] [src/] [pwm_ineq.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:    15:31:34 01/27/2021 
6
-- Design Name:          PWM
7
-- Module Name:    pwm_ineq - Behavioral 
8
-- Target Devices: None / non-specific
9
-- Tool versions:  None / non-specific
10
-- Description:          Dithered PWM. Cycle ends with inequality operation. Clock
11
--                                               frequency is not maximal. Not sensitive to input 
12
--                                               changes during operation.
13
--
14
-- Revision: 
15
-- Revision 0.01 - File Created
16
-- Additional Comments: 
17
--
18
----------------------------------------------------------------------------------
19
library IEEE;
20
use IEEE.STD_LOGIC_1164.ALL;
21
use IEEE.NUMERIC_STD.ALL;
22
use IEEE.STD_LOGIC_UNSIGNED.ALL;
23
 
24
entity pwm_ineq is
25
    Generic (
26
                                bits: integer:=16;
27
                                dithering:integer:=5
28
                                );
29
    Port ( clk : in  STD_LOGIC;
30
           set : in  STD_LOGIC_VECTOR(bits-1 downto 0);
31
           o   : out  STD_LOGIC);
32
end pwm_ineq;
33
 
34
architecture Behavioral of pwm_ineq is
35
signal o_i:std_logic:='0';
36
signal cnt:std_logic_vector(bits-1 downto 0):=(others => '0');
37
signal zeros:std_logic_vector((bits-dithering)-1 downto 0):=(others => '0');
38
signal target:std_logic_vector((bits-dithering) downto 0);
39
signal reversed_cnt_top:std_logic_vector((dithering)-1 downto 0);
40
signal inc:std_logic;
41
 
42
function reverse_and_rebase_bit_order(a: in std_logic_vector)
43
return std_logic_vector is
44
  variable result: std_logic_vector(a'high-a'low downto 0);
45
begin
46
  for i in a'RANGE loop
47
    result(a'high-i) := a(i);
48
  end loop;
49
  return result;
50
end;
51
 
52
begin
53
 
54
-- output mapping:
55
o <= o_i;
56
 
57
zeros <= (others => '0');
58
 
59
normal: if dithering = 0  generate
60
doit:process(clk)
61
begin
62
if rising_edge(clk) then
63
        cnt <= cnt + 1;
64
        o_i <= o_i;
65
        if cnt=zeros then o_i <= '1'; end if;
66
        if cnt>=set then o_i <= '0'; end if;
67
end if;
68
end process;
69
end generate;
70
 
71
dithered: if dithering > 0  generate
72
 
73
reversed_cnt_top <= reverse_and_rebase_bit_order(cnt(bits-1 downto bits-dithering));
74
inc <= '1' when (reversed_cnt_top < set(dithering-1 downto 0)) else '0';
75
target <= ('0' & set(bits-1 downto dithering)) + inc;
76
 
77
doit:process(clk)
78
begin
79
if rising_edge(clk) then
80
        cnt <= cnt + 1;
81
        o_i <= o_i;
82
        if cnt((bits-dithering)-1 downto 0) = zeros then o_i <= '1'; end if;
83
        if ('0' & cnt((bits-dithering)-1 downto 0)) >= target then o_i <= '0'; end if;
84
end if;
85
end process;
86
end generate;
87
 
88
end Behavioral;
89
 

powered by: WebSVN 2.1.0

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