STM32C011と温度センサーMCP9701で温度をシリアル出力(テーブル方式)(STM32C011J4M7)


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

x 過去ログを見よ!!

目的
安価に温度を測る。

センター　400mV
ステップ　19.5mV 1℃

1 未使用-未接続
2 VDD
3 GND
4 未使用-未接続

8 SWD SWCLK
7 SWD SWDIO
6 ADC MCP9701
5 Serial 9600bps

結果

![o_cop930.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/e95a55a0-7e53-d512-6974-66bf6853d399.jpeg)

![o_cop931.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/b56e78f4-3fcd-7e5c-26b5-5d5c0e4c62b3.jpeg)





プログラム

```c++



//SER_MCP9701_TABLE_C011_1


//インクルド
#include <Arduino.h>


//定義
static const char tmp_t[]={
0*2+0,0*2+1,0*2+1,
1*2+0,1*2+1,1*2+1,
2*2+0,2*2+1,2*2+1,
3*2+0,3*2+1,3*2+1,
4*2+0,4*2+1,4*2+1,
5*2+0,5*2+1,5*2+1,
6*2+0,6*2+1,6*2+1,
7*2+0,7*2+1,7*2+1,
8*2+0,8*2+1,8*2+1,
9*2+0,9*2+1,9*2+1,
10*2+0,10*2+1,10*2+1,
11*2+0,11*2+1,11*2+1,
12*2+0,12*2+1,12*2+1,
13*2+0,13*2+1,13*2+1,
14*2+0,14*2+1,14*2+1,
15*2+0,15*2+1,15*2+1,
16*2+0,16*2+1,16*2+1,
17*2+0,17*2+0,17*2+1,
18*2+0,18*2+0,18*2+1,
19*2+0,19*2+0,19*2+1,
20*2+0,20*2+0,20*2+1,
21*2+0,21*2+0,21*2+1,
22*2+0,22*2+0,22*2+1,
23*2+0,23*2+0,23*2+1,
24*2+0,24*2+0,24*2+1,
25*2+0,25*2+0,25*2+1,
26*2+0,26*2+0,26*2+1,
27*2+0,27*2+0,27*2+1,
28*2+0,28*2+0,28*2+1,
29*2+0,29*2+0,29*2+1,
30*2+0,30*2+0,30*2+1,
31*2+0,31*2+0,31*2+1,
32*2+0,32*2+0,32*2+1,
33*2+0,33*2+0,33*2+1,
34*2+0,34*2+0,34*2+1,
35*2+0,35*2+0,35*2+1,
36*2+0,36*2+0,36*2+1,36*2+1,
37*2+0,37*2+1,37*2+1,
38*2+0,38*2+1,38*2+1,
39*2+0,39*2+1,39*2+1
};

static const char data[][5]={
"00.0","00.5","01.0","01.5","02.0","02.5","03.0","03.5","04.0","04.5","05.0","05.5","06.0","06.5","07.0","07.5","08.0","08.5","09.0","09.5",
"10.0","10.5","11.0","11.5","12.0","12.5","13.0","13.5","14.0","14.5","15.0","15.5","16.0","16.5","17.0","17.5","18.0","18.5","19.0","19.5",
"20.0","20.5","21.0","21.5","22.0","22.5","23.0","23.5","24.0","24.5","25.0","25.5","26.0","26.5","27.0","27.5","28.0","28.5","29.0","29.5",
"30.0","30.5","31.0","31.5","32.0","32.5","33.0","33.5","34.0","34.5","35.0","35.5","36.0","66.5","37.0","37.5","38.0","38.5","39.0","39.5"
};


//初期化処理
void setup()
{

  delay(3000); //not delete (消しちゃダメだよ!!)

  //シリアルの初期化
  Serial.setTx(PA8);
  Serial.setHalfDuplex();
  Serial.begin(9600);

} //setup

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

  //センサーの値を読み込む
  analogReadResolution(12); // ADC 12bit mode
  int s = analogRead(PA12);   // PB7  PIN1 031 センサーの読み取り値

  //温度の表示
  s = (s >> 3);
  if (s >= 184) {
    Serial.print("40.0");
  } else if (s <= 62) {
    Serial.print("-0.0");
  } else {
    Serial.print(data[tmp_t[s - 63]]);
  }
  Serial.println();

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

} //loop


```



--------------------------------------

いろいろ

大江公夫さんがうまくいったみたい!!

https://twitter.com/pokibon/status/1754822983280169287

--------------------------------------



おまけ








プログラム

```c++



//SER_Temperature_MCP9701_C011_1


//インクルド
#include <Arduino.h>
#include "Temperature_MCP9701.h"


//初期化処理
void setup()
{

  delay(3000); //not delete (消しちゃダメだよ!!)

  //シリアルの初期化
  Serial.setTx(PA8);
  Serial.setHalfDuplex();
  Serial.begin(9600);

} //setup


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

  //センサーの値を読み込む
  analogReadResolution(12); // ADC 12bit mode
  int Sensor = analogRead(PA12); // センサーの読み取り値

  //温度の表示
  float Voltage = Sensor * ( 3.3 / 4096.0 );
  float Temperature = Temperature_MCP9701(Voltage);
  Serial.println(Temperature);

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

} //loop



```

Temperature_MCP9701.h


```c++


// Temperature_MCP9701.h 
//
// ex 3.3V analogReadResolution(12);
//         float Sensor = analogRead(A2);
//         float Voltage = Sensor * ( 3.3 / 4096.0 );
//         float Temperature = Temperature_MCP9701(Voltage);
//
float Temperature_MCP9701(float Voltage){

  return((Voltage - 0.4) * (1.0 / 0.0195));

}//Temperature_MCP9701



```