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?

More than 3 years have passed since last update.

STM32L010とI2CでIOエキスパンダー化(IN)(Mbed)

Last updated at Posted at 2021-12-30

x Mbed2リビジョン130

x 過去ログを見よ

x 結構な力作

目的
I2CでIOを拡張する。
金銭的には、ほぼ意味は、無い。
たまたまSTM32L010F4P6がある場合、買いに行く手間が省ける。

o_con183.jpg

6593C1FE-27A8-4A1D-B3F2-E1819EBFB4B0.jpeg

スレーブのプログラム

mbed2リビジョン130




//I2C_EXPANDER_IN_1

#include "mbed.h"

DigitalOut TX(PB_1);
//DigitalOut TX(PC_14);

//仮想シリアルへの一文字出力 1200bps
int pc_putc(char ch)
{

    TX=1;
    TX=0;//Start
    wait_us(832);

    for(int ii=0; ii<8; ii++) { //Data x 8
        TX=(ch>>ii)&1;
        wait_us(832);
    }; //for

    TX=1;//Stop
    wait_us(832);

    return(0);

} //pc_putc


PortIn     p(PortA, 0x000000FF);   // PA0-PA7

//char HEX_C[] =
//{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' } ;

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

//メインルーチン
int main()
{

    TX=1; //ポートの初期化

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

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

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

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

        buf[0] = p.read();

        //I2Cマスターに送信する
        slave.write(buf,1);

        //I2Cスレーブの送信データの表示 HEX
        //int cc = buf[0];
        //pc_putc(' ');
        //pc_putc('D');
        //pc_putc(HEX_C[ cc >> 4    ]);
        //pc_putc(HEX_C[ cc &  0x0f ]);
        //pc_putc('\r');
        //pc_putc('\n');

//        //0.5秒の待ち
//        wait_ms(500);

    }//while

}//main


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






マスタープログラム

mbed2リビジョン125


//I2C_M_Read_1

#include "mbed.h"

//I2C i2c(I2C_SDA, I2C_SCL); //767
//I2C i2c(dp5, dp27); //1114
I2C i2c(PA_10, PA_9); //010


//Serial pc(USBTX, USBRX); // tx, rx
//Serial pc(SERIAL_TX, SERIAL_RX); //767
RawSerial pc(PA_2, PA_3); //010

//#define S5851A 0x48
#define ADDR        (0x80)   //  address

int main() {

    char data_read[8]; //バッファー





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



        //dataの読み込み
        // Read temperature register
        i2c.read(ADDR, data_read,1,false);

        //温度の保存
        // Calculate temperature value in Celcius
        int tempval =  data_read[0]; //dataの転記

        //tempval = 0;

        //画面に表示
        pc.printf("Read : %x\r\n",tempval);

        //1秒待つ
        wait_ms(1000);

    }//while

}//main

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



ナイトライダー



//IO_Knight_Rider_1

#include "mbed.h"

//GPIOの初期化
DigitalOut myled(PB_1);//D7

//GPIOポートの初期化
//                       76543210
PortOut ledport(PortA, 0b11111110); //D0 - D6

//シフトの軽量高速化の為
//              12345678   12345678   12345678   12345678
char b8[8] = {0b10000000,0b01000000,0b00100000,0b00010000,
              0b00001000,0b00000100,0b00000010,0b00000001
             };

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

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

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

        //0から7までループ
        for(int ii=0; ii<8; ii++) {

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

            ledport = buf[0]<<1; //1シフト後に代入

            myled =( ((buf[0] & 0x80) == 0)  ? 0 : 1 ); //D7を代入

            wait_ms(1000); //1秒待つ

        }//for

    }//while

}//main

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




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?