MINO-o'-2016 17. FW(2)

テスト用ファーム、その2。

7セグテスト。

#include<avr/io.h>
#include<avr/interrupt.h>
#include<avr/sleep.h>
#include<avr/cpufunc.h>
#include<stdint.h>

#define LATCH595() do{\
  PORTB &= ~(1<<0);\
  _NOP();\
  PORTB |=  (1<<0);\
} while(0)

#define SEND2SPI2(p,s) do{\
  SPDR = (p);\
  while(SPSR&0x80);\
  SPDR = (s);\
} while(0)

EMPTY_INTERRUPT( TIMER0_OVF_vect );

int main(){
  uint8_t column = 0;

  DDRB = 0x2e;  // PB1,2,3,5=OUT
  PORTB = 0x00; // PB1, PB2=L
  DDRD = 0x0d;
  PORTD = 0x0c;

  SPCR = 0x5c;
  SEND2SPI2(0x00,0xff);

  set_sleep_mode( SLEEP_MODE_IDLE );
  sleep_enable();

  TCCR0A = 0x00;
  TMSK0 = 0x01;
  TCNT0 = 0;
  TCCR0B = 0x05;

  sei();

  for(;;){
    switch( column ) {
case 0:
      PORTD = 0x0c;
      LATCH595();
      PORTD = 0x04; // 7SEGA1 to LO
      PORTB = 0x00; // BOTH LED OFF
      SEND2SPI2(0x00,0xff);
      break;
case 4:
      PORTD = 0x0c;
      LATCH595();
      PORTD = 0x08; // 7SEGA0 to LO
      PORTB = 0x02; // LED#1 ON
      SEND2SPI2(0xff,0x00);
      break;
case 8:
      PORTD = 0x0c;
      LATCH595();
      PORTD = 0x04; // 7SEGA1 to LO
      PORTB = 0x04; // LED #2 ON
      SEND2SPI2(0xff,0x00);
      break;
case 12:
      PORTD = 0x0c;
      LATCH595();
      PORTD = 0x08; // 7SEGA0 to LO
      PORTB = 0x06; // BOTH LED ON
      SEND2SPI2(0x00,0xff);
      break;
default:
      break;
    }
    sleep_cpu();
    ++column;
    column &= 0x0f;
  }
}

なんかミスってそうな気もするけど、とりあえず今日はここまで。