我尝试使用VHDL来实现序列以使用步进电机。由于我是VHDL的新手,因此无法查看代码中缺少的内容。我想循环遍历一个数组,为我的名为motor的变量提供不同的步骤。
我很感激任何帮助。
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity main is
Port ( motor : out STD_LOGIC_VECTOR (3 downto 0));
end main;
architecture Behavioral of main is
type my_array is array (0 to 6) of std_logic_vector(2 downto 0);
signal sequence: my_array;
sequence:= ("0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111");
begin
variable i : std_logic:= '1';
for i in sequence' range loop
motor <= sequence(i);
end loop;
end Behavioral;
这些是错误:
ERROR:HDLCompiler:806 - "C:/Users/main.vhd" Line 12: Syntax error near "sequence".
ERROR:HDLCompiler:806 - "C:/Users/main.vhd" Line 15: Syntax error near ":=".
ERROR:HDLCompiler:806 - "C:/Users/main.vhd" Line 16: Syntax error near "loop".
ERROR:HDLCompiler:806 - "C:/Users//main.vhd" Line 18: Syntax error near "loop".
ERROR:ProjectMgmt:496 - 4 error(s) found while parsing design hierarchy.
答案 0 :(得分:2)
这里有些不对劲。
1)您无法在声明区域中执行信号分配。你可以在那里初始化一个信号;语法是
signal sequence: my_array := ("0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111");
然而,因为它是一个查找表,而不是你想要改变的东西,所以把它变成一个常数:
constant sequence: my_array := ("0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111");
2)循环数组比这简单得多!数组为索引声明了自己的数据类型,因此for i in sequence' range loop
隐式声明了自己的i
变量,我建议你删除你自己的i
声明(这完全是错误的类型!)
3)正如Godel所说,没有时钟:因此无法控制电机速度。您需要将循环嵌入到时钟进程中,这是一种标准设计模式;您可以在任何VHDL指南中找到它。
答案 1 :(得分:2)
从根本上说,这个:
for i in sequence' range loop
motor <= sequence(i);
end loop;
是描述逐步遍历数组的代码。但是,您没有任何东西可以让模型等待一段时间。您需要将该代码包装在一个进程中
wait for 100 us;
(例如)如果这是您不希望合成芯片并且只想使用时间延迟的代码也许是这样的:
process
variable index : integer range sequence'range;
begin
wait until rising_edge(clk);
if step_now = '1' then
if index >= sequence'high then
index := sequence'low;
else
index := index + 1;
end if;
motor <= sequence(index);
end process;