Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Created July 4, 2021 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/ea86ddc86771156022b98d9eee2c4ab7 to your computer and use it in GitHub Desktop.
Save mamemomonga/ea86ddc86771156022b98d9eee2c4ab7 to your computer and use it in GitHub Desktop.
tinyAVR0(xmega)でWatchdog Timerを使う

tinyAVR0(xmega)でWatchdog Timerを使う

ATtiny1606 + megaTinyCore

Watch Dog Timer(WDT)とはタイマーの一種で、カウンタがいっぱいになるとハードウェアリセットがかかる。プログラムから定期的にWDTをリセットすることで、プログラムのフリーズで強制再起動させることが可能である。

1秒間のWDT(PERIOD)を有効にする

_PROTECTED_WRITE(WDT.CTRLA,WDT_PERIOD_1KCLK_gc);

WDT_PERIOD_1KCLK_gc の他に以下のものが選べる

WDT_PERIOD_OFF_gc = (0x00<<0),  /* Off */
WDT_PERIOD_8CLK_gc = (0x01<<0),  /* 8 cycles (8ms) */
WDT_PERIOD_16CLK_gc = (0x02<<0),  /* 16 cycles (16ms) */
WDT_PERIOD_32CLK_gc = (0x03<<0),  /* 32 cycles (32ms) */
WDT_PERIOD_64CLK_gc = (0x04<<0),  /* 64 cycles (64ms) */
WDT_PERIOD_128CLK_gc = (0x05<<0),  /* 128 cycles (0.128s) */
WDT_PERIOD_256CLK_gc = (0x06<<0),  /* 256 cycles (0.256s) */
WDT_PERIOD_512CLK_gc = (0x07<<0),  /* 512 cycles (0.512s) */
WDT_PERIOD_1KCLK_gc = (0x08<<0),  /* 1K cycles (1.0s) */
WDT_PERIOD_2KCLK_gc = (0x09<<0),  /* 2K cycles (2.0s) */
WDT_PERIOD_4KCLK_gc = (0x0A<<0),  /* 4K cycles (4.1s) */
WDT_PERIOD_8KCLK_gc = (0x0B<<0),  /* 8K cycles (8.2s) */

WDTをリセットする

コードに埋め込んでいい感じにリセットをかける。上で設定した期間内にWDRが発行されないとハードウェアにリセットがかかる。

asm("WDR");

WDT(PERIOD)を無効にする

_PROTECTED_WRITE(WDT.CTRLA,WDT_PERIOD_OFF_gc);

参考資料

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment