1 | library IEEE;
|
2 | use IEEE.std_logic_1164.ALL;
|
3 | use IEEE.numeric_std.ALL;
|
4 |
|
5 | entity lockin is
|
6 | port(
|
7 | clock_in : in std_logic;
|
8 | enable : in std_logic;
|
9 | reset : in std_logic;
|
10 | frequency_in : in std_logic_vector(31 downto 0);
|
11 | adc_in : in std_logic_vector (13 downto 0);
|
12 | sinus_in : in std_logic_vector (13 downto 0);
|
13 | cosinus_in : in std_logic_vector (13 downto 0);
|
14 | clock_out : out std_logic;
|
15 | I_out : out std_logic_vector (17 downto 0);
|
16 | Q_out : out std_logic_vector (17 downto 0)
|
17 | );
|
18 | end lockin;
|
19 |
|
20 | architecture behave of lockin is
|
21 |
|
22 | component iir_lp
|
23 | port(
|
24 | clk : IN std_logic;
|
25 | clk_enable : IN std_logic;
|
26 | reset : IN std_logic;
|
27 | filter_in : IN std_logic_vector(17 DOWNTO 0); -- sfix18_En17
|
28 | filter_out : OUT std_logic_vector(17 DOWNTO 0) -- sfix18_En12
|
29 | );
|
30 | END component;
|
31 |
|
32 | component deci
|
33 | PORT( clk : IN std_logic;
|
34 | clk_enable : IN std_logic;
|
35 | reset : IN std_logic;
|
36 | filter_in : IN std_logic_vector(17 DOWNTO 0); -- sfix18_En17
|
37 | filter_out : OUT std_logic_vector(17 DOWNTO 0) -- sfix18_En9
|
38 | );
|
39 |
|
40 | END component;
|
41 |
|
42 |
|
43 | component demodulate --get sin and coine in and multiply by adc value
|
44 | generic ( sine_width : integer :=14);
|
45 | port (--clock_in : in std_logic;
|
46 | fromadc_in : in Std_logic_vector (13 downto 0);
|
47 | sine_in : in std_logic_vector (sine_width -1 downto 0); -- as 2s complement
|
48 | cosine_in : in std_logic_vector (sine_width -1 downto 0);
|
49 | output_i : out std_logic_vector (17 downto 0); --in*cos
|
50 | output_q : out std_logic_vector (17 downto 0) --in*sin
|
51 | );
|
52 | end component;
|
53 |
|
54 | --signal adc_value : std_logic_vector (13 downto 0);
|
55 | --signal sine_value : std_logic_vector (sine_width -1 downto 0);
|
56 | --signal cosine_value : std_logic_vector (sine_width -1 downto 0);
|
57 | signal multiplied_sine : std_logic_vector (17 downto 0);
|
58 | signal multiplied_cosine : std_logic_vector (17 downto 0);
|
59 | signal filtered1_sine : std_logic_vector (17 downto 0);
|
60 | signal filtered1_cosine : std_logic_vector (17 downto 0);
|
61 | signal filtered2_sine : std_logic_vector (17 downto 0);
|
62 | signal filtered2_cosine : std_logic_vector (17 downto 0);
|
63 | signal filtered3_sine : std_logic_vector (17 downto 0);
|
64 | signal filtered3_cosine : std_logic_vector (17 downto 0);
|
65 | signal decimated1_sine : std_logic_vector (17 downto 0);
|
66 | signal decimated1_cosine : std_logic_vector (17 downto 0);
|
67 | signal decimated2_sine : std_logic_vector (17 downto 0);
|
68 | signal decimated2_cosine : std_logic_vector (17 downto 0);
|
69 | signal decimated1_clock : std_logic;
|
70 | signal decimated2_clock : std_logic;
|
71 | signal counter1 : integer range 0 to 7;
|
72 | signal counter2 : integer range 0 to 7;
|
73 | --signal filtered2_sine : std_logic_vector (17 downto 0);
|
74 | --signal filtered2_cosine : std_logic_vector (17 downto 0);
|
75 |
|
76 | begin
|
77 | d1 : demodulate port map(adc_in, sinus_in, cosinus_in, multiplied_cosine, multiplied_sine);
|
78 | tp1cos : iir_lp port map (clock_in, enable, reset, multiplied_cosine, filtered1_cosine); -- filtered cos
|
79 | tp1sin : iir_lp port map (clock_in, enable, reset, multiplied_sine, filtered1_sine); -- filtered sin
|
80 | deci1 : deci port map (clock_in, enable, reset, filtered1_cosine, decimated1_cosine);
|
81 | deci2 : deci port map (clock_in, enable, reset, filtered1_sine, decimated1_sine);
|
82 | tp2cos : iir_lp port map (decimated1_clock, enable, reset, decimated1_cosine, filtered2_cosine); -- filtered cos
|
83 | tp2sin : iir_lp port map (decimated1_clock, enable, reset, decimated1_sine, filtered2_sine); -- filtered sin
|
84 | deci3 : deci port map (decimated1_clock, enable, reset, filtered2_cosine, decimated2_cosine);
|
85 | deci4 : deci port map (decimated1_clock, enable, reset, filtered2_sine, decimated2_sine);
|
86 | tp3cos : iir_lp port map (decimated2_clock, enable, reset, decimated2_cosine, filtered3_cosine); -- filtered cos
|
87 | tp3sin : iir_lp port map (decimated2_clock, enable, reset, decimated2_sine, filtered3_sine); -- filtered sin
|
88 |
|
89 | process (clock_in)
|
90 | begin --process
|
91 | if rising_edge(clock_in) then
|
92 | if (frequency_in (31 downto 29) = "000") then
|
93 | I_out <= filtered1_cosine;
|
94 | Q_out <= filtered1_sine;
|
95 | clock_out <= clock_in;
|
96 | elsif (frequency_in (28 downto 26)="000") then
|
97 | I_out <= filtered2_cosine;
|
98 | Q_out <= filtered2_sine;
|
99 | clock_out <= decimated1_clock;
|
100 | else
|
101 | I_out <= filtered3_cosine;
|
102 | Q_out <= filtered3_sine;
|
103 | clock_out <= decimated2_clock;
|
104 | end if;
|
105 | end if;
|
106 | end process;
|
107 |
|
108 | process (clock_in)
|
109 | begin --process
|
110 | if rising_edge(clock_in) then
|
111 | counter1 <=counter1+1;
|
112 | if (counter1=7) then
|
113 | counter2 <= counter2+1;
|
114 | decimated1_clock <= not decimated1_clock;
|
115 | end if;
|
116 | if (counter2=7) then
|
117 | decimated2_clock <= not decimated2_clock;
|
118 | end if;
|
119 | end if;
|
120 | end process;
|
121 |
|
122 | end behave;
|