-
Notifications
You must be signed in to change notification settings - Fork 1
/
pwm10bit.vhd
118 lines (104 loc) · 3.72 KB
/
pwm10bit.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
----------------------------------------------------------------------------------
-- Company: @Home
-- Engineer: Zoltan Pekic ([email protected])
--
-- Create Date: 01:57:47 02/27/2016
-- Design Name:
-- Module Name: pwm10bit - Behavioral
-- Project Name: Alarm Clock
-- Target Devices: Mercury FPGA + Baseboard (http://www.micro-nova.com/mercury/)
-- Tool versions: Xilinx ISE 14.7 (nt64)
--
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity pwm10bit is
Port ( clk : in STD_LOGIC;
adc_samplingrate: in STD_LOGIC;
adc_channel : in STD_LOGIC_VECTOR (2 downto 0);
adc_miso : in std_logic; -- ADC SPI MISO
adc_mosi : out std_logic; -- ADC SPI MOSI
adc_cs : out std_logic; -- ADC SPI CHIP SELECT
adc_clk : out std_logic; -- ADC SPI CLOCK
adc_value: out std_logic_vector(15 downto 0);
adc_valid: out std_logic;
pwm_out : out STD_LOGIC);
end pwm10bit;
-- From http://www.micro-nova.com/resources/
use work.MercuryADC;
architecture Behavioral of pwm10bit is
component MercuryADC is
port
(
-- command input
clock : in std_logic; -- 50MHz onboard oscillator
trigger : in std_logic; -- assert to sample ADC
diffn : in std_logic; -- single/differential inputs
channel : in std_logic_vector(2 downto 0); -- channel to sample
-- data output
Dout : out std_logic_vector(9 downto 0); -- data from ADC
OutVal : out std_logic; -- pulsed when data sampled
-- ADC connection
adc_miso : in std_logic; -- ADC SPI MISO
adc_mosi : out std_logic; -- ADC SPI MOSI
adc_cs : out std_logic; -- ADC SPI CHIP SELECT
adc_clk : out std_logic -- ADC SPI CLOCK
);
end component;
signal threshold: integer range 0 to 1023 := 0;
signal counter: integer range 0 to 1023 := 0;
signal adc_out: std_logic_vector(9 downto 0);
signal adc_pulse: std_logic;
begin
-- map to output and extend to 16 bits
adc_value <= "000000" & adc_out;
adc_valid <= adc_pulse;
adc: MercuryADC port map
(
-- command input
clock => clk, -- from onboard oscillator
trigger => adc_samplingrate, -- assert to sample ADC
diffn => '1', -- single/differential inputs
channel => adc_channel, -- channel to sample (3 == light sensor)
-- data output
Dout => adc_out, -- data from ADC
OutVal => adc_pulse, -- pulsed when data sampled
-- ADC connection
adc_miso => adc_miso, -- ADC SPI MISO
adc_mosi => adc_mosi, -- ADC SPI MOSI
adc_cs => adc_cs, -- ADC SPI CHIP SELECT
adc_clk => adc_clk -- ADC SPI CLOCK
);
get_adc: process(adc_pulse)
begin
if (adc_pulse = '1') then
threshold <= to_integer(unsigned(adc_out));
end if;
end process;
generate_pwm: process(clk)
begin
if (clk'event and clk = '1') then
counter <= counter + 1; -- just let it wrap around
if (counter > threshold) then
pwm_out <= '0';
else
pwm_out <= '1';
end if;
end if;
end process;
end Behavioral;