STM32L010でソフトウェアI2Cで遊ぶ




x Mbed2です。

x ANK時もPP(プルプッシュ)でしているので注意 修正済み2022/5/3 7:09


目的
I2C-USART用


![o_con380.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/c28ee822-03ab-330f-0ff6-faed7fbf06e6.jpeg)




![o_con372.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/b7265368-f62e-ba32-5ef6-d62ccc85307c.jpeg)


![o_con381.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/7918e6d2-312a-a390-eb17-d3c430b30167.jpeg)




イメージです。


![o_con368.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/0f52c4d3-7e18-ab97-44e1-985db7cec66a.jpeg)


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


いろいろ修正




```c++



//soft_i2c_test_010_2

#include "mbed.h"

DigitalInOut SDA1(PA_10, PIN_OUTPUT,OpenDrain,0);
DigitalInOut SCL1(PA_9, PIN_OUTPUT,OpenDrain,0);

//A5
#define SCK_0_HIGH()  SCL1=1
#define SCK_0_LOW()   SCL1=0

//A4
#define SDA_0_HIGH()  SDA1=1
#define SDA_0_LOW()   SDA1=0

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


int soft_i2c(char add_p, char data_p)
{

    int ii; //カウンター

    //待ち
    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();
        }//endif
        myDelay();
        SCK_0_HIGH();
        myDelay();
    }//for

    //ack
    SCK_0_LOW();
    SDA_0_HIGH();
    myDelay();
    SCK_0_HIGH();
    myDelay();
 
    for (ii = 0; ii < 8; ii++) {
        SCK_0_LOW();

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

        myDelay();
        SCK_0_HIGH();
        myDelay();
    }//for

    //ack
    SCK_0_LOW();
    SDA_0_HIGH();
    myDelay();
    SCK_0_HIGH();
    myDelay();

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

    return (0);
}//soft_i2c


//メイン関数
int main()
{

    SCL1 = 1;
    SDA1 = 1;

    //無限ループ
    while(1) {

        char *ss1 = "hello world\r\n";
        int ii=0;
        while(ss1[ii] != 0 ){
            soft_i2c(0x80, ss1[ii]);ii++;
        }//while
        
        wait_ms(1000); //1秒の待ち

    }//while

}//main

//容量削減
void error(const char* format, ...){}




```




修正前

```c++



//soft_i2c_test_010_1

#include "mbed.h"

DigitalOut SDA1(PA_10);
DigitalOut SCL1(PA_9);

//A5
#define SCK_0_HIGH()  SCL1=1
#define SCK_0_LOW()   SCL1=0

//A4
#define SDA_0_HIGH()  SDA1=1
#define SDA_0_LOW()   SDA1=0

// 1000us   1kh
//  100us  10kh
//   10us 100kh
#define myDelay()  wait_us(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;}

*/





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);
}


//メイン関数
int main()
{

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

  SCL1 = 1;
  SDA1 = 1;

while(1) {

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

}//while


}//main





```