ATtiny85の赤外線受信


前回はIRremote ライブラリを 使った赤外線の受信を紹介しました。
こちらで ライブラリを一切使わずに、ATtiny85での赤外線の受信方法が公開されています。
ATtiny85に以下のスケッチを1MHz(Internal)のクロックで書き込みます。
ATtiny85のシリアル出力ポートはPB0にアサインされています。
/* ATtiny85 IR Remote Control Receiver

   David Johnson-Davies - www.technoblogy.com - 3rd April 2015
   ATtiny85 @ 1 MHz (internal oscillator; BOD disabled)
   
   CC BY 4.0
   Licensed under a Creative Commons Attribution 4.0 International license:
   http://creativecommons.org/licenses/by/4.0/

   http://www.technoblogy.com/show?V6F
   
*/

volatile int NextBit;
volatile unsigned long RecdData;
int Brightness;

// Demo routine
void ReceivedCode(boolean Repeat) {
//  char buff[64];
//  sprintf(buff,"%d,%x",Repeat,RecdData);
//  mySerial.println(buff);
  Serial.print(Repeat);
  Serial.print(",");
  Serial.println(RecdData,HEX);
}


// Interrupt service routine - called on every falling edge of PB2
ISR(INT0_vect) {
  int Time = TCNT0;
  int Overflow = TIFR & 1<<TOV0;
  // Keep looking for AGC pulse and gap
  if (NextBit == 32) {
    if ((Time >= 194) && (Time <= 228) && (Overflow == 0)) {
      RecdData = 0; NextBit = 0;
    } else if ((Time >= 159) && (Time <= 193) && (Overflow == 0)) ReceivedCode(1);
  // Data bit
  } else {
    if ((Time > 44) || (Overflow != 0)) NextBit = 32; // Invalid - restart
    else {
      if (Time > 26) RecdData = RecdData | ((unsigned long) 1<<NextBit);
      if (NextBit == 31) ReceivedCode(0);
      NextBit++;
    }
  }
  TCNT0 = 0;                  // Clear counter
  TIFR = TIFR | 1<<TOV0;      // Clear overflow
  GIFR = GIFR | 1<<INTF0;     // Clear INT0 flag
}

// Setup **********************************************
 
void setup() {
  // Set up Timer/Counter0 (assumes 1MHz clock)
  TCCR0A = 0;                 // No compare matches
  TCCR0B = 3<<CS00;           // Prescaler /64
  // Set up INT0 interrupt on PB2
  MCUCR = MCUCR | 2<<ISC00;   // Interrupt on falling edge
  GIMSK = GIMSK | 1<<INT0;    // Enable INT0
  NextBit = 32;               // Wait for AGC start pulse

  Serial.begin(9600);
  Serial.println("ATtiny 85 IR Receive demo start");
}

void loop() {
}

スケッチを実行して、赤外線受信機に向けてリモコンを発射してみました。


IRremoteライブラリを使って受信した時とはコードが違いますが、ON/OFFコードの違いは識別できます。


試しに8MHz(Internal)のクロックで書き込んでみましたが、全く動きませんでした。
詳しい方なら、ATtiny85以外のモデルにも移植できると思いますが、私には無理です。

続く...