STM32G031と内部ADCと温度センサーMCP9701でソフトウェアシリアルに出力する。


温度を表示

目的
秋月で売っている安価なMCP9701(約25円)を使って温度を
ソフトウェアシリアルに出力する。

構成
MCP9701-E/TO I-03199
STM32G031J6M6




//#define in7 1 //uno
#define in7      PB7  // 1pin

#define DW   digitalWrite

//#define UART_DELAY 832 //  1200bps ok 031
#define UART_DELAY 102   //  9600bps ok 031

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

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

  DW(in7, HIGH);

    //q_st =  micros(); //debug

  DW(in7, LOW);//START
  delayMicroseconds(UART_DELAY);

  for(int ii=0;ii<8;ii++){
    DW(in7, (ch>>ii)&1  );
    delayMicroseconds(UART_DELAY);
  }//for

  DW(in7, HIGH);//Stop
  delayMicroseconds(UART_DELAY);

    //q_et =  micros(); //debug 引いた数が0で1041usなら正解

  return(0);

}//pc_putc

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

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

        //一文字出力
        pc_putc(*str1 ++);
        //Serial.print( *str1 ++ ); //uno

    } //while

    //戻り値
    return(0);
}//pc_printf

//0から999まで文字列に変換
char ch_hex_a_b[5];
char *ch_hex_a(int l_num)
{
  int a,b,c;

  b=l_num/10;
  c=l_num-(b*10); //1の桁
  a=b/10;         //100の桁
  b=b-(a*10);     //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

//初期化
void setup()
{
  //ポートをhiにする 初期化
  pinMode(in7, OUTPUT);
  DW(in7, HIGH);
  delayMicroseconds(UART_DELAY*10);

  //Serial.begin(9600);

} //setup

//メインループ
void loop()
{

  int s; //センサーの値  //101
  int ii; //ループカウンター

  s = analogRead(A3); // PA11 PIN5
  //s = (65536/2)-1;
  //s = 65536/4;
  //s=(4096/2);
  //s=(1024/2);

//d  String thisString2 = String(s);
//d  pc_printf( (char *)thisString2.c_str() );
//d  pc_printf("\r\n");

  //電圧を温度に変換 ex 20.0 -> 200 温度の十倍を出力
  s=((  (s*4)  -496)*27081)>>16;

//d  //pc_printf( "," ); //debug_i
//d  String thisString3 = String(s);
//d  pc_printf( (char *)thisString3.c_str() );
//d  pc_printf("\r\n");

  //小数点以上と小数点以下を分ける
  ii=(s/10);  // 10の桁
  s =(s-(ii*10)); //  1の桁

  //温度の小数点以上の表示
  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] = 'C';      
  pc_printf(  ch_hex_a_b  );

  //リターン
  pc_printf(  "\r\n"  );

  //1秒の待ち
  delay(1000);

} //loop