LoginSignup
0
0

STM32L010とMCP3425でなんちゃってluxを液晶表示(MCP3425A0T-E/CH 16bitADC)(明るさ)

Last updated at Posted at 2021-04-17

STM32L010用に軽量コンパクト

xリビジョンは、125
xコードメンテナンス 主にバイナリサイズの縮小 2021/4/20

  1. 10の割り算のシフト演算化 -44バイト
//10の割り算 0から1028までは、正しい。主に0から999
#define DVI10(n) ((n*205)>>11)

2.errorの無効化 -2452バイト

void error(const char* format, ...){}

構成
STM32L010F4P6
MCP3425A0T-E/CH
NJL7502L
AQM0802A

1.SCLとSDAを接続、プルアップも忘れずに
2.電源の接続
3.下記のソースコードを書き込む
4.コンパイル実行で表示されたら終了
5.おわり


#include "mbed.h"

//10の割り算 0から1028までは、正しい。主に0から999
#define DVI10(n) ((n*205)>>11)

// AnalogIn adc_vbat(A0);

#define ADDR        (0xD0)   //  address
#define ADDR_LCD    (0x7C)   //  address

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

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

char INIT_com[]={0x0,0x38,
0x0,0x39,
0x0,0x4,
0x0,0x14,
0x0,0x70,
0x0,0x56,
0x0,0x6C,
0x0,0x38,
0x0,0xC,
0x0,0x1,
0x40,0x41};

char INIT_cls[]={0x0,0x1};

char ch_hex_a_b[5];
char *ch_hex_a(int l_num)
{
    int a,b,c;

    b=DVI10(l_num);
    c=l_num-(b*10);
    l_num=b;
    a=DVI10(l_num);
    b=l_num-(a*10);

    ch_hex_a_b[0] = '@';
    ch_hex_a_b[1] = '0' + a;
    ch_hex_a_b[2] = '0' + b;
    ch_hex_a_b[3] = '0' + c;
    ch_hex_a_b[4] = 0;

    return(ch_hex_a_b);
}

int ii;     //ループカウンタ

int main()
{
    //液晶の初期化
    for(ii=0;ii<11;ii++){
        i2c.write(ADDR_LCD, &INIT_com[ii*2], 2);wait_ms(2);
    } //for

    i2c.write(ADDR, "\230", 1); //16bit 15sps PGA x1

    int p,s;
    while (1) {
        //液晶のクリア
        i2c.write(ADDR_LCD,INIT_cls,2);wait_ms(2);

        //ii = (adc_vbat.read_u16()>>4);

        //データの読み込み
        i2c.read(ADDR | 1, data_read, 2);
        
        s = (data_read[0] * 256 ) + data_read[1];
        //s = 32767;
        //printf("-s=%d\r\n",s); //767
        //printf(" h=%d\r\n",data_read[0] * 256 );
        //printf(" l=%d\r\n",data_read[1]);

        // 32768/(163.84/2)=400 luxに変換
        p=( ((s>>5)*6)  + (s>>7)  ) >> 4;
        //printf(" p=%d\r\n",p); //767
        
        i2c.write(ADDR_LCD, ch_hex_a( p ) ,4);

        wait_ms(1000);
    }//while

}//main

void error(const char* format, ...){}


mcp3425_010_lux_1.jpg

バイナリサイズの縮小化前

mcp3425_010_lux_2.jpg

バイナリサイズの縮小化後

mcp3425_010_lux_3.jpg

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