目次

スケッチ作成

 Arduinoから信号を与え、TTL_ICの入力とします。
 TTL_ICの出力を、Arduinoで拾って、表示するだけの
 単純な処理で、真理値表、機能表は作成できます。



 Arduinoからは6出力とし、6入力で
 TTL_ICの出力を拾うことにします。

 TTL_ICは、電源電圧に+5Vを利用するので
 Arduino基板の+5V、GNDを接続します。



 ArduinoとTTL_ICとのやり取りは、ポートC、Bを
 利用します。



 ポートBから出力する論理値は、シリアルインタフェース
 を利用して設定できるようにします。また、ポートCから
 の入力値を端末に表示します。



 端末ソフトは、TeraTermを利用。

 出力値設定、入力値表示が必要なので、1文字コマンド
 を次のように決めました。

 ポートBの出力値を指定、ポートCの値表示が
 わかりやすいように、2進数で扱います。

 仕様を確定したので、2つの関数を定義。

 setup

  ポートB、Cの初期値と入出力方向を指定します。
    /* initialize pin state */
    PORTB = 0xff ;
    PORTC = 0xff ;

    /* set data direction */
    DDRB = 0xff ;
    DDRC = 0x00 ;

  シリアルインタフェースでコマンドとパラメータを
  受信するので、バッファとバッファ中の文字位置を
  利用します。配列と変数を定義します。
    char sbuf[8] ;
    char sindex ;

  文字位置を格納している変数を初期化しておきます。
    /* initialize variables */
    sindex = 0 ;

  必要と思われる変数を宣言します。
    char uflag ;
    char cmd ;
    char loopx ;
    unsigned char tmp ;
    char val ;

  まとめると、以下。

   #define OFF 0
   #define ON  OFF+1

   void setup()
   {
      /* initialize serial port */
      Serial.begin(9600);
     /* initialize pin state */
     PORTB = 0xff ;
     PORTC = 0xff ;
     /* set data direction */
     DDRB = 0xff ;
     DDRC = 0x00 ;
     /* initialize variables */
     sindex = 0 ;
     uflag = OFF ;
     cmd = 0 ;
     loopx = 0 ;
     tmp = 0 ;
     val = 0 ;
   }

 loop

  受信バッファにコマンド、パラメータが'\r'で
  終端したレコードで入力されたとき、動作する
  よう、シーケンスを作成します。

     /* command intepreter */
     if ( uflag == ON ) {
       /* clear flag */
       uflag = OFF ;
        /*
          ??????????
        */
      }

  受信バッファから、コマンドを取出して
  対応する機能実現コードに処理を任せます。

     /* command intepreter */
     if ( uflag == ON ) {
       /* clear flag */
       uflag = OFF ;
        /* new line */
        crlf() ;
        /* get command */
        cmd = sbuf[0] ;
        /* judge */
        if ( cmd == '?' ) {

        }
        if ( cmd == 'o' ) {

        }
        if ( cmd == 'i' ) {

        }
      }

  コマンド'?'はヘルプなので、文字列を出力する
  関数を呼び出します。
        if ( cmd == '?' ) {
          show_help();
        }

  コマンド'o'は6ビットの値を、ポートBに出力
  するので、次のシーケンスで実現します。
  1. 変数をゼロクリア
  2. 3から6を6回繰り返す
  3. 変数を1ビット左シフト
  4. 受信バッファから1文字取り出し
  5. 1文字が'1'ならば、変数のLSBに1を加える
  6. 受信バッファポインタを1進める
  7. 変数をポートBに出力
  コードにまとめます。 if ( cmd == 'o' ) { tmp = 0 ; for ( loopx = 1 ; loopx <= 6 ; loopx++ ) { /* shift */ tmp <<= 1 ; /* judge */ if ( sbuf[loopx] == '\r' ) break ; /* add */ if ( sbuf[loopx] == '1' ) { tmp |= ON ; } } /* impress */ PORTB = tmp ; }   コマンド'i'はポートCの6ビット値を、端末に   転送すればよいので、次シーケンスで実現します。
  1. ポートCの値を、変数に転送
  2. LSBから6ビット目の値を判定し、'1'か'0'を出力
  3. 変数を1ビット左シフト
  4. 6回処理していれば終了、それ以外は1にもどる
  処理コードは、以下。 if ( cmd == 'i' ) { /* get value */ tmp = PINC ; /* show */ Serial.write(' ') ; for ( loopx = 0 ; loopx < 6 ; loopx++ ) { /* default */ val = '0' ; /* judge */ if ( tmp & 0x20 ) { val++ ; } /* show */ Serial.write(val) ; /* shift */ tmp <<= 1 ; } /* new line */ crlf(); }   ここまでの内容を、まとめると以下。   void loop()   {    /* command intepreter */   if ( uflag == ON ) {   /* clear flag */   uflag = OFF ;   /* new line */   crlf() ;   /* get command */   cmd = sbuf[0] ;   /* judge */   if ( cmd == '?' ) {   show_help() ;   }   if ( cmd == 'o' ) {   tmp = 0 ;   for ( loopx = 1 ; loopx <= 6 ; loopx++ ) {   /* shift */   tmp <<= 1 ;   /* judge */   if ( sbuf[loopx] == '\r' ) break ;   /* add */   if ( sbuf[loopx] == '1' ) { tmp |= ON ; }   }   /* impress */   PORTB = tmp ;   }   if ( cmd == 'i' ) {   /* get value */   tmp = PINC ;   /* show */   for ( loopx = 0 ; loopx < 6 ; loopx++ ) {   /* default */   val = '0' ;   /* judge */   if ( tmp & 0x20 ) { val++ ; }   /* show */   Serial.write(val) ; /* shift */ tmp <<= 1 ;   }   /* new line */   crlf();   }   }   }  受信割込みハンドラ   端末からコマンドとパラメータが、いつ転送されるのか   わからないので、割込みを使い、取りこぼさないように   します。   serialEventを利用し、受信割込みハンドラ   を定義するので、次のように1文字転送の   たびに、受信バッファに保存し、'\r'を検出   したら、loop()にフラグで通知します。   void serialEvent()   {    char ch;    if ( Serial.available() > 0 ) {    /* get 1 charactor */    ch = Serial.read();    /* store */    sbuf[sindex] = ch ;    /* increment */    sindex++ ;    /* judge */    if ( ch == '\r' ) {    sindex = 0 ;    uflag = ON ;    }    }   }  スケッチとして、まとめます。 #define OFF 0 #define ON OFF+1 char sbuf[8] ; char sindex ; char uflag ; char cmd ; char loopx ; unsigned char tmp ; char val ; void serialEvent(); void crlf() { Serial.write('\r'); Serial.write('\n'); } void show_help() { Serial.println("? help"); Serial.println("o put bit pattern"); Serial.println("i get bit pattern"); } /* initialize */ void setup() { /* initialize serial port */ Serial.begin(9600); /* */ PORTB = 0xff ; PORTB = 0xff ; /* data direction */ DDRB = 0xff ; DDRC = 0x00 ; /* initialize variables */ sindex = 0 ; uflag = OFF ; cmd = 0 ; loopx = 0 ; tmp = 0 ; val = 0 ; } /* main */ void loop() { /* command intepreter */ if ( uflag == ON ) { /* clear flag */ uflag = OFF ; /* new line */ crlf() ; /* get command */ cmd = sbuf[0] ; /* judge */ if ( cmd == '?' ) { show_help() ; } if ( cmd == 'o' ) { tmp = 0 ; for ( loopx = 1 ; loopx <= 6 ; loopx++ ) { /* shift */ tmp <<= 1 ; /* judge */ if ( sbuf[loopx] == '\r' ) break ; /* add */ if ( sbuf[loopx] == '1' ) { tmp |= ON ; } } /* impress */ PORTB = tmp ; } if ( cmd == 'i' ) { /* get value */ tmp = PINC ; /* show */ Serial.write(' ') ; for ( loopx = 0 ; loopx < 6 ; loopx++ ) { /* default */ val = '0' ; /* judge */ if ( tmp & 0x20 ) { val++ ; } /* show */ Serial.write(val) ; /* shift */ tmp <<= 1 ; } /* new line */ crlf(); } } } /* receive interrupt */ void serialEvent() { char ch; if ( Serial.available() > 0 ) { /* get 1 charactor */ ch = Serial.read(); /* store */ sbuf[sindex] = ch ; /* increment */ sindex++ ; /* judge */ if ( ch == '\r' ) { sindex = 0 ; uflag = ON ; } } }

目次

inserted by FC2 system