1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

STM32C011でI2Cスレーブから「46」を読み込む(STM32C011J4M7)

Posted at

STM32C011でI2Cスレーブから「46」を読み込む(STM32C011J4M7)

Arduino 電子工作 I2C STM32 STM32C011

x プルアップも忘れずに

目的
I2Cスレーブの読み取りテスト

結果

o_cop976.jpg

o_cop977.jpg

受信(スレーブ)



//I2C_Slave_46_C011_1


#include <Arduino.h>
#include <Wire.h>


//初期化処理
void setup() {

  //I2Cの初期化
  Wire.setSDA(19);   //PA10
  Wire.setSCL(18);   //PA9
  Wire.begin( (0x80)>>1 ); // Slave ID #80
  Wire.onRequest(requestEvent);

}//setup


//メインループ
void loop() {
}//loop


//I2Cの読み取り関数
void requestEvent() {

  //値の送信
  Wire.write( 46 );

}//requestEvent



送信(マスター)



//I2C_Master_46_C011_1


#include <Arduino.h>
#include <Wire.h>
#include "SER_9600_C011_1.h"

//初期化
void setup()
{

  delay(3000); //not delete

  //シリアルの初期化
  pc.beginNS(9600);

  pc.printNS("\r\nSTART\r\n"); //スタートと表示

  //I2Cの初期化
  Wire.setSDA(19);   //PA10
  Wire.setSCL(18);   //PA9
  Wire.begin();  //I2Cの初期化

}//setup


//メインループ
void loop()
{

  int  b = 0; //読み込み値

  //読み込み
  Wire.requestFrom( (0x80)>>1 , 1);

  while (Wire.available())  {   // 要求より短いデータが来る可能性あり
    b = Wire.read();            // 1バイトを受信
  }//while

  //データの表示
  String thisString1 = String(b);
  pc.printNS( (char *)thisString1.c_str()  );
  pc.printNS("\r\n");
  delay(1000); // 1秒の待ち

}//loop



SER_9600_C011_1.h

SER_9600_C011_1.h



#ifndef TEST_H
#define TEST_H

//SER_9600_C011_1.h


#define TX1      PB7      // ?pin

//#define UART_DELAY  96   //  9600bps  NG
//#define UART_DELAY  97   //  9600bps  NG
//#define UART_DELAY  98   //  9600bps  NG
//#define UART_DELAY  99   //  9600bps  NG
//#define UART_DELAY 100   //  9600bps  OK 1 
//#define UART_DELAY 101   //  9600bps  OK 2 
//#define UART_DELAY 102   //  9600bps  OK 3 
//#define UART_DELAY 103   //  9600bps  OK 4 
//#define UART_DELAY 104   //  9600bps  OK 5 
#define UART_DELAY 105   //  9600bps  OK 6 
//#define UART_DELAY 106   //  9600bps  OK 7 
//#define UART_DELAY 107   //  9600bps  OK 8 
//#define UART_DELAY 108   //  9600bps  OK 9 
//#define UART_DELAY 109   //  9600bps  OK 10 
//#define UART_DELAY 110   //  9600bps  NG
//#define UART_DELAY 111   //  9600bps  NG


//#define UART_DELAY 1000   //  9600bps ok E


#define DW   digitalWrite


//クラスの定義
struct _pc
{
  void beginNS(int sp);      //メソッドの宣言
  int  putcNS(char ch);      //メソッドの宣言
  int  printNS(char *str1);  //メソッドの宣言
};


//ポートをhiにする 初期化
//メソッドの定義
void _pc::beginNS(int sp)
{
  //ポートをhiにする初期化
  pinMode(TX1, OUTPUT);
  DW(TX1, HIGH);
}


//仮想シリアルへの一文字出力 9600bps
//メソッドの定義
int _pc::putcNS(char ch)
{
  DW(TX1, HIGH);

  DW(TX1, LOW);//START
  delayMicroseconds(UART_DELAY); //START BIT WAIT

  for (int ii = 0; ii < 8; ii++) {
    DW(TX1, (ch >> ii) & 1  );
    delayMicroseconds(UART_DELAY); //DATA 1-8BIT WAIT
  }//for

  DW(TX1, HIGH);//Stop
  delayMicroseconds(UART_DELAY); //StOP BIT WAIT

  return (0);
}


//文字列の表示
//メソッドの定義
int _pc::printNS(char *str1)
{
  //文字の中身がゼロか
  while (*str1) {

    //一文字出力
    putcNS(*str1 ++);

  } //while

  //戻り値
  return (0);
}


//実体の作成
_pc pc;


#endif



1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?