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とMCP3425で電圧を測る(今、ふう20231128)(MCP3425A0T-E/CH 16bitADC)

Last updated at Posted at 2023-11-28

STM32G031とMCP3425で電圧を測る(今、ふう20231128)(MCP3425A0T-E/CH 16bitADC)

参考

x 4PINをOBでGPIOにする。わかる人むけ

目的
シンプルにリッチに作る
なぜかStringが容量不足で動かない


Serial.println(String(Volts,5));


o_cop814.jpg

o_cop815.jpg

プログラム




//mcp3425_to_volts_031_1


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


//定義
//MCP3425A0T-E/CHのアドレス
#define ADD 0x68

float Volts;
float Vref = 2.048 ;

//STM32G031J6M6 i2cピンの定義
#define sdaPin PA12    // ArduinoA4
#define sclPin PA11    // ArduinoA5


//初期化
void setup() {

  delay(3000); //do not delete

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

  //i2cの初期化
  Wire.setSDA(sdaPin);
  Wire.setSCL(sclPin);
  Wire.begin(); //pa12 pa11

  // 7 *(1)待ち
  // 6 *(0)
  // 5 *(0)
  // 4 (0)ワンショット,*(1)連続
  // 3 | (00)12ビット,(01)14ビット
  // 2 | *(10)16ビット
  // 1 I *(00)x1,(01)x2
  // 0 I (10)x4,(11)x8

  //初期値の書き込み
  Wire.beginTransmission(ADD);
  Wire.write(0b10011000); //16bit 15sps PGA x1
  Wire.endTransmission();
}


//i2cの読み出し 出力は、16BIT
int read_data() {

  //2文字の読み込み
  Wire.requestFrom(ADD, 2);

  //戻し
  return ( (Wire.read() << 8 ) + Wire.read() );
}


//メインルーチン
void loop() {
  Volts = read_data() * Vref / 32767.0 ;

  //変換、表示
  int d1 = (int)Volts;
  Serial.print(d1);
  Serial.print(".");
  int d01 = (int)((Volts - ((float)d1)) * 10000.0);
  if (d01 <=  999 & d01 >= 100) {Serial.print("0");}
  if (d01 <=   99 & d01 >=  10) {Serial.print("00");}
  if (d01 <=    9 & d01 >=   0) {Serial.print("000");}
  Serial.println(d01);
  //Serial.println(String(Volts,5));
  delay(1000); //1秒待つ
}




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?