【问题标题】:VHDL If problemsVHDL 如果有问题
【发布时间】:2014-02-20 23:38:11
【问题描述】:

我正在努力学习 VHDL,但进展并不顺利..

这段代码是我写的,

library IEEE;
use IEEE.bit_1164.ALL;
use IEEE.bit_ARITH.ALL;
use IEEE.bit_UNSIGNED.ALL;


entity Switch_led is
port(
    Switch_0: in bit;
    Switch_1: in bit;
    Switch_2: in bit;
    Switch_3: in bit;
    Switch_4: in bit;
    Switch_5: in bit;
    Switch_6: in bit;
    Switch_7: in bit;

    Led_0: out bit;
    Led_1: out bit;
    Led_2: out bit;
    Led_3: out bit;
    Led_4: out bit;
    Led_5: out bit;
    Led_6: out bit;
    Led_7: out bit
                        );
end Switch_led;

architecture Behavioral of Switch_led is

begin

    if Switch_0 = '1' then 
    Led_0 <= 1;

    elsif Switch_1 = '1' then 
    Led_1 <= 1;

    elsif Switch_2 = '1' then 
    Led_2 <= 1;

    elsif Switch_3 = '1' then 
    Led_3 <= 1;

    elsif Switch_4 = '1' then 
    Led_4 <= 1;

    elsif Switch_5 = '1' then 
    Led_5 <= 1;

    elsif Switch_6 = '1' then 
    Led_6 <= 1;

    elsif Switch_7 = '1' then 
    Led_7 <= 1;
    end if;

end Behavioral;

由于某种原因,我的架构中的 if 语句出现错误。但我无法找到错误所在。我希望代码有意义。

【问题讨论】:

  • 您需要准确地向我们展示错误消息的内容。

标签: if-statement vhdl


【解决方案1】:

位类型的枚举名称在包标准中给出。

type BIT is ('0', '1');

这类作业:

Led_0 <= 1;

应该是这样的:

Led_0 <= '1';

您还会注意到 if 语句用于适合顺序语句的位置,这意味着所有这些都应该在流程语句中。

SWITCH:
    process (Switch_0,Switch_1,Switch_2,Switch_3,Switch_4,Switch_5, Switch_6,Switch_7)
    begin
        if Switch_0 = '1' then 
            Led_0 <= '1';

        elsif Switch_1 = '1' then 
            Led_1 <= '1';

        elsif Switch_2 = '1' then 
            Led_2 <= '1';

        elsif Switch_3 = '1' then 
            Led_3 <= '1';

        elsif Switch_4 = '1' then 
            Led_4 <= '1';

        elsif Switch_5 = '1' then 
            Led_5 <= '1';

        elsif Switch_6 = '1' then 
            Led_6 <= '1';

        elsif Switch_7 = '1' then 
            Led_7 <= '1';
        end if;
    end process;

您还可以注意到 if elsif 将按特定顺序评估开关,并且仅对最高优先级的开关生效(首先在 if 然后 elsif 然后结束 if 结构)。

没有任何主名称以“bit”开头的 ieee 包:

-- IEEE.bit_1164.ALL;
-- use IEEE.bit_ARITH.ALL;
-- use IEEE.bit_UNSIGNED.ALL;

(你可以使用 std_logic)。

因为提到了开关和 LED,您应该知道您只为任何 LED 分配了一个值,它们将继续亮起(BIT 的最左侧值),并且在打开时,如果成功合成和实施,则保持亮起在 FPGA 中。

Led_0 <= Switch_0;  -- as a concurrent signal assignment statement.

摆脱这种现象的一个简单方法是不使用 if 语句,其中 LED 值与开关值直接相关。如果您不想简单地推断连接端口的连线,您可以在进程内为每个 if 语句使用 else

if Switch_0 = '1' then 
        Led_0 <= '1';
else
        Led_0 <= '0';
end if;

然后在适合并发语句的地方(不在进程语句内)有一个等效于 if 语句的并发信号赋值语句:

Led_0 <= '1'  when Switch_0 = '1' else '0';

这些语句可以是独立的或并发的

【讨论】:

  • 甚至更简单,Led_0 &lt;= Switch_0; :)
【解决方案2】:

除了 David 的全面回答之外,我想补充一点,VHDL 对数组类型有很好的支持,知道如何使用它们是编写更紧凑代码的基础。

一维位数组称为bit_vector。因为您可以一次分配给 bit_vector 中的所有值,所以您的代码可以变得更加简单:

entity switches_to_leds is
    port (
        switches: in bit_vector(7 downto 0);
        leds: out bit_vector(7 downto 0)
    );
end;

architecture behavior of switches_to_leds is
begin
    leds <= switches;
end;

还可以查看类型 std_logicstd_logic_vector,它们是数字电路之间接口的行业标准。

【讨论】:

  • 你如何将引脚添加到约束文件/.ucf 中。你怎么会知道 switch1 位于 H18 等等......?
  • 这是我放置的旧 ucf 文件的摘录:NET "led" LOC="ae22" | IOSTANDARD=LVTTL; NET "led" LOC="ae21" | IOSTANDARD=LVTTL; NET "led" LOC="ag21" | IOSTANDARD=LVTTL;我想你可以明白的。无论如何,这是一种依赖于工具或供应商的格式,而不是 VHDL。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
相关资源
最近更新 更多