-
Notifications
You must be signed in to change notification settings - Fork 1
/
Servo Control with Matrix Keypad.txt
251 lines (227 loc) · 6.41 KB
/
Servo Control with Matrix Keypad.txt
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#########Servo Control with Keypad###########
After each heading make a new module
Top Module Code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity top is
Port (clk: in std_logic;
reset: in std_logic;
row: in std_logic_vector(3 downto 0);
col: out std_logic_vector(3 downto 0);
pwmo: out std_logic );
end top ;
architecture Behavioral of top is
signal tmp : std_logic_vector(3 downto 0);
component Decoder is
Port ( clk : in STD_LOGIC;
Row : in STD_LOGIC_VECTOR (3 downto 0);
Col : out STD_LOGIC_VECTOR (3 downto 0);
DecodeOut : out STD_LOGIC_VECTOR (3 downto 0));
end component;
component PWM is
Port ( clk_input: in std_logic;
A : in STD_LOGIC_vector(3 downto 0);
reset: in std_logic;
clk_output: out std_logic
);
end component;
begin
C0: Decoder port map (clk=>clk, Row =>Row, Col=>Col, DecodeOut=> tmp);
S0: PWM port map (clk_input=>clk, A=>tmp, reset=>reset, clk_output=>pwmo);
end Behavioral;
Heading 2:
Keypad Decoder:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity Decoder is
Port (
clk : in STD_LOGIC;
Row : in STD_LOGIC_VECTOR (3 downto 0);
Col : out STD_LOGIC_VECTOR (3 downto 0);
DecodeOut : out STD_LOGIC_VECTOR (3 downto 0));
end Decoder;
architecture Behavioral of Decoder is
signal clksig :STD_LOGIC_VECTOR(19downto 0);
begin
process(clk)
begin
if clk'event and clk = '1' then
-- 1ms
if clksig = "00011000011010100000" then
--C1
Col<= "1000";
clksig <= clksig+1;
-- check row pins
elsif clksig = "00011000011010101000" then
--R1
if Row = "1000" then
DecodeOut <= "0001"; --1
--R2
elsif Row = "0100" then
DecodeOut <= "0100"; --4
--R3
elsif Row = "0010" then
DecodeOut <= "0111"; --7
--R4
elsif Row = "0001" then
DecodeOut <= "0000"; --0
end if;
clksig <= clksig+1;
-- 2ms
elsif clksig = "00110000110101000000" then
--C2
Col<= "0100";
clksig <= clksig+1;
-- check row pins
elsif clksig = "00110000110101001000" then
--R1
if Row = "1000" then
DecodeOut <= "0010"; --2
--R2
elsif Row = "0100" then
DecodeOut <= "0101"; --5
--R3
elsif Row = "0010" then
DecodeOut <= "1000"; --8
--R4
elsif Row = "0001" then
DecodeOut <= "1111"; --F
end if;
clksig <= clksig+1;
--3ms
elsif clksig = "01001001001111100000" then
--C3
Col<= "0010";
clksig <= clksig+1;
-- check row pins
elsif clksig = "01001001001111101000" then
--R1
if Row = "1000" then
DecodeOut <= "0011"; --3
--R2
elsif Row = "0100" then
DecodeOut <= "0110"; --6
--R3
elsif Row = "0010" then
DecodeOut <= "1001"; --9
--R4
elsif Row = "0001" then
DecodeOut <= "1110"; --E
end if;
clksig <= clksig+1;
--4ms
elsif clksig = "01100001101010000000" then
--C4
Col<= "0001";
clksig <= clksig+1;
-- check row pins
elsif clksig = "01100001101010001000" then
--R1
if Row = "1000" then
DecodeOut <= "1010"; --A
--R2
elsif Row = "0100" then
DecodeOut <= "1011"; --B
--R3
elsif Row = "0010" then
DecodeOut <= "1100"; --C
--R4
elsif Row = "0001" then
DecodeOut <= "1101"; --D
end if;
clksig <= "00000000000000000000";
else
clksig <= clksig+1;
end if;
end if;
end process;
end Behavioral;
Heading 3: PWM Servo Module:
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use IEEE.std_logic_unsigned.all;
entity PWM is
Port ( clk_input: in std_logic;
A : in STD_LOGIC_vector(3 downto 0);
reset: in std_logic;
clk_output: out std_logic
);
end PWM;
architecture Behavioral of PWM is
signal tmp: std_logic;
signal count: integer range 0 to 4000001:=0;
signal count1: integer range 0 to 4000001:=0;
signal count2: integer range 0 to 4000001:=0;
begin
process(clk_input, reset)
begin
if reset = '1' then
tmp <= '0';
count<= 0 ;
else
if rising_edge (clk_input)then
if A="0110" then --if 6 is pressed
count <= count +1;
if count < 2222 then
tmp <= '1';
count <= count + 1;
end if;
if count > 2222 then
tmp <= '0';
count <= count+1;
end if;
if count > 2000000 then
count <= 0;
end if;
end if;
if A = "1001" then -- if 9 is pressed
count <= count +1;
if count < 400000 then
tmp <= '1';
count <= count + 1;
end if;
if count > 400000 then
tmp <= '0';
count <= count+1;
end if;
if count > 2000000 then
count <= 0;
end if;
end if;
end if;
end if;
end process;
clk_output <= tmp;
end Behavioral;
Constraints:
set_property PACKAGE_PIN W5 [get_ports clk]
set_property IOSTANDARD LVCMOS33 [get_ports clk]
set_property PACKAGE_PIN W15 [get_ports {reset}]
set_property IOSTANDARD LVCMOS33 [get_ports {reset}]
set_property PACKAGE_PIN J1 [get_ports {pwmo}]
set_property IOSTANDARD LVCMOS33 [get_ports {pwmo}]
set_property PACKAGE_PIN A14 [get_ports {row[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {row[0]}]
#Sch name = JB2
set_property PACKAGE_PIN A16 [get_ports {row[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {row[1]}]
#Sch name = JB3
set_property PACKAGE_PIN B15 [get_ports {row[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {row[2]}]
#Sch name = JB4
set_property PACKAGE_PIN B16 [get_ports {row[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {row[3]}]
set_property PACKAGE_PIN K17 [get_ports {col[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {col[0]}]
#Sch name = JC2
set_property PACKAGE_PIN M18 [get_ports {col[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {col[1]}]
#Sch name = JC3
set_property PACKAGE_PIN N17 [get_ports {col[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {col[2]}]
#Sch name = JC4
set_property PACKAGE_PIN P18 [get_ports {col[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {col[3]}]