-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevator.vhd
193 lines (179 loc) · 4.69 KB
/
elevator.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
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
-- first part lab sessions - digital electronics
-- programming a microcontroller for an elevator
-- session 2
-- design by Roberto Uceda Gomez - NIA 100346538
-- elevator - main entity
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity elevator is
port (
CLK,RESET,S: in std_logic;
B: in std_logic_vector(3 downto 0);
SENSE,MOTOR,OPENING,BUSY: out std_logic;
FLOOR,TARGET: out std_logic_vector(1 downto 0)
);
end elevator;
architecture a_elevator of elevator is
-- type and signals for state machine
type state is (s0,s1,s2,s3,s4);
signal curr_state, next_state: state;
-- waiting time for the door, adjustable
constant opening_time : integer := 3; -- equal to 3s
-- declaring needed blocks
-- REGISTER --------------------------
component regist is
port (
reset,clk,enable : in std_logic;
input : in std_logic_vector(1 downto 0);
output : out std_logic_vector(1 downto 0)
);
end component;
-- EDGE DETECTOR ---------------------
component edge_det is
port (
input,clk,reset : in std_logic;
output : out std_logic
);
end component;
-- ENCODER ---------------------------
component encoder is
port (
input : in std_logic_vector(3 downto 0);
output : out std_logic_vector(1 downto 0);
act : out std_logic
);
end component;
-- COUNTER ---------------------------
component counter is
port (
sensor,reset,clk,enable,sense: in std_logic;
count : out std_logic_vector(1 downto 0);
carry_out : out std_logic
);
end component;
-- TIMER -----------------------------
component timer is
port (
clk, reset, enable: in std_logic;
elapsed: out integer
);
end component;
-- internal signal declarations to link components
signal pcod_s, floor_s, target_s: std_logic_vector(1 downto 0);
signal count_s: std_logic_vector(1 downto 0);
signal sense_s, pressed_s, floor_pass_s, busy_s: std_logic;
signal elapsed_s: integer;
signal opening_s: std_logic;
signal enable_s : std_logic;
begin
enable_s <= '1';
-- instantiating button encoder component
-- mapping all components with their signals
en: encoder port map (
input => B,
output => pcod_s,
act => pressed_s );
ed: edge_det port map (
input => S,
output => floor_pass_s,
clk => CLK,
reset => RESET );
r0: regist port map ( --target
input => pcod_s,
output => target_s,
enable => (pressed_s and not busy_s),
clk => CLK,
reset => RESET );
r1: regist port map ( --floor
input => count_s,
output => floor_s,
enable => enable_s,
clk => CLK,
reset => RESET );
ct: counter port map (
sensor => floor_pass_s,
sense => sense_s,
count => count_s,
enable => enable_s,
clk => CLK,
reset => RESET );
tim: timer port map (
elapsed => elapsed_s,
clk => clk,
reset => not enable_s,
enable => opening_s ); -- the timer starts when opening_s is active
sync_process: process (clk, reset)
begin
if reset='1' then
curr_state <= s0;
elsif rising_edge(clk) then
curr_state <= next_state;
end if;
end process sync_process;
state_change: process (curr_state, floor_s, target_s, elapsed_s)
begin
case curr_state is
when s0 =>
MOTOR <= '0';
opening_s <= '0';
busy_s <= '0';
sense_s <= '0';
if target_s > floor_s then
next_state <= s1;
else
next_state <= s0;
end if;
when s1 =>
MOTOR <= '1';
sense_s <= '1';
busy_s <= '1';
opening_s <= '0';
if target_s = floor_s then
next_state <= s2;
else
next_state <= s1;
end if;
when s2 =>
MOTOR <= '0';
opening_s <= '1';
busy_s <= '1';
sense_s <= '0';
if elapsed_s = opening_time then
next_state <= s3;
else
next_state <= s2;
end if;
when s3 =>
MOTOR <= '0';
opening_s <= '0';
busy_s <= '0';
sense_s <= '0';
if target_s > floor_s then
next_state <= s1;
elsif target_s < floor_s then
next_state <= s4;
else
next_state <= s3;
end if;
when s4 =>
MOTOR <= '1';
sense_s <= '0';
busy_s <= '1';
opening_s <= '0';
if target_s = floor_s then
next_state <= s2;
else
next_state <= s4;
end if;
when others =>
MOTOR <= '0';
next_state <= s3;
end case;
end process state_change;
TARGET <= target_s;
FLOOR <= floor_s;
SENSE <= sense_s;
BUSY <= busy_s;
OPENING <= opening_s;
end a_elevator;