📄 xiaoche_text.vhd
字号:
-- xiaoche_text.vhd
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity xiaoche_text is
port(key_add,key_sub:in std_logic; --控制小车加速和减速键
key_qj,key_ht:in std_logic; --控制小车前进和后退键
clk:in std_logic; --48MHz时钟信号输入端
reset:in std_logic; --复位按键
pwm_out1,pwm_out2,pwm_out3,pwm_out4:out std_logic); --PWM波信号输出端
end;
architecture behave of xiaoche_text is
type state is(st0,st1,st2);
signal s : state;
signal pwm_out_done1,pwm_out_done2 : std_logic;
signal key_add1,key_sub1 : std_logic;
signal dout11,dout21,dout31 : std_logic;
signal dout12,dout22,dout32 : std_logic;
signal count : std_logic_vector(16 downto 0);
signal clk1 : std_logic_vector(9 downto 0);
signal duty_cycle : integer range 0 to 100; --占空比调节信号
begin
u1:process(clk) --对时钟进行分频得到频率较低的时钟
begin
if clk'event and clk='1'
then count<=count+'1';clk1<=clk1+'1';
end if;
end process;
key_add1<=dout11 and dout21 and dout31; --按键消抖处理
key_sub1<=dout12 and dout22 and dout32;
u2:process(count(16)) --利用低频率的时钟进行按键消抖
begin
if count(16)'event and count(16)='1'
then dout11<=key_add;dout21<=dout11;dout31<=dout21;
dout12<=key_sub;dout22<=dout12;dout32<=dout22;
end if;
end process;
u3:process(key_add1,key_sub1,reset,clk) --按键控制模块
variable d_c : integer range 0 to 100;
begin
if reset='1' then d_c:=50;s<=st0;
elsif clk'event and clk='1' then
case s is
when st0 =>if key_add1='1' then s<=st1;
elsif key_sub1='1' then s<=st2;
end if;
when st1 =>if key_add1='0' then d_c:=d_c+10;s<=st0;
end if;
when st2 =>if key_sub1='0' then d_c:=d_c-10;s<=st0;
end if;
end case;
end if;
duty_cycle<=d_c;
end process;
u_pwm:process(clk1(9),reset) --PWM波生成模块
variable freq : integer range 0 to 100; --PWM波频率调节变量
begin
if reset='1' then freq:=0;
elsif clk1(9)'event and clk1(9)='1'
then freq:=freq+1;
if freq<duty_cycle then
pwm_out_done1<='1';pwm_out_done2<='1';
else pwm_out_done1<='0';pwm_out_done2<='0';
end if;
end if;
end process;
u_control:process(clk,reset) --控制小车前进和后退模块
begin
if reset='1' then pwm_out1<='1';pwm_out2<='1';pwm_out3<='1';pwm_out4<='1'; --小车复位(停止)
elsif clk'event and clk='1' then
if key_qj='1' then pwm_out1<=pwm_out_done1;pwm_out2<='1';
pwm_out3<=pwm_out_done2;pwm_out4<='1';
elsif key_ht='1' then pwm_out1<='1';pwm_out2<=pwm_out_done1;
pwm_out3<='1';pwm_out4<=pwm_out_done2;
end if;
end if;
end process;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -