0
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初心者(その7:遅延とリセット&繰り返し)

Posted at

遅延とリセット後のその遅延の繰り返し

今回のお題は、遅延、その後リセットを行い、同じ遅延を繰り返す、、、というもの。その5(遅延)の続きである。用いるボードおよび開発環境は、すべてその1と同じ。

具体的には

ボタン押下3秒後に点灯し、ボタン押下後に消灯(リセット)、その後のボタン押下3秒後に再点灯、ボタン押下後に消灯(リセット)、、、、の繰り返しである。

VHDLコード

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

entity ClkLedDelayReset is
	port (
		BTN:	in std_logic;
		CLK:	in std_logic;
		LED0:   out std_logic;
		LED7:	out std_logic);
end ClkLedDelayReset;

architecture RTL of ClkLedDelayReset is
constant CYCLE : integer := 3*50000000;  -- ① 3秒
signal CNT: integer range 0 to CYCLE-1 := 0;
signal TimerFlag: std_logic := '0';
signal LedFlag: std_logic := '1'; -- Off
	
begin
	process (BTN) -- ②
	begin
		if (BTN'event and BTN='0') then	-- Pushed
			TimerFlag <= not TimerFlag;
		end if;
	end process;

	
	process (CLK) -- ③
	begin
		if (rising_edge(CLK)) then
			if (TimerFlag='1') then
				if (CNT = CYCLE-1) then -- ④
					LedFlag <= '0'; -- On
					-- TimerFlag <= not TimerFlag;  -- ⑤ NG
				else
					CNT <= CNT+1;
				end if;
			else -- ⑥ TimerFlag='0' Reset
				LedFlag <= '1'; -- Off
				CNT <= 0;
			end if;
		end if;
	end process;
	
	LED0 <= LedFlag;  -- ⑦
	LED7 <= BTN; -- ⑧ For debug
end RTL;
  • ①:3秒の定義(用いるボードは50MHzのクロックを持つ)
  • ②(Processブロック全体)、③(Processブロック全体)、⑦、⑧は同時に実行
  • ②:ボタン押下時に、タイマー用フラグ(TimerFlag)反転
  • ④:規定時間(3秒)到達後、LED用フラグON(処理⑦へとつながる)
  • ⑥:TimerFlagがOff時に、LED用フラグOFFかつカウント(CNT)リセット
  • ⑤:ここでのTimerFlag変更は不可(QuartusでBuildエラー = 論理エラー)
  • ⑧:デバッグ用、ボタン押下時に別のLED点灯

動作自体は自明なので、写真、動画、ピンアサインは省略(勘弁)。

補足

2回以上押さないと再点灯しないことがあった。詳細は未調査だが、チャタリングが原因の可能性がある。チャタリング発生状況の把握については、別途検討してみたい。

0
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
0
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?