STM32L010とMCP9701で温度をシリアルに出力 (ソフトウェアシリアル)(STM32)(Keil)


x Mbed2
x あまり正確では、ない

目的
秋月で売っている安価なMCP9701(約25円)を使って温度を出力する。

構成
MCP9701-E/TO I-03199

説明
MCP9701は、
0℃の時、400mV
1℃あたり19.5mV
精度は、±4℃
電線が引き出しやすい位置のPA4をアナログ入力にする
計算には、容量削減の為に浮動小数点となるべく割り算は、使わない
MCP9700は、ファミリー、オフセット500mV、10mV/1℃ 今回は、使わない

19.5は、1℃あたりの電圧
3300は、ADCの電圧
400は、0℃

3300/19.5=169.2
400/19.5=20.5

(4096*1692)/4096=1692



//温度

#include "mbed.h"

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

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

//RawSerial pc(PA_2, PA_3); //010


#define UART_DELAY 101 //  1/9600 98-105

DigitalOut TX(PA_2);

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

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

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

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

    return(0);

} //pc_putc

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

    //文字の中身がゼロか
    while(*str1){

        //一文字出力
        pc_putc(*str1 ++);

    } //while

    //戻り値
    return(0);

}//ns_printf


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

    int s;  //ADCの値
    int n0; //温度 小数点以上
    char data_read[8]; //バッファー

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

        //adcの読み込み 0から4096
        s = (adc_vbat.read_u16()>>4);
//s=2048;
        //電圧を温度に変換 ex 20.0 -> 200 温度の十倍を出力
        s=((s*1692)>>12)-205;

        //小数点以上と小数点以下を分ける
        n0=DVI10(s);    // 小数点以上
        s =(s-(n0*10)); // 小数点以下

        //温度の表示
        data_read[0] = '0' + DVI10(n0);
        data_read[1] = '0' + (  n0-(DVI10(n0)*10)  );
        data_read[2] = '.';
        data_read[3] = '0' + s;
        data_read[4] = '\r';
        data_read[5] = '\n';
        data_read[6] = 0;
        //pc.printf(data_read); //010
        pc_printf(data_read); //010
        //1秒の待ち
        wait_ms(1000);

    } //while

} //main

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