呵呵,好爽啊,前面几个星期看电源看得我头都大啦,也落了不少课,前几天我突然发现我的EDA落了很多东西啦,唉,心想,把电源放放,看看EDA再说,突然有了兴趣,学起来也挺快的,前面两天我写了一个能够实现加减的计数器,并且可以选择加1,10,100,1000。呵呵,这个程序写下来,对VHDL里面的状态机有了初步的了解,遇到的问题挺多的,比如说一个进程里面是不能同时有上升沿和下降沿判断的。可是我还没有通过硬件测试,我只是简单的用软件仿真看了下,感觉是对的。明天做实验时检测正确后,我再把源码贴上来。今天从下午6点多到现在才把这个分频器个整出来。具体的功能是:对一个频率进行任意分频,分频数可以通过按键输入,且分频数可以加减,不过加减数为1。还有一点不足的是分频数没有通过数码管显示出来。等明天把我前两天写得计数器调试好后有时间再添上去吧,因为它只是一个例化语句的使用,应该不会难倒我。
下面是分频器的源代码:
【注】:仿真图我就不贴上来啦,提醒一下,仿真时要把pclk设置几个脉冲后,后面就让它为低电平,因为电脑有点难受的。还有rst开始一段很短时间要设置为高电平,clk要在pclk为低电平后在设置它的时钟。至于怎么设,相信大家应该可以搞定。这样设置有利于看仿真结果。同时电脑的CPU不至于崩溃。
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity foudiv is
port(
clk,pclk :in std_logic; --clk:待分频时钟 pclk:按键
clkmd:in std_logic;--按键加减模式
rst:in std_logic;--复位,
fout:out std_logic--频率输出
);
end;
architecture lammy of foudiv is
signal up:std_logic;--上升沿计数结束标志位
signal do:std_logic;--下降沿计数结束标志位
signal full:std_logic;--分频频率输出状态位
signal fullup:std_logic;--分频频率翻转标志位
signal fulldo:std_logic;--分频频率翻转标志位
signal db:std_logic_vector(7 downto 0);--分频数
signal du:std_logic_vector(7 downto 0);--上升沿计数器
signal dd:std_logic_vector(7 downto 0);--下降沿计数器
begin
lammy01:process(pclk,rst,clkmd)
begin
if rst='1' then db<=(others=>'0');
elsif pclk'event and pclk='1' then
if clkmd='1' then db<=db+1;
elsif clkmd='0' then db<=db-1;
end if;
end if;
end process;
lammy02:process(clk)
variable updata:std_logic_vector(7 downto 0);
begin
if rst='1' then fullup<='0';
elsif clk'event and clk='1' then
-- if updata=db-1 then updata:=(others=>'0');
if do='1' then updata:=(others=>'0');
end if;
updata:=updata+1;
if dd+updata=db then updata:=(others=>'0');fullup<='1';up<='1';
else fullup<='0';up<='0';
end if;
end if;
du<=updata;
end process;
lammy03:process(clk)
variable dodata:std_logic_vector(7 downto 0);
begin
if rst='1' then fulldo<='0';
elsif clk'event and clk='0' then
-- if dodata=db-1 then dodata:=(others=>'0');
if up='1' then dodata:=(others=>'0');
end if;
dodata:=dodata+1;
if du+dodata=db then dodata:=(others=>'0');fulldo<='1';do<='1';
else fulldo<='0';do<='0';
end if;
end if;
dd<=dodata;
end process;
lammy04:process(fulldo,fullup)
begin
full<=fulldo or fullup;
end process;
lammy05:process(full)
variable cnt2:std_logic;
begin
if full'event and full='1' then cnt2:=not cnt2;
if cnt2='1' then fout<='1';
else fout<='0';
end if;
end if;
end process;
end;
|