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?

[I2C]STM32C0116-DKでI2Cスレーブ送受信のテストして遊ぶ。UNO3で1足された値を読む。

Last updated at Posted at 2025-08-09

参考

いろいろ注意

  • 過去ログを見た?
  • STM32C011の事。
  • STM32C011F4P6でも、同じかも?

結果

Screenshot from 2025-08-09 12-23-00.png

イメージ

image_original(60).jpg

プログラム

UNO3ホスト側




//i2c_a_to_b_UNO3_1


//インクルド
#include <Arduino.h>
#include <Wire.h>  // I2C


//初期化
void setup() {

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

}  // setup


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

  //書き込み 'a'
  char a = 'a';
  Wire.beginTransmission(0x80 >> 1);  // アドレス
  Wire.write(a);                      // 'a'をI2Cスレーブに送信
  Wire.endTransmission();             // END
  delay(3);                           // ダミー

  // I2Cスレーブ側で1足した値を返す。

  // 読み込み 'b' エラー時は、'z'
  Wire.requestFrom(0x80 >> 1, 1);
  char b = 'z';               // 読み込み値 エラー時は、z
  while (Wire.available()) {  // 要求より短いデータが来る可能性あり
    b = Wire.read();          // 1バイトを受信
  }  // while

  //値を表示
  Serial.print('[');
  Serial.print(a);
  Serial.print(']');
  Serial.print('-');
  Serial.print('>');
  Serial.print('[');
  Serial.print(b);
  Serial.print(']');
  Serial.println();
  delay(1000);  // 1秒待つ

}  // loop



STM32C0116-DK I2Cスレーブ側




//I2C_slave_IN_OUT_add1_c0116_1


//インクルド
#include <Arduino.h>
#include <Wire.h>  //I2C


//定義 volatileは、最適化するな
static volatile char data = 'z';


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

  //I2Cの初期化
  Wire.begin(0x80 >> 1);         // Slave ID #80
  Wire.onReceive(receiveEvent);  // データが来ると呼ばれる関数(ホストからライト)
  Wire.onRequest(requestEvent);  // 読み込み命令が来ると呼ばれる関数(ホストからリード)

}  //setup


//メインループ
void loop() {
  delay(3);  //ダミー
}  //loop


//I2Cの読み取り関数(ホストからライト)
void receiveEvent(int howMany) {

  data = Wire.read();  // I2C受信データの読み込み

}  //receiveEvent


//I2Cの読み取り関数(ホストからリード)
void requestEvent() {

  Wire.write(data + 1);  // 値の送信

}  //requestEvent



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?