您的位置:首页 > 其它

这两个VHDL的问题终于解决了!

2013-06-05 10:30 162 查看
这个星期做EDA的课程设计,终于得用VHDL写一些东西了,而不仅仅是实验课的时候那样十几行就能解决了。

写长一点的时候,发现两个相当棘手的禁令啊:


1、一个进程内不能进行两次边沿检测。

2、不同进程不能对同一信号进行赋值。


正因为这两条“禁令”,让本来看上去很简单的东西搞得我焦头烂额了。

于是,我打算把事情简单化,做了两个这样的练习:


1、两个按键,分别控制一个值的加和减。(这个主要是为了解决“禁令1”)

2、一个按键,按下按键输出一个固定时间脉冲。(解决“禁令2”)


我先做的是第二个练习。如果没有禁令,我的想法是,一个进程内检测到按键信号上升沿的时候对信号A写‘1’,然后另外一个进程检测到信号A为‘1’时开始用时钟信号计时并输出,到时间后对这个信号A写‘0’。看上去十分简单,天衣无缝,但是因为“禁令2”,这个简单的方法不能实现。

我的解决方法是用了两个信号,并且还有优先级的区别:

Library IEEE;
Use IEEE.STD_LOGIC_1164.ALL;

Entity KeyClk Is
Port(
clk:in std_logic;
c_in:in std_logic;
c_out:out std_logic
);
End;

Architecture one of keyclk is
signal out_flag,stop_flag:std_logic;
begin

process(c_in,out_flag)
begin
if c_in='1' then
out_flag<='1';
elsif stop_flag='1' then
out_flag<='0';
end if;
end process;

c_out<=in_flag;

process(clk,in_flag)
variable cnt:integer range 0 to 100;
begin
if rising_edge(clk) then
if out_flag='1' then
stop_flag<='0';
if cnt<5 then
cnt := cnt+1;

else
cnt := 0;
stop_flag<='1';
end if;
end if;
end if;
end process;
end;


第二个练习是这样的,貌似有点纠结:

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity twokey is
port(
clk:in std_logic;
key1,key2:in std_logic;
key_rst:in std_logic;
qout:out std_logic_vector(7 downto 0)
);
end entity;

architecture one of twokey is
signal key1_down,key2_down:std_logic;
signal qout2:std_logic_vector(7 downto 0);
begin
process(key1,key2,key_rst,clk)
begin
if rising_edge(clk) then
if key_rst='1' then
qout2<=(others=>'0');
else
if key1='1' and key1_down='0' then
key1_down<='1';
qout2<=qout2+'1';
elsif key1='0' then
key1_down<='0';
end if;
if key2='1' and key2_down='0' then
key2_down<='1';
qout2<=qout2-'1';
elsif key2='0' then
key2_down<='0';
end if;
end if;
end if;
end process;
qout<=qout2;
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: