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.

STM32L010とMbedでI2Cスレーブで遊ぶ(受信)(STM32L010F4P6)

Last updated at Posted at 2021-12-08

x Mbed2リビジョン125

目的
I2Cスレーブのテスト

参考

o_con136.jpg



# include "mbed.h"

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

//シリアルの初期化
RawSerial pc(PA_2, PA_3); //010

//I2Cの初期化
I2CSlave slave(PA_10, PA_9); //010

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

    char buf[10]; //I2Cバッファー

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

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

        //I2Cの状態の読み出し(ポーリング)
        int i = slave.receive();

        //ステータスの表示
        pc.printf("I2C Status: %x\r\n", i);

        if (i == I2CSlave::WriteAddressed) {

            buf[0] = 0;//バッファーのクリア

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

            //I2Cスレーブの受信データの表示
            pc.printf("\tRead A: %d\r\n", buf[0]);

        }//if

        //LEDの点滅
        myled = 1;
        wait_ms(200); //0.2秒待つ
        myled = 0;
        wait_ms(200); //0.2秒待つ

    }//while

}//main

//容量削減
void error(const char* format, ...){}




I2Cマスター STM32L010F4P6



//I2C_OUT_TEST1

# include "mbed.h"

//I2Cアドレス
const int ADDR = 0x80; //  address

//I2Cの構造体
I2C_HandleTypeDef hi2c1;

//I2Cの初期化
void I2C_INIT1()
{
    __HAL_RCC_GPIOA_CLK_ENABLE();

    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    __HAL_RCC_I2C1_CLK_ENABLE();

    //I2C_HandleTypeDef hi2c1;

    hi2c1.Instance = I2C1;
    hi2c1.Init.Timing = 0x00000708;
    hi2c1.Init.OwnAddress1 = 0;
    hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c1.Init.OwnAddress2 = 0;
    hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
    HAL_I2C_Init(&hi2c1);

    HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE);

    HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0);

}//I2C_INIT1

//GPIOの初期化
void GPIO_INIT1()
{
    
    __HAL_RCC_GPIOA_CLK_ENABLE();

    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = GPIO_PIN_4;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}//GPIO_INIT1

//メイン関数
int main()
{
    wait_ms(2); //HAL_Delay()になぜか失敗するので呼び出す

    //I2Cの初期化
    I2C_INIT1();

    //GPIOの初期化
    GPIO_INIT1();

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

        //I2Cに1を送信
        HAL_I2C_Master_Transmit(&hi2c1,ADDR, (uint8_t *)"\001", 1, 3000); //data 1
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
        HAL_Delay(1000);//1秒待つ

        //I2Cに1を送信
        HAL_I2C_Master_Transmit(&hi2c1, ADDR, (uint8_t *)"\002", 1, 3000); //data 2
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
        HAL_Delay(1000);//1秒待つ

    }//while

}//main





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?