Skip to content

Instantly share code, notes, and snippets.

@akirayou
Last active September 14, 2024 15:04
Show Gist options
  • Save akirayou/9de76f88316e2ec121ddc27a4b2d054d to your computer and use it in GitHub Desktop.
Save akirayou/9de76f88316e2ec121ddc27a4b2d054d to your computer and use it in GitHub Desktop.
ATTiny10用の小電力Lチカの例。
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#define sbi(x, b) (x) |= 1 << (b)
#define cbi(x, b) (x) &= ~(1 << (b))
ISR(WDT_vect) {} // ウォッチドックたいま割込
// WDTタイマ割込があるまで眠る操作 (INT0のLowレベル/ピン変化割込でも起きるので併用時注意)
void sleep_with_wdt()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sei();
sleep_cpu();
sleep_disable();
}
int main()
{
CCP = 0xD8; // CLKMSR, CLKPSR, WDTCSRに書き込む前に必要な操作 
CLKMSR = 1; //  CPU クロックを 128kHz wdt オシレータにする
CCP = 0xD8; // CLKMSR, CLKPSR, WDTCSRに書き込む前に必要な操作
CLKPSR = 0; // CPUクロック分周 1
PUEB = 0x00; // no pull up
PRR = 0x03; // ADC/TIMER stop
ACSR = 0x80; // comparetor off
DDRB = 0x0f; // 全pin出力
CCP = 0xD8; // CLKMSR, CLKPSR, WDTCSRに書き込む前に必要な操作
WDTCSR = _BV(WDIE) | 2; // 64ms毎にWDT割込
uint8_t i = 0;
while (1)
{
sbi(PORTB, PORT2);
for (i = 0; i < 10; i++)
sleep_with_wdt();
cbi(PORTB, PORT2);
for (i = 0; i < 10; i++)
sleep_with_wdt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment