0
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 1 year has passed since last update.

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

Last updated at Posted at 2022-03-22

x MCP9701-E/TO 販売コード 103199

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

o_con276.jpg



//S_SER_9701_V2_031_1
#include <Arduino.h>

#define in7      PB7  // 1pin

#define DW   digitalWrite

#define UART_DELAY 102   //  9600bps ok 031

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


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

  DW(in7, HIGH);

  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);

  return (0);

}//pc_putc


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

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

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

  } //while

  //戻り値
  return (0);

}//pc_printf


//初期化
void setup()
{

  //ポートをhiにする 初期化
  pinMode(in7, OUTPUT);
  DW(in7, HIGH);

} //setup


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

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

  analogReadResolution(12); //adc 12bit mode
  s = analogRead(A3); // PA11 PIN5 031

  //電圧を温度に変換 ex 20.0 -> 200 温度の十倍を出力
  s = ((s * 1692) >> 12) - 205;

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

  //温度の表示
  data_read[0] = '0' + DIV10(n0);
  data_read[1] = '0' + (  n0 - (DIV10(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
  //1秒の待ち
  delay(1000);

} //loop



PA0がソフトウェアシリアル、PA13がADC入力のバージョン
x シリアル書き込みでしか消せないので注意





//S_SER_9701_V2_PA0_PA13_031_1
#include <Arduino.h>

#define in7      PA0  // 1pin

#define DW   digitalWrite

#define UART_DELAY 102   //  9600bps ok 031

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


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

  DW(in7, HIGH);

  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);

  return (0);

}//pc_putc


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

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

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

  } //while

  //戻り値
  return (0);

}//pc_printf


//初期化
void setup()
{

  //ポートをhiにする 初期化
  pinMode(in7, OUTPUT);
  DW(in7, HIGH);

} //setup


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

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

  analogReadResolution(12); //adc 12bit mode
  //s = analogRead(A3); // PA11 PIN5 031
  s = analogRead(A5); // PA13 PIN7 031

  //電圧を温度に変換 ex 20.0 -> 200 温度の十倍を出力
  s = ((s * 1692) >> 12) - 205;

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

  //温度の表示
  data_read[0] = '0' + DIV10(n0);
  data_read[1] = '0' + (  n0 - (DIV10(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
  //1秒の待ち
  delay(1000);

} //loop



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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?