1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ずっとFPGA初心者(その5:遅延)

Posted at

遅延

今回は、Delay(遅延)を実現する。用いるボードおよび開発環境は、すべてその1と同じである。

実現したいこと

ボタン押下3秒後にLED点灯。

VHDLコード

ClkLedDelay.vhd
library ieee;
use ieee.std_logic_1164.all;

entity ClkLedDelay is
  port (
    BTN: in std_logic;
    CLK: in std_logic;
    LED: out std_logic);
end ClkLedDelay;

architecture RTL of ClkLedDelay is
constant CYCLE : integer := 3*50000000;  -- ①
signal CNT: integer range 0 to CYCLE-1 := 0;  -- ②
signal TimerFlag: std_logic := '0';  -- ③
signal LedFlag: std_logic := '1'; -- Off  ④
	
begin	
  process (BTN, CLK) -- ⑤
    begin
      if (BTN'event and BTN='0') then	-- Pushed
        TimerFlag <= '1'; -- ⑥
      end if;	
      if (rising_edge(CLK)) then
        if (TimerFlag='1') then
          if (CNT = CYCLE-1) then
            LedFlag <= '0'; -- On  ⑦
          else
            CNT <= CNT+1;  -- ⑧
          end if;
        end if;
      end if;
    end process;
	
    LED <= LedFlag; -- ⑨
end RTL;

コードの解説。

①:3秒の定義(用いるボードのクロックは50MHzのため)
②:カウンタ(0で初期化)
③:カウンタ開始のためのフラグ
④:LED制御(On/Off)フラグ(負論理)
⑤:ボタン(BTN)やクロック(CLK)に変化があったときに処理されるProcess文
⑥:ボタン押下時にカウンタ開始フラグOn
⑦:クロック立ち上がり、かつ、カウンタが3秒経過後に、LED制御フラグOn
⑧:クロック立ち上がり、かつ、カウンタ開始フラグOnの時に、カウンタインクリメント
⑨:LED制御フラグの値をLEDに代入

⑤と⑨とは同時に実行されることに注意。その2参照。

C'est tout!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?