状态机思想编程

1. LED流水灯的FPGA代码

一个使用状态机思想来实现LED流水灯的FPGA代码

这个例子采用VHDL编写

VHDL代码示例:
复制代码
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity led_flowing is
    Port ( clk   : in  std_logic;
           reset : in  std_logic;
           led   : out std_logic_vector(7 downto 0));
end led_flowing;

architecture Behavioral of led_flowing is
    type state_type is (s0, s1, s2, s3, s4, s5, s6, s7);
    signal current_state, next_state : state_type;
    signal count : integer := 0;
    
begin
    process(clk, reset)
    begin
        if reset = '1' then
            current_state <= s0;
        elsif rising_edge(clk) then
            current_state <= next_state;
        end if;
    end process;

    process(current_state)
    begin
        case current_state is
            when s0 =>
                led <= "00000001";  -- 灯1亮
                next_state <= s1;
            when s1 =>
                led <= "00000010";  -- 灯2亮
                next_state <= s2;
            when s2 =>
                led <= "00000100";  -- 灯3亮
                next_state <= s3;
            when s3 =>
                led <= "00001000";  -- 灯4亮
                next_state <= s4;
            when s4 =>
                led <= "00010000";  -- 灯5亮
                next_state <= s5;
            when s5 =>
                led <= "00100000";  -- 灯6亮
                next_state <= s6;
            when s6 =>
                led <= "01000000";  -- 灯7亮
                next_state <= s7;
            when s7 =>
                led <= "10000000";  -- 灯8亮
                next_state <= s0;
            when others =>
                led <= "00000000";  -- 关闭所有灯
                next_state <= s0;
        end case;
    end process;

end Behavioral;
仿真测试代码:
复制代码
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity tb_led_flowing is
end tb_led_flowing;

architecture Behavioral of tb_led_flowing is

    signal clk   : std_logic := '0';
    signal reset : std_logic := '0';
    signal led   : std_logic_vector(7 downto 0);

    constant CLK_PERIOD : time := 10 ns;

begin

    uut: entity work.led_flowing
        port map (
            clk => clk,
            reset => reset,
            led => led
        );

    -- Clock generation
    clk_process :process
    begin
        while True loop
            clk <= '0';
            wait for CLK_PERIOD/2;
            clk <= '1';
            wait for CLK_PERIOD/2;
        end loop;
    end process;

    -- Stimulus process
    stim_proc: process
    begin
        reset <= '1';
        wait for 20 ns;
        reset <= '0';
        wait for 300 ns; -- 放置足够的时间进行观察
        assert false report "End of simulation" severity note;
        wait;
    end process;

end Behavioral;

流水灯演示:

2. CPLD和FPGA芯片的主要技术区别

CPLD(复杂可编程逻辑器件)与FPGA(现场可编程门阵列)的主要区别:

1、结构与规模

CPLD :通常具有较小的逻辑单元和较低的延迟,适合简单的组合逻辑和小规模状态机。FPGA:具有较大的逻辑块(逻辑单元),可以支持更复杂的设计和更高的并行处理能力。

2、应用场合

CPLD :适合用于控制逻辑、状态机、小型接口或数据处理,通常用于低功耗、高速的应用。244FPGA:适用于需要处理大量并行信号和复杂算法的应用,如图像处理、数据加速等。

相关推荐
v1326656236819 分钟前
博通集成:BK7239N 双频wifi6 超低功耗 iot芯片
嵌入式硬件·物联网·iot·双频wifi
768dh20 分钟前
TL431+光耦反馈电路
单片机·嵌入式硬件
ShiMetaPi24 分钟前
NeurIPS 2024 | 丝滑视觉新极限:EPA 框架利用事件相机突破插帧伪影瓶颈
人工智能·嵌入式硬件·计算机视觉·自动驾驶·事件相机·evs
水云桐程序员37 分钟前
LED电路的设计原理
单片机·嵌入式硬件
charlie1145141911 小时前
嵌入式现代C++教程实战篇第12篇:C宏时代的LED驱动 —— 能跑但不优雅
c语言·c++·stm32·单片机·嵌入式硬件·c
西城微科方案开发1 小时前
八电极AC体脂秤单片机BH66F2660-B LQFP48
单片机
每天进步一点点️1 小时前
CMN600AE——片上总线
嵌入式硬件·soc·芯片
cici158741 小时前
51单片机实时温度监测系统(DS18B20 + LCD1602)
单片机·嵌入式硬件·51单片机
桃里桑1 小时前
【嵌入式硬件】红外发射接收应用电路
嵌入式硬件
beleadsensors2 小时前
模数转换ADC(上):模数转换(ADC)核心原理与关键概念
单片机·嵌入式硬件·硬件架构·硬件工程·pcb工艺