Arduino UNOでソフトウェアI2Cで遊ぶ



x @7of9さんの記事を非営利、研究目的でArduino UNOに移植

x ANK時もppでしているので注意


目的
I2C-USART用


![o_con369.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/ef28527c-b4c9-aec3-4931-dbeddde9b09b.jpeg)


デバッグ時


![o_con368.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/5fe41c46-d9da-40df-9eac-64cd86068bbe.jpeg)



![o_con370.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/a8a7aba5-2d84-391f-f423-df7de6a99026.jpeg)


https://qiita.com/caa45040/items/5032e6905c9425f41495



参考




https://qiita.com/7of9/items/a1a42359cff9ed06778f



```c++

//soft_i2c_test_uno_1


//A5
#define SCK_0_HIGH()  digitalWrite(A5, HIGH)
#define SCK_0_LOW()   digitalWrite(A5, LOW)

//A4
#define SDA_0_HIGH()  digitalWrite(A4, HIGH)
#define SDA_0_LOW()   digitalWrite(A4, LOW)
#define SDA_O         A4


// 1000us   1kh
//  100us  10kh
//   10us 100kh
#define myDelay()  delayMicroseconds(100);

//debug
/*

  int d_sda = 0;
  int d_sck = 0;

  void debug1(){

  Serial.print("300,");
  Serial.print( (d_sda * 100)+20 );
  Serial.print( ',' );
  Serial.print( d_sck * 100 + 10);
  Serial.print(",0");
  Serial.println(" ");

  }

  void myDelay(){

  for(int ii=0;ii<10;ii++){
    delay(100);
    debug1();
  }

  }

  //A5
  void SCK_0_HIGH()  {digitalWrite(A5, HIGH);d_sck =1;}
  void SCK_0_LOW()   {digitalWrite(A5, LOW); d_sck =0;}

  //A4
  void SDA_0_HIGH()  {digitalWrite(A4, HIGH);d_sda = 1;}
  void SDA_0_LOW()   {digitalWrite(A4, LOW); d_sda = 0;}

*/


void setup() {

  //debug
  //シリアルポートの初期化
  //Serial.begin(9600);

  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);
  digitalWrite(A4, HIGH);
  digitalWrite(A5, HIGH);

}


int soft_i2c(char add_p, char data_p) {

  int ii;

  //  for (ii = 0;ii<15;ii++){
  //   digitalWrite(13, HIGH);delay(20);digitalWrite(13, LOW);delay(20);
  //  }

  //待ち
  SDA_0_HIGH();
  SCK_0_HIGH();
  myDelay();
  myDelay();
  myDelay();

  //START
  SDA_0_LOW();
  myDelay();
  SCK_0_LOW();
  myDelay();


  for (ii = 0; ii < 8; ii++) {
    SCK_0_LOW();
    if ( ((add_p << ii) & 0x80) == 0 ) {
      SDA_0_LOW();
    } else {
      SDA_0_HIGH();
    }
    myDelay();
    SCK_0_HIGH();
    myDelay();
  }

  //ack
  SCK_0_LOW();
  SDA_0_HIGH();
  //pinMode(SDA_O, INPUT);
  myDelay();
  SCK_0_HIGH();
  myDelay();
  //pinMode(SDA_O, OUTPUT); digitalWrite(SDA_O, HIGH);

  //    //debug
  //    SCK_0_HIGH();SDA_0_HIGH();
  //    myDelay();myDelay();myDelay();

  for (ii = 0; ii < 8; ii++) {
    SCK_0_LOW();

    if ( ((data_p << ii) & 0x80) == 0 ) {
      SDA_0_LOW();
    } else {
      SDA_0_HIGH();
    }

    myDelay();
    SCK_0_HIGH();
    myDelay();
  }

  //ack
  SCK_0_LOW();
  SDA_0_HIGH();
  //pinMode(SDA_O, INPUT);
  myDelay();
  SCK_0_HIGH();
  myDelay();
  //pinMode(SDA_O, OUTPUT); digitalWrite(SDA_O, HIGH);

  //    //debug
  //    SCK_0_HIGH();SDA_0_HIGH();
  //    myDelay();myDelay();myDelay();

  //STOP
  SCK_0_LOW();
  SDA_0_LOW();
  myDelay();
  SCK_0_HIGH();
  myDelay();
  SDA_0_HIGH();
  myDelay();


  //    //debug
  //    SCK_0_HIGH();SDA_0_HIGH();
  //    myDelay();myDelay();myDelay();
  //    //debug
  //    SCK_0_HIGH();SDA_0_HIGH();
  //    myDelay();myDelay();myDelay();

  return (0);
}

void loop() {

  soft_i2c(0x80, '0');
  delay(1000); //1秒の待ち

}



```