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と内蔵ADCで電流をソフトウェアシリアルに出力 330.0uAまで 10kΩ上側または10kΩ下側

Last updated at Posted at 2021-05-06

目的
主にNJL7502Lの微小な電流を測る。

説明
主な変更点は、電圧表示の計算式を次の計算式にした

adcの値(4096)に6600掛けて8192で割ると3300にピッタリなる。

どして、こうなったは、STM32L010を参照

serial_adc_010_2_3.jpg


//電流

#include "mbed.h"

//10k6Ωの上側 下側フラグ 1は上側 0は下側
#define DO_UP  0

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

//アナログ入力の設定
AnalogIn adc_vbat(A3); //PA_4
//AnalogIn adc_vbat(A0); //767

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

#define UART_DELAY 96 //  1/9600

//仮想シリアルの出力ポート
//DigitalOut TX(PA_9);
DigitalOut TX(PA_2);

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] = '0' + a;
    ch_hex_a_b[1] = '0' + b;
    ch_hex_a_b[2] = '0' + c;
    ch_hex_a_b[3] = 0;

    return(ch_hex_a_b);
} //ch_hex_a




//仮想シリアルへの一文字出力 9600bps
int pc_putc(char ch) {
        
    TX=1;
    TX=0;//START
    wait_us(UART_DELAY);

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

    TX=1;//Stop
    wait_us(UART_DELAY);
             
    return(0);
    
} //pc_putc

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

    int ii = 0; //ループカウンター
    while(str1[ii]!=0){
            
        //一文字出力
        pc_putc(str1[ii]);ii++;
                        
    } //while

    //戻り値
    return(0);
}

int main() {

    //ポートをhiにする 初期化
    TX=1;
    
    //無限ループ
    while(1) {
    
        //adcの読み込み 0から4096
        int s = (adc_vbat.read_u16()>>4);
    
//        pc.printf("\r\n -S=%d",s); //767

        //電圧を電流に変換 ex 330.0uA -> 3300 uAの十倍を出力
        if(1 == DO_UP) { s=4095-s; }
        s=(s*6600)>>13;

//        s = 3300;
//        pc.printf("\t c=%d",s); //767

//小数点以上と小数点以下を分ける iiは10の桁 sは1の桁
int ii = s;
if     ( ii >= 3000 ) { ii = DVI10( (ii - 3000)  ) + 300; }
else if( ii >= 2000 ) { ii = DVI10( (ii - 2000)  ) + 200; }
else if( ii >= 1000 ) { ii = DVI10( (ii - 1000)  ) + 100; }
else                  { ii = DVI10( (ii       )  )      ; }
s = s - (ii*10);

//        pc.printf("\t t=%d %d",ii,s); //767
//        pc.printf("\r\n"); //767 リターン
    
        pc_printf(  ch_hex_a(ii)  );
        
        //電流の小数点以下の表示
        ch_hex_a( s*10 );
        ch_hex_a_b[0] = '.';
      //ch_hex_a_b[1] = '0' + s;
        ch_hex_a_b[2] = 'u';      
        pc_printf(  ch_hex_a_b  );
        
        //リターン
        pc_printf(  "\r\n"  );

        //1秒の待ち
        wait_ms(1000);
        
    } //while

} //main

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



serial_adc_010_2_1.jpg

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?