0
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 3 years have passed since last update.

ArduinoUNOとI2Cを使い0x40(8ビット表記0x80)に「hello...」と出力する。(Arduino)

Last updated at Posted at 2022-03-17

目的
I2Cのテスト
I2C->USART9600bps->USBのテスト

o_con267.jpg

STM32G031とVSCode+STM32CubeでI2Cスレーブの受信文字をシリアル出力(受信)(STM32-I2C-USART)(CH340N)





//I2C_HELLO_UNO_1

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

#define en     13     // 1pin

#define ADDR1  0x40


//文字の表示 nana_seg
int ns_putc(char str1)
{

  //I2Cに送信
  Wire.beginTransmission(ADDR1);
  Wire.write( str1 );
  Wire.endTransmission();

  delay(1);//連続送信防止の為に1ms待つ

  //戻り値
  return (0);

}//ns_putc


//文字列の表示 nana_seg
int ns_printf(char *str1)
{

  //文字の中身がゼロか
  while (*str1) {

    ns_putc( *str1 ++  );

  } //while

  //戻り値
  return (0);

}//ns_printf


//初期化
void setup() {

  //GPIOの初期化
  pinMode(en, OUTPUT);

  //i2cの初期化
  Wire.begin(); //UNO

}//setup


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

  digitalWrite(en, 1); //LED ON debug

  //I2Cに文字列を出力
  ns_printf("hello world\r\n");

  delay(500);//0.5秒待つ


  digitalWrite(en, 0); //LED OFF debug
  delay(500);//0.5秒待つ

}//loop



コメントなしバージョン



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

int ns_putc(char str1){
  Wire.beginTransmission(0x40);
  Wire.write(str1);
  Wire.endTransmission();
  delay(1);
  return (0);
}

int ns_printf(char *str1){
  while (*str1){ns_putc(*str1 ++);}
  return (0);
}

void setup(){
  Wire.begin();
}

void loop(){
  ns_printf("hello world\r\n");
  delay(1000);
}




0
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
0
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?