Repository navigation
Expand file tree
/
Copy pathdoor_controller.vhd
More file actions
196 lines (172 loc) · 7.58 KB
/
Copy pathdoor_controller.vhd
File metadata and controls
196 lines (172 loc) · 7.58 KB
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
--------------------------------------------------------------------------------
-- door_controller.vhd
-- 电梯门控制器 + 10秒自动关门定时
--
-- 功能: 4状态子状态机控制开/关门过程
-- DS_CLOSED -> DS_OPENING -> DS_OPEN_WAIT -> DS_CLOSING -> DS_CLOSED
-- 支持手动开门/关门按钮覆盖
-- 标准: VHDL-93, Quartus II 9.1 兼容
-- 风格: 双进程(时序寄存器 + 组合逻辑)
-- 复位: 异步低电平有效 (rst_n)
--------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity door_controller is
port(
clk_1hz : in std_logic; -- 1Hz 时钟(秒计数)
rst_n : in std_logic; -- 异步复位(低电平有效)
-- 来自 elevator_ctrl 的命令
door_open_cmd : in std_logic; -- 开门命令
door_close_cmd: in std_logic; -- 关门命令
-- 消抖后的手动按钮(电平信号)
btn_open : in std_logic; -- 手动开门(电平)
btn_close : in std_logic; -- 手动关门(电平)
-- 输出状态
door_opened : out std_logic; -- 门已打开标志
door_closed : out std_logic; -- 门已关闭标志
door_status : out std_logic; -- 门当前状态('1'=开, '0'=关),驱动 LED
-- 跨时钟域请求清除
clear_open_req : out std_logic;
clear_close_req: out std_logic
);
end entity;
architecture rtl of door_controller is
---------------------------------------------------------------
-- 门状态定义(4状态)
---------------------------------------------------------------
constant DS_CLOSED : std_logic_vector(1 downto 0) := "00"; -- 门已关闭
constant DS_OPENING : std_logic_vector(1 downto 0) := "01"; -- 开门中(1秒过程)
constant DS_OPEN_WAIT: std_logic_vector(1 downto 0) := "10"; -- 门开等待(10秒计时)
constant DS_CLOSING : std_logic_vector(1 downto 0) := "11"; -- 关门中(1秒过程)
---------------------------------------------------------------
-- 内部寄存器信号
---------------------------------------------------------------
signal current_ds : std_logic_vector(1 downto 0); -- 当前门状态
signal timer : std_logic_vector(3 downto 0); -- 10秒定时计数器(0~9)
-- door_open_cmd 上升沿检测用寄存器
signal open_cmd_d1 : std_logic;
signal close_cmd_d1 : std_logic;
---------------------------------------------------------------
-- 组合逻辑到寄存器的中间信号
---------------------------------------------------------------
signal next_ds : std_logic_vector(1 downto 0);
signal next_timer : std_logic_vector(3 downto 0);
signal open_cmd_rise : std_logic;
signal close_cmd_rise : std_logic;
begin
---------------------------------------------------------------
-- 命令和按钮信号上升沿检测(组合逻辑)
---------------------------------------------------------------
open_cmd_rise <= door_open_cmd and (not open_cmd_d1);
close_cmd_rise <= door_close_cmd and (not close_cmd_d1);
---------------------------------------------------------------
-- 进程1: 时序逻辑(状态寄存器 + 计时器 + 命令延迟寄存器)
---------------------------------------------------------------
process(clk_1hz, rst_n)
begin
if rst_n = '0' then
current_ds <= DS_CLOSED;
timer <= "0000";
open_cmd_d1 <= '0';
close_cmd_d1 <= '0';
elsif rising_edge(clk_1hz) then
current_ds <= next_ds;
timer <= next_timer;
open_cmd_d1 <= door_open_cmd;
close_cmd_d1 <= door_close_cmd;
end if;
end process;
---------------------------------------------------------------
-- 进程2: 组合逻辑(计算 next_ds, next_timer 和输出)
---------------------------------------------------------------
process(current_ds, timer, open_cmd_rise, close_cmd_rise,
btn_open, btn_close)
begin
---------------------------------------------------------
-- 所有输出/中间信号默认值(防止锁存器)
---------------------------------------------------------
next_ds <= current_ds;
next_timer <= timer;
door_opened <= '0';
door_closed <= '0';
door_status <= '0';
clear_open_req <= '0';
clear_close_req <= '0';
case current_ds is
--=====================================================
-- DS_CLOSED: 门已关闭
-- door_closed='1', 等待开门命令
--=====================================================
when DS_CLOSED =>
door_closed <= '1';
door_status <= '0';
clear_close_req <= '1'; -- 门已关闭,持续清除任何关门请求
if open_cmd_rise = '1' or btn_open = '1' then
-- 收到开门命令上升沿,或手动开门按钮有效,开始开门
next_ds <= DS_OPENING;
next_timer <= "0000";
end if;
--=====================================================
-- DS_OPENING: 开门过程(1秒)
-- 1个 clk_1hz 周期后完成开门
--=====================================================
when DS_OPENING =>
door_status <= '1'; -- 开门中视为门开状态
clear_open_req <= '1'; -- 持续清除开门请求
-- 1秒开门过程完成,进入等待状态
next_ds <= DS_OPEN_WAIT;
next_timer <= "0000"; -- 进入等待时清零计时器
--=====================================================
-- DS_OPEN_WAIT: 门开等待,10秒计时
-- btn_close 或 timer=9 -> 关门
-- btn_open -> 计时器清零重新计时
-- close_cmd_rise -> 强制关门
--=====================================================
--=====================================================
when DS_OPEN_WAIT =>
door_opened <= '1';
door_status <= '1';
clear_open_req <= '1'; -- 门已开完,持续清除开门请求
if btn_close = '1' or close_cmd_rise = '1' then
-- 手动关门或关门命令:立即开始关门
next_ds <= DS_CLOSING;
next_timer <= "0000";
elsif btn_open = '1' then
-- 手动开门:重置定时器(重新10秒计时)
next_timer <= "0000";
elsif timer = "1001" then
-- 10秒定时到(计数0~9,到9即10个周期)
next_ds <= DS_CLOSING;
next_timer <= "0000";
else
-- 正常计时
next_timer <= timer + 1;
end if;
--=====================================================
-- DS_CLOSING: 关门过程(1秒)
-- btn_open 有效时立即返回开门
-- 1秒后完成关门
--=====================================================
--=====================================================
when DS_CLOSING =>
door_status <= '0'; -- 关门中视为门关状态
clear_close_req <= '1'; -- 持续清除关门请求
if btn_open = '1' then
-- 关门过程中按开门,立即返回开门
next_ds <= DS_OPENING;
next_timer <= "0000";
else
-- 1秒关门过程完成
next_ds <= DS_CLOSED;
end if;
--=====================================================
-- 默认分支(防止非法状态)
--=====================================================
when others =>
next_ds <= DS_CLOSED;
next_timer <= "0000";
door_closed <= '1';
end case;
end process;
end architecture;