SSブログ

202duino で リモコン信号をシリアル送信 [Arduino]

文字列と数値(n進法の整数)をシリアル出力する関数もつくって、自作シリアル送信プログラムを赤外線リモコン受信スケッチと組み合わせてみた。

赤外線リモコンの受信を簡単なスケッチで。:放課後マイコンクラブ:SSブログ
https://hello-world.blog.ss-blog.jp/2023-09-09

これと組み合わせて、、
最大2048バイトのフラッシュメモリのうち、スケッチが1576バイト(76%)を使っています。
最大128バイトのRAMのうち、グローバル変数が10バイト(7%)を使っていて、ローカル変数で118バイト使うことができます。

余裕で2kBに収まりました。

202irduio.jpg

// IR Receive and Decode
#define IR_IN   0               // IR Receiver pin
#define TX      2               // Serial TX pin
#define BAUD    9600            // baud rate

void setup() {
  pinMode( TX   , OUTPUT );                     // Tx pin
  pinMode( IR_IN , INPUT );                     // input  Vout(active low, negative logic)
}

void loop() {
  uint32_t  irData = 0;
  uint8_t   irProtocol = 0, irBit = 0, irArray[20] = {0};

  // ****** Receive and Decode IR signals ******
  uint16_t  us = pulseIn( IR_IN, LOW , 65535 ); // Judge the protocol by the length of the leader section (usec.)
  if      ( us > 2000 && us <  3000 ) {         //  *** SIRC ***
      irProtocol = 'S';                         //  T=600us, ON/OFF : leader= 4T/1T, 0=1T/1T, 1=2T/1T 
      for(;(us = pulseIn( IR_IN, LOW , 3000 )); irBit++)
        if( us > 1000 && us <  1500 ) irData |= (1UL << irBit);
  }else if( us > 3500 && us <  5000 ) {         //  *** AEHA ***
      irProtocol = 'A';                         //  T=425us, ON/OFF : leader= 8T/4T, 0=1T/1T, 1=1T/3T, stop=1T
      while( digitalRead( IR_IN ) );
      for(;(us = pulseIn( IR_IN, HIGH, 4000 )); irBit++)
        if( us > 1050 && us <  1500 ) irArray[ irBit / 8 ] |= (1 << (irBit % 8) );
  }else if( us > 7200 && us < 11000 ) {         //  *** NEC ***
      irProtocol = 'N';                         //  T=560us, ON/OFF : leader=16T/8T, 0=1T/1T, 1=1T/3T, stop=1T
      while( digitalRead( IR_IN ) );
      for(;(us = pulseIn( IR_IN, HIGH, 3000 )); irBit++)
        if( us > 1350 && us <  2000 ) irData |= (1UL << irBit);
  }else return;
  
  // ****** Display results by protocol ******
  serialPrintStr( irProtocol == 'S' ? "SIRC: " : irProtocol == 'A' ? "AEHA: " : "NEC : " );
  if( !irBit ) { 
    serialPrintStr( " (repeat code) \n" );
    return;
  }
  serialPrintNum( irBit, DEC );
  serialPrintStr( "bit" ); 
  if( irProtocol == 'A' ) {
    for( uint8_t i = 0; i < ( (irBit + 7) / 8 ); i++ ) {
      serialPrintStr((irArray[i] < 0x10) ? " 0x0" : " 0x" );
      serialPrintNum( irArray[i], HEX );
    }
  } else {
      serialPrintStr( " 0x" );
      serialPrintNum( irData, HEX );
      serialPrintStr( "\t0b" );
      serialPrintNum( irData, BIN );
  }
  serialPrintStr("\n");
}

void inoTX( uint8_t c ) {       // *** OK up to 19200bps at 20MHz(ATtiny202) ***
    digitalWrite( TX,  LOW );                   // start bit
    delayMicroseconds( 1000000 / BAUD );
    for(uint8_t i = 0; i < 8; i++ ) {           // data bits
        digitalWrite( TX, ( c >> i ) & 1 );
        delayMicroseconds( 1000000 / BAUD );
    }
    digitalWrite( TX, HIGH );                   // stop bit
    delayMicroseconds( 1000000 / BAUD );
}

void serialPrintStr( const char *str ) {
    for( ; str[0]; str++ )  inoTX( str[0] );
}

void serialPrintNum( uint32_t num , uint8_t N ) {
    uint8_t n[32], dig = 0;
    do { n[dig++] = num % N; }  while( num /= N );
    for( ; dig; ) inoTX( "0123456789ABCDEF"[ n[--dig] ] );
}

// Uses less RAM, but requires more flash memory and calculations
void serialPrintNum2( uint32_t num , uint8_t N ) {
    uint32_t  t  = num, pwr  = 1;
    for(    ; t /= N  ; pwr *= N );
    for(    ; pwr     ; pwr /= N ) inoTX( "0123456789ABCDEF"[ (num / pwr) % N ] );
}

nice!(0)  コメント(0) 

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。