LoginSignup
0
1

MCP9701温度センサーで遊ぶ(STM32C011J4M7)(PA12ソフトウェアシリアル)(マクロ)

Last updated at Posted at 2024-01-28

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

MCP9701温度センサーで遊ぶ(STM32C011J4M7)(PA12ソフトウェアシリアル)(マクロ)

目的
温度計の値をシリアルに出力

o_cop904.jpg


//SER_LB_9600_MCP9701_C011_1

//ヘッダー
#include <Arduino.h>
#include "SER_9600_C011_1.h"

//定義
//100の割り算 0から30000までは、だいたい正しい   
#define DIV100(in) ((in*(65536/100*64+24))>>22)
//25の割り算 0から30000までは、だいたい正しい   
#define DIV25(in) ((in*(65536/25*16+8))>>20)
//MCP9701の温度を求める x100
#define TMP(so) ((((so)*270769)-134432821)>>16)


//初期化
void setup()
{

  //シリアルの初期化
  pc.beginNS(9600);

} //setup


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

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


  //センサーの値を温度に変換
  int t = TMP(s);
  int d1 = DIV100(t);
  int d01 = (t - (d1 * 100));d01 = (DIV25(d01))*25;


  //温度の表示
  String thisString1 = String(d1);
  pc.printNS( (char *)thisString1.c_str()  );

  pc.printNS(".");

  thisString1 = String(d01);
  if (d01==0) {pc.printNS("0");}
  pc.printNS( (char *)thisString1.c_str()  );
  pc.printNS("\r\n");

  
  delay(1000);

} //loop

SER_9600_C011_1.h




#ifndef TEST_H
#define TEST_H

//SER_9600_C011_1.h


#define TX1      PA12      // ?pin

//#define UART_DELAY  96   //  9600bps  NG
//#define UART_DELAY  97   //  9600bps  NG
//#define UART_DELAY  98   //  9600bps  NG
//#define UART_DELAY  99   //  9600bps  NG
//#define UART_DELAY 100   //  9600bps  OK 1 
//#define UART_DELAY 101   //  9600bps  OK 2 
//#define UART_DELAY 102   //  9600bps  OK 3 
//#define UART_DELAY 103   //  9600bps  OK 4 
//#define UART_DELAY 104   //  9600bps  OK 5 
#define UART_DELAY 105   //  9600bps  OK 6 
//#define UART_DELAY 106   //  9600bps  OK 7 
//#define UART_DELAY 107   //  9600bps  OK 8 
//#define UART_DELAY 108   //  9600bps  OK 9 
//#define UART_DELAY 109   //  9600bps  OK 10 
//#define UART_DELAY 110   //  9600bps  NG
//#define UART_DELAY 111   //  9600bps  NG


//#define UART_DELAY 1000   //  9600bps ok E


#define DW   digitalWrite


//クラスの定義
struct _pc
{
  void beginNS(int sp);      //メソッドの宣言
  int  putcNS(char ch);      //メソッドの宣言
  int  printNS(char *str1);  //メソッドの宣言
};


//ポートをhiにする 初期化
//メソッドの定義
void _pc::beginNS(int sp)
{
  //ポートをhiにする初期化
  pinMode(TX1, OUTPUT);
  DW(TX1, HIGH);
}


//仮想シリアルへの一文字出力 9600bps
//メソッドの定義
int _pc::putcNS(char ch)
{
  DW(TX1, HIGH);

  DW(TX1, LOW);//START
  delayMicroseconds(UART_DELAY); //START BIT WAIT

  for (int ii = 0; ii < 8; ii++) {
    DW(TX1, (ch >> ii) & 1  );
    delayMicroseconds(UART_DELAY); //DATA 1-8BIT WAIT
  }//for

  DW(TX1, HIGH);//Stop
  delayMicroseconds(UART_DELAY); //StOP BIT WAIT

  return (0);
}


//文字列の表示
//メソッドの定義
int _pc::printNS(char *str1)
{
  //文字の中身がゼロか
  while (*str1) {

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

  } //while

  //戻り値
  return (0);
}


//実体の作成
_pc pc;


#endif






0
1
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
1