マイコンで生成していた1 Hzクロック、10 HzクロックをFPGAで生成する

前回まで1 Hzクロック、10 HzクロックはATmega328Pで生成していたが今度はFPGAで生成する。デバイスもMAX VからMAX10へ移行した。

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity auto_clk is
    generic(
        F_CLK  : natural := 48_000_000;
        FREQ_LO: natural := 1;
        FREQ_HI: natural := 10
    );
    port(
        clk    : in  std_logic;
        sel    : in  std_logic; -- when 0, FREQ_LO; when 1, FREQ_HI
        clk_out: out std_logic
    );
end entity;

architecture rtl of auto_clk is
begin

    process(all)
        variable count_max: natural range 0 to F_CLK/2-1;
        variable counter  : natural range 0 to F_CLK/2-1 := 0;
    begin

        case sel is
            when '0'    => count_max := F_CLK/(FREQ_LO*2)-1;
            when '1'    => count_max := F_CLK/(FREQ_HI*2)-1;
            when others => count_max := F_CLK/2-1; -- 1 Hz
        end case;
    
        if rising_edge(clk) then
            if counter >= count_max then
                clk_out <= not clk_out;
                counter := 0;
            else
                counter := counter + 1;
            end if;
        end if;
    end process;

end architecture;

f:id:ti-nspire:20210830153029p:plain:w550 f:id:ti-nspire:20210830153032p:plain:w550