VHDL呼吸灯源码解析

VHDL呼吸灯源码解析

Date:2016/10/27

CSDN主页

固有声明使用IEEE库和标准逻辑器件

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

申明器件实体,输入和输出接口
其中clkin为时钟输入信号,clkout和clkout2为呼吸灯输出信号.异步输出呼吸灯效果

entity led is 
port (
clkin:in std_logic;
clkout:out std_logic;
clkout2:out std_logic
);
end entity;

代码实体部分,对接入引脚和输出引脚进行逻辑定义,在这个过程中我首先对时钟进行分频
我在之前测试时钟频率的时候,发现在分频2的24次方可以让灯以一秒左右一闪的频率闪动,可以由此反推时钟频率大概为2的24次方左右也就是大概8MHz-12MHz左右.
我们需要定一个单次呼和单次吸的时间周期.既然是呼吸灯,自然不能周期太短.也就是说最后我们要实现的频率不能比2的24次方短

architecture behave of led is 
signal mlow:std_logic_vector(9 downto 0);
signal mhigh:std_logic_vector(15 downto 0);
signal mcur:std_logic_vector(15 downto 0);
signal mtype:std_logic;
begin 
process(clkin)
begin
    if(clkin'event and clkin='1')then
        mcur<=mcur+1;
        if(mcur="1111111111111111")then 
        mhigh<=(others=>'0');
        mcur<=(others=>'0');
        end if;
        mlow <=mlow+1;
        if(mlow="1111111111")then
            mlow<=(others=>'0');
            mhigh<=mhigh+1;
        end if;
        if(  mhigh="1111111111111111")then
            mhigh<=(others=>'0');
            mtype<=not mtype;
        end if;
        if(mtype='1')then 
            if(mcur > mhigh)then 
            clkout<='1';
            clkout2<='0';
            else
            clkout<='0';
            clkout2<='1';
            end if;
        else
            if(mcur > mhigh)then 
            clkout<='0';
            clkout2<='1';
            else
            clkout<='1';
            clkout2<='0';
            end if;
        end if;
    end if;
    end process;
    --clkout<=t_o;
end behave;

完整代码

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

entity led is 
port (
clkin:in std_logic;
clkout:out std_logic;
clkout2:out std_logic
);
end entity;
architecture behave of led is 
signal mlow:std_logic_vector(9 downto 0);
signal mhigh:std_logic_vector(15 downto 0);
signal mcur:std_logic_vector(15 downto 0);
signal mtype:std_logic;
begin 
process(clkin)
begin
    if(clkin'event and clkin='1')then
        mcur<=mcur+1;
        if(mcur="1111111111111111")then 
        mhigh<=(others=>'0');
        mcur<=(others=>'0');
        end if;
        mlow <=mlow+1;
        if(mlow="1111111111")then
            mlow<=(others=>'0');
            mhigh<=mhigh+1;
        end if;
        if(  mhigh="1111111111111111")then
            mhigh<=(others=>'0');
            mtype<=not mtype;
        end if;
        if(mtype='1')then 
            if(mcur > mhigh)then 
            clkout<='1';
            clkout2<='0';
            else
            clkout<='0';
            clkout2<='1';
            end if;
        else
            if(mcur > mhigh)then 
            clkout<='0';
            clkout2<='1';
            else
            clkout<='1';
            clkout2<='0';
            end if;
        end if;
    end if;
    end process;
    --clkout<=t_o;
end behave;  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章