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]STM32C011-DK、I2Cスレーブの押しボタンの状態を読んで、I2CスレーブのLEDに状態を表示。UNO3は、ホスト。

Last updated at Posted at 2025-08-09

結果

  • 押す

image_original(61).jpg

  • 離す

image_original(62).jpg

プログラム

UNO3 (ホスト)




//i2c_button_to_LED_UNO3_1


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


//初期化
void setup() {

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

}  // setup


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

  int a;  //一時変数


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


  // LEDへ書き込み
  Wire.beginTransmission(0x80 >> 1);  // アドレス
  Wire.write(a);                      // I2Cスレーブに送信
  Wire.endTransmission();             // END


  // 速すぎるからウェート
  delay(300);  // 0.3秒待つ

}  // loop



STM32C0116-DK (スレーブ)




//I2C_slave_IN_OUT_test1_c0116_1


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


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

  //GPIOの初期化
  pinMode(PA8, INPUT);   // button
  pinMode(PB6, OUTPUT);  // sets the LED pin as output


  //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) {

  int x = Wire.read();            // I2C受信データの読み込み
  digitalWrite(PB6, (x & 0x01));  // 1bit出力

}  //receiveEvent


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

  int a = digitalRead(PA8);  // ボタンの信号の読み込み
  Wire.write(a);             //値の送信

}  //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?