LoginSignup
0
0

More than 1 year has passed since last update.

STM32G031とMbedでI2Cスレーブの受信文字をシリアル出力

Last updated at Posted at 2022-08-21

x 超超重要 なぜか秋月STM32G031J6M6がうり切れていた。注意!!(2022/8/27現在)

x 過去ログを見よ!!

x Mbedでのボード設定は、NUCLEO-G031K8
x ここで言うSTM32G031は、秋月で売っているSTM32G031J6M6の事

目的
I2Cスレーブのテスト

o_con698.jpg

o_con699.jpg

参考





//i2c_usart_0x80_031_1

#include "mbed.h"

//1 LED
//2 VDD
//3 GND
//4 Serial 9600bps (PA_2)
//
//5 SWCLK
//6 SWDIO
//7 SDA
//8 SCL

//GPIOの初期化
DigitalOut myled(PB_7);//LED1

//シリアルの定義
UnbufferedSerial serial_port(PA_2, PA_3); //031

//I2Cの初期化
//               sda   scl
I2CSlave slave(PA_12, PA_11); //0031

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

    //シリアルの初期化
    serial_port.baud(9600);
    serial_port.format(
        /* bits */ 8,
        /* parity */ SerialBase::None,
        /* stop bit */ 1
    );

    char buf; //I2Cバッファー

    //I2Cスレーブのアドレスの設定
    slave.address(0x80);

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

        //I2Cの状態の読み出し(ポーリング)
        while (slave.receive() != I2CSlave::WriteAddressed) {}

        myled = 1;//LEDの点灯 debug

        //I2Cスレーブの受信バッファーを読み取る
        slave.read(&buf, 1);

        //I2Cスレーブの受信データの出力
        serial_port.write(&buf, 1);

        myled = 0;//LEDの消灯 debug

    }//while

}//main






main.cpp





//I2C_TEST_STRING_R1_031_1

#include "mbed.h"

#include "OUT80.h"

DigitalOut myled(PB_7);

//        sda   scl
I2C i2c(PA_12, PA_11); //031


//メインプログラム
int main()
{

    OUT80 out80(&i2c);

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

        myled = 1;//LEDをオン

        //I2Cに文字列を出力
        out80.ns_puts( (char *)"hello world\r\n");

        //I2Cに文字列を出力
        out80.ns_printf( (char *)"%d",1234);

        //I2Cに文字を出力
        out80.ns_putc('\r');
        out80.ns_putc('\n');

        myled = 0;//LEDをオフ

        wait_us(1000*1000);//1秒待つ

    }//while

} // main






OUT80.h




//OUT80.h

#include "mbed.h"

struct OUT80 {

    OUT80( I2C *i2c );

    void initialize();

    void ns_putc(char ch);

    void ns_puts(char *str1);

    void ns_printf(char* fmt, ...);

    I2C *_i2c;

};



OUT80.cpp




//OUT80.cpp

#include "mbed.h"

#include "OUT80.h"

//最初に呼ばれるプログラム
OUT80::OUT80( I2C *i2c )
{
    _i2c = i2c;

    initialize();
}//OUT80


//イニシャライズ
void OUT80::initialize()
{
    //_i2c->write(0x80, "START", 3);
}//initialize


void OUT80::ns_putc(char ch)
{
    _i2c->write(0x80, &ch, 1);
    wait_us(1000);//約1ms秒待つ
}//ns_putc


//文字列の表示 nana_seg
void OUT80::ns_puts(char *str1)
{
    //文字の中身がゼロか
    while(*str1) {
        ns_putc( *str1 ++);
    } //while
}//ns_puts


void OUT80::ns_printf(char* fmt, ...)
{

    char buff[256];
    va_list args;
    va_start(args, fmt);
    vsnprintf(buff, sizeof(buff), fmt, args);
    va_end(args);

    char *s = buff;
    while (*s) {
        ns_putc(*s++);
    }//while

}//ns_printf



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