ボーイング737でLチカ
提供:泣かないでゆり子
ナビゲーションに移動検索に移動
後輩からボーイング737-700のプラモデルを頂いたので、ただ作るだけじゃ飽き足らずAVRマイコンを内蔵して翼端灯と衝突防止灯を点滅させてみた♪ なかなか贅沢なLチカ。
AVRマイコンのスリープ(パワーダウン)機能を使って、タクトスイッチPUSHでON/OFFできるようにしてみた。
C言語コード
ATtiny13A又はATtiny13Vを使用。
ヒューズビット設定
- SPIEN *
- BODLEVEL 1V8
- CKDIV8 *
- SUT_CKSEL INTRCOSC_128KHZ_14CK_64MS
コード
/* * B737.c * * Created: 2016/03/28 17:56:56 * Author: 市川ゆり子 */ /* --PIN Connection-- 1 RESET: N/C 2 PB3: Beacon light LED Cathode 3 PB4: N/C 4 GND: GND 5 PB2: Right Wing Strobe LED Cathode 6 PB1/INT0: Push SW to GND 7 PB0: Left Wing Strobe LED Cathode 8 Vcc: +5V */ #define F_CPU 16000UL #include <avr/io.h> #include <avr/interrupt.h> #include <avr/sleep.h> #include <util/delay.h> void goToSleep(); ISR(INT0_vect) { cli(); _delay_ms(500); //Chattering Killer sei(); //If PushSW pushed go to sleep. //But When PushSW pushed for resume from sleep, do not go sleep. if(bit_is_clear(MCUCR, SE)) { goToSleep(); } sleep_disable(); } static uint8_t strobe_state = 0; static uint8_t beacon_state = 0; //Timer interrupt routine ISR(TIM0_COMPA_vect) { switch (strobe_state) { case 0: //All Strobe off PORTB |= _BV(PORTB0); PORTB |= _BV(PORTB2); break; case 3: PORTB &= ~_BV(PORTB0); //Left wing strobe on PORTB |= _BV(PORTB2); break; case 4: //All Strobe off PORTB |= _BV(PORTB0); PORTB |= _BV(PORTB2); break; case 15: PORTB |= _BV(PORTB0); PORTB &= ~_BV(PORTB2); //Right wing strobe on break; } switch(beacon_state) { case 0: PORTB &= ~_BV(PORTB3); //Beacon light on break; case 1: PORTB |= _BV(PORTB3); //Beacon light off break; } strobe_state++; if (strobe_state == 16) { strobe_state = 0; } beacon_state++; if(beacon_state == 48) { beacon_state = 0; } } void goToSleep() { PORTB = 0b00111111; //All lights off. set_sleep_mode(_BV(SM1)); //Power down mode sleep_enable(); sleep_cpu(); } int main(void) { //Peripheral initialize. DDRB = 0b00111101; //DDRB1 is in. other is out. PORTB = 0b00000010; //PORTB1 pull up. OCR0A = (unsigned int) 8; //init Counter Compare Register (for 0.5sec interrupt interval) TCCR0A = (1 << WGM01); //CTC counter mode. TCCR0B = (1 << CS01) + (1 << CS00); //Counter clock source clkI/O(128kHz / 8) / 64. TIMSK0 = _BV(OCIE0A); //INT0 interrupt GIMSK = _BV(INT0); //INT0 enable sei(); goToSleep(); sleep_disable(); while(1) { } }