LoginSignup
0
0

More than 1 year has passed since last update.

STM32G031と温度センサーS-8120Cで温度をPA_2シリアル出力(浮動小数点)

Last updated at Posted at 2022-08-18

x 超超重要 なぜか秋月STM32G031J6M6がうり切れていた。注意!!(2022/8/27現在)

x 過去ログを見よ
x PA_2をOBでGPIOに出来る人むけ
x STM32G031は、秋月で売っているSTM32G031J6M6の事

目的
安価に温度を測る。

v = -8.2 * t + 1705

式の変形

t = (v - 1705.0) / -8.2;

o_con569.jpg

o_con690.jpg

o_con691.jpg

参考





//SER_S-8120C_float_031_1


#include "mbed.h"

//シリアルの定義
UnbufferedSerial serial_port(PA_2, PA_3); //031

//アナログ入力の設定
//AnalogIn adc_vbat(D0); //PB_7 ADC1/11
AnalogIn adc_vbat(PB_7); //PB_7 ADC1/11

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

    //シリアルの初期化
    serial_port.baud(9600);
    serial_port.format(
        /* bits */ 8,
        /* parity */ SerialBase::None,
        /* stop bit */ 1
    );

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

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

        //ADCの読み込み 0から4096
        //int s = (adc_vbat.read_u16()>>4);

        //センサーのADC値を電圧に変換
        //float v = s * ( 3.3 / 4096.0 );
        //v = 1541;
 
        //センサーのADC値を電圧に変換
        float v = adc_vbat.read() * 3300;  
        

        //センサーの電圧を温度に変換
        // v = -8.2 * t + 1705  =>  t = (v - 1705.0) / -8.2;
        float t = (v - 1705.0) / -8.2;

        //整数と小数に分離
        int t_i = (int)t;
        int t_s = (int)(t*1000);
        t_s = t_s - (t_i * 1000);

        //数値の表示
        sprintf( data_read, "%3d", t_i );
        serial_port.write(data_read, 3);

        //ピリオドの表示
        serial_port.write(".", 1);

        //数値の表示
        sprintf( data_read, "%03d", t_s );
        serial_port.write(data_read, 3);

        //リターンの表示
        serial_port.write("\r\n", 2);

        //1秒待つ
        wait_us(1000*1000);

    } //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