You are on page 1of 16

太原理工大学 夏路易

3.4.2 使用 VHDL 语言设计例


Max+plus2 软件支持 VHDL 语言描述被设计电路的逻辑功能,如下举例说明如何使用该语
言设计逻辑电路。
例一:设计一个加法器
第一步:进入 Max+plus2 软件环境
第二步:建立项目名称,选择 File/Project/Name 菜单
第三步:建立新文件,选择 File/New 菜单,进入文本编辑器(Text Editor File)窗口
第四步:输入 VHDL 语言编写的源程序,如图 3.4.1 所示,这是对一个加法器进行的描述。
输入完毕后存盘。

图 3.4.1

第五步:选择 Assign/Device 菜单,选定器件。

第六步:选 File/Project/Save & Comlile 菜单,编译该 VHDL 源文件


若有错误则改错,然后再编译,直至无错编译成功

第七步:选 Max+plus2\Waveform Editor 菜单,进入波形编辑窗口,编辑输入波形如图 3.4.2


所示。

99
太原理工大学 夏路易

图 3.4.2

第八步:选择 Max+plus2/Simulator 菜单,仿真结果如图 3.4.3 所示。

图 3.4.3

用 VHDL 语言描述的电路同样可以使用 File/Create Default Symbol 菜单,将该电路转换成


一个单元电路,在图形编辑器中调用该单元电路就象调用其它单元电路一样方便、简单,
见图 3.4.4。

100
太原理工大学 夏路易

图 3.4.4

例二: 设计一个 十进制加 法计数器


第一步:进入 Max+plus2 软件环境
第二步:建立项目名称,选择 File/Project/Name 菜单
第三步:建立新文件,选择 File/New 菜单,进入文本编辑器(Text Editor File)窗口
第四步:输入 VHDL 语言编写的源程序,这是对一个十进制加法计数器的行为描述。输入
完毕后存盘。

十进制加法计数器的源程序:
LIBRARY ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity count10 is
port(clk,reset,en: in std_logic;
qa,qb,qc,qd: out std_logic);
end count10;
architecture behave of count10 is
signal count_4: std_logic_vector(3 downto 0);
begin
qa<=count_4(0);
qb<=count_4(1);
qc<=count_4(2);
qd<=count_4(3);
101
太原理工大学 夏路易

process(clk,reset)
begin
if (reset='0') then
count_4<="0000";
elsif(clk'event and clk='1') then
if(en='1') then
if(count_4="1001") then
count_4<="0000";
else
count_4<=count_4+'1';
end if;
end if;
end if;
end process;
end behave;
第五步:选择 Assign/Device 菜单,选定器件。
第六步:选 File/Project/Save & Comlile 菜单,编译该 VHDL 源文件
若有错误则改错,然后再编译,直至无错编译成功
第七步:选 Max+plus2\Waveform Editor 菜单,进入波形编辑窗口,编辑输入波形如图 3.4.5
所示。

102
太原理工大学 夏路易

图 3.4.5

第八步:选择 Max+plus2/Simulator 菜单,仿真结果如图 3.4.6 所示。

图 3.4.6

例 3:
如下是一个六十进制计数器和一个十二进制计数器异步连接的例子,六十进制计数器的
进位输出是十二进制计数器的时钟脉冲。例中调用了六十进制计数器设计文件 cou60.vhd 和
十二进制计数器设计文件 cou12.vhd。从仿真中发现,若选用 FLEX10k 系列器件,由于设计
不尽合理会出现不该有的毛刺,致使不该输出进位时输出了进位,导致十二进制计数器误
动作,然而选用 MAX7000 系列器件就不会出现毛刺和误动现象。
六十进制计数器设计文件和十二进制计数器设计文件基本相同,只是十位数的归零条件不
同。
设计文件:
LIBRARY ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity cou12 is
103
太原理工大学 夏路易

port(clk,reset,cin : in std_logic;
co : out std_logic;
bcd1p : out std_logic_vector(3 downto 0);
bcd10p : out std_logic_vector(2 downto 0));
end cou12;

architecture behave of cou12 is


signal bcd1n: std_logic_vector(3 downto 0);
signal bcd10n: std_logic_vector(2 downto 0);
begin
bcd1p<=bcd1n;
bcd10p<=bcd10n;
kk1: process(clk)
begin
if(clk'event and clk='1') then
if (reset='0') then
bcd1n<="0000";
elsif (cin='1') then
if(bcd1n="1001" ) then
bcd1n<="0000";
else
bcd1n<=bcd1n+'1';
end if;
end if;
end if;
end process kk1;

kk2: process(clk)
begin
if(clk'event and clk='1') then
if (reset='0') then
bcd10n<="000";
elsif(cin='1') and (bcd1n="1001") then
if(bcd10n="001") then --归零条件
bcd10n<="000";
104
太原理工大学 夏路易

else
bcd10n<=bcd10n+'1';
end if;
end if;
end if;
end process kk2;

kk3: process(bcd10n,bcd1n,cin)
begin
if(cin='1' and bcd1n="1001" and bcd10n="001") then
co<='1';
else
co<='0';
end if;
end process kk3;
end behave;

六十进制计数器和十二进制计数器互连设计文件:
LIBRARY ieee;
USE ieee.std_logic_1164.all;
ENTITY x6012 IS
PORT (clk, reset,ena : IN std_LOGIC;
cpp : out std_logic;
a12g : out std_logic_vector(3 downto 0);
a12s : out std_logic_vector(2 downto 0);
b60g : out std_logic_vector(3 downto 0);
b60s : out std_logic_vector(2 downto 0));
END x6012;

ARCHITECTURE x12 OF x6012 IS


component cou12
port(clk,reset,cin : in std_logic;
co : out std_logic;
bcd1p : out std_logic_vector(3 downto 0);
bcd10p : out std_logic_vector(2 downto 0));
105
太原理工大学 夏路易

end component;

component cou60
port(clk,reset,cin : in std_logic;
co : out std_logic;
bcd1p : out std_logic_vector(3 downto 0);
bcd10p : out std_logic_vector(2 downto 0));
end component;

signal c12,c601 : std_logic;


signal b60gp : std_logic_vector(3 downto 0);
signal b60sp : std_logic_vector(2 downto 0);

begin
u1:cou60 port map(clk,reset,ena,c601,b60gp,b60sp);
u0:cou12 port map(c601,reset,ena,c12,a12g,a12s);
b60s<=b60sp;
b60g<=b60gp;
cpp<=c601;
end x12;
指定 MAX7000 系列器件的仿真结果见图 3.4.7:

106
太原理工大学 夏路易

图 3.4.7
指定 FLEX10K 系列的仿真结果见图 3.4.8。

图 3.4.8
例 4:四位十进制计数器
设计文件:
107
太原理工大学 夏路易

LIBRARY ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity cou4 is
port(
clk,reset,cin : in std_logic;
-- co : out std_logic;
bcdap : out std_logic_vector(3 downto 0);
bcdbp : out std_logic_vector(3 downto 0);
bcdcp : out std_logic_vector(3 downto 0);
bcddp : out std_logic_vector(3 downto 0)

);
end cou4;

architecture behave of cou4 is


signal bcdan: std_logic_vector(3 downto 0);
signal bcdbn: std_logic_vector(3 downto 0);
signal bcdcn: std_logic_vector(3 downto 0);
signal bcddn: std_logic_vector(3 downto 0);

begin

bcdap<=bcdan;
bcdbp<=bcdbn;
bcdcp<=bcdcn;
bcddp<=bcddn;
kk1: process(clk)
begin
if(clk'event and clk='1') then
if (reset='0') then
bcdan<="0000";
elsif (cin='1') then
if(bcdan="1001" ) then
108
太原理工大学 夏路易

bcdan<="0000";
else
bcdan<=bcdan+'1';
end if;
end if;
end if;
end process kk1;

kk2: process(clk)
begin
if(clk'event and clk='1') then
if (reset='0') then
bcdbn<="0000";
elsif(cin='1') and (bcdan="1001") then
if(bcdbn="1001") then
bcdbn<="0000";
else
bcdbn<=bcdbn+'1';
end if;
end if;
end if;
end process kk2;

kk3: process(clk)
begin
if(clk'event and clk='1') then
if (reset='0') then
bcdcn<="0000";
elsif(cin='1') and (bcdbn="1001") and (bcdan="1001") then
if(bcdcn="1001") then
bcdcn<="0000";
else
bcdcn<=bcdcn+'1';
end if;
end if;
109
太原理工大学 夏路易

end if;
end process kk3;

kk4: process(clk)
begin
if(clk'event and clk='1') then
if (reset='0') then
bcddn<="0000";
elsif(cin='1') and (bcdcn="1001") and (bcdbn="1001") and (bcdan="1001") then
if(bcddn="1001") then
bcddn<="0000";
else
bcddn<=bcddn+'1';
end if;
end if;
end if;
end process kk4;
end behave;

该计数器使用 MAX7000 系列器件的仿真结果见图 3.4.9。

110
太原理工大学 夏路易

图 3.4.9
7 段译码的 VHDL 设计文件:
LIBRARY ieee;
USE ieee.std_logic_1164.all;
entity seg7 is
port(ii :in std_logic_vector(3 downto 0);
y :out std_logic_vector(6 downto 0));
end seg7;

architecture behave OF seg7 is


signal indata: std_logic_vector(3 downto 0);
begin
indata<=ii;
process(indata)
begin

case indata is
when "0000"=>y<="1111110";
when "0001"=>y<="0110000";
when "0010"=>y<="1101101";
when "0011"=>y<="1111001";
when "0100"=>y<="0110011";
when "0101"=>y<="1011011";
when "0110"=>y<="1011111";
when "0111"=>y<="1110000";
when "1000"=>y<="1111111";
when "1001"=>y<="1111011";
when "1010"=>y<="1110111";
when "1011"=>y<="0011111";
when "1100"=>y<="1001110";
when "1101"=>y<="0111101";
when "1110"=>y<="1001111";
when "1111"=>y<="1000111";
when others=>y<="XXXXXXX";
end case;
111
太原理工大学 夏路易

end process;
end behave;
该 7 段译码器的仿真结果见图 3.4.10。图中数字 126,48 等是 7 段笔划的编码。

图 3.4.10

四位十进制计数七段译码电路
设计文件:
LIBRARY ieee;
USE ieee.std_logic_1164.all;
ENTITY cou47seg IS
PORT (clk, reset,ena : IN std_LOGIC;
seg1 : out std_logic_vector(6 downto 0);
seg2 : out std_logic_vector(6 downto 0);
seg3 : out std_logic_vector(6 downto 0);
seg4 : out std_logic_vector(6 downto 0));
END cou47seg;

ARCHITECTURE x47 OF cou47seg IS


component seg7
port(ii :in std_logic_vector(3 downto 0);
y :out std_logic_vector(6 downto 0));
end component;

component cou4
port(
112
太原理工大学 夏路易

clk,reset,cin : in std_logic;
bcdap : out std_logic_vector(3 downto 0);
bcdbp : out std_logic_vector(3 downto 0);
bcdcp : out std_logic_vector(3 downto 0);
bcddp : out std_logic_vector(3 downto 0)
);
end component;
signal a,b,c,d : std_logic_vector(3 downto 0);
begin
u0:cou4 port map(clk,reset,ena,a,b,c,d);
u1:seg7 port map(a,seg1);
u2:seg7 port map(b,seg2);
u3:seg7 port map(c,seg3);
u4:seg7 port map(d,seg4);
end x47;
计数译码电路的仿真结果见图 3.4.11。

113
太原理工大学 夏路易

图 3.4.11

114

You might also like