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で電圧を測る3(浮動小数点を使わない)(ハードウェアシリアル出力)

Last updated at Posted at 2023-11-28

STM32G031とMCP3425で電圧を測る3(浮動小数点を使わない)(ハードウェア_シリアル出力)

x 4PINをOBでGPIO

目的
カラー液晶の開発用
ハードウェア_シリアル出力

o_cop816.jpg

o_cop817.jpg




//mcp3425_to_volts_031_1


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


//定義
//MCP3425A0T-E/CHのアドレス
#define ADD 0x68
//10の割り算 0から1028までは、正しい。主に0から999
#define DIV10(n) ((n*205)>>11)
//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();
}//setup


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

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

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


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

  //変換、表示
  //16ビットadcの4ビットの変換テーブル
  char bit4[16][4] =
  { "000" , "062" , "125" , "187" ,
    "250" , "312" , "375" , "437" ,
    "500" , "562" , "625" , "687" ,
    "750" , "812" , "875" , "937"
  };

  int s = read_data();
  //s = ((1654*16)+8); //debug

  int e = (s >> 4);

  int g = '0';
  if      ( e >= 2000 ) {e = e - 2000;g = '2';}
  else if ( e >= 1000 ) {e = e - 1000;g = '1';}

  char str1[9];
  str1[0] = g;
  str1[1] = '.';

  str1[4] = '0' + (e - (DIV10(e) * 10)); e = DIV10(e);
  str1[3] = '0' + (e - (DIV10(e) * 10));
  str1[2] = '0' + DIV10(e);

  str1[5] = bit4[s & 0xf][0];
  str1[6] = bit4[s & 0xf][1];
  str1[7] = bit4[s & 0xf][2];

  str1[8] = 0;

  //表示
  Serial.print(str1);
  Serial.println();

  delay(1000); //1秒待つ
}//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?