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

STM32C011F4P6、電圧計を作って遊ぶ。だいたいDC30Vぐらい(でも30V入れる時は、要注意(分圧抵抗と極性))

Last updated at Posted at 2025-08-25

いろいろ注意

  • 過去ログを見よ
  • 自己責任の意味がわかる人
  • いろいろ1:容量が約600バイトオーバーして、対策に小一時かかって、やばかった(シリアルポートのライブラリを外す([ツール]から入る
  • いろいろ2:8bit PICは、74なんちゃらの標準ロジックICの置き換え、だから処理能力も容量も、必要としないわけだ。PICを狙う32bit系は、ポテンシャルの高さから、高性能に引きずられる、わけだ?(なんの事?
  • 標準のI2Cは、(PC14,PB7)
  • PA3に0.33Vの時は、3.3Vと表示される
  • U(S)ART support:"Disabled(no Serial support)"
  • 10分の1にする

o_coq951.jpg

結果

Screenshot from 2025-08-26 08-11-57.png

Screenshot from 2025-08-26 08-03-16.png

Screenshot from 2025-08-26 08-08-04.png

プログラム



//Soft_I2C_AQM0802A_Voltage_C0116


//定義
//A5
#define SCK_0_HIGH() digitalWrite(PB7, HIGH)
#define SCK_0_LOW() digitalWrite(PB7, LOW)

//A4
#define SDA_0_HIGH() digitalWrite(PC14, HIGH)
#define SDA_0_LOW() digitalWrite(PC14, LOW)

// 1000us   1kh
//  100us  10kh
//   10us 100kh
#define myDelay() delayMicroseconds(100);

//i2cバッファー
char data_read[] = { 0x00, 0x00 };

//初期レジスター
char INIT_com[] = {
  0x0, 0x38,  //1
  0x0, 0x39,  //2
  0x0, 0x4,   //3
  0x0, 0x14,  //4
  0x0, 0x70,  //5
  0x0, 0x56,  //6
  0x0, 0x6C,  //7
  0x0, 0x38,  //8
  0x0, 0xC    //9
};

//画面のクリアレジスター
char INIT_cls[] = { 0x0, 0x1 };


//i2c書き込みルーチン
void i2c_lcd_w(char *buff1) {

  int add_p = 0x7C;              //I2Cアドレス
  int data_p = buff1[0] & 0xff;  //文字列から文字を取り出す。[0]
  int data_q = buff1[1] & 0xff;  //文字列から文字を取り出す。[1]

  int ii;  //カウンター

  //待ち
  SDA_0_HIGH();
  SCK_0_HIGH();
  myDelay();
  myDelay();
  myDelay();

  //START
  SDA_0_LOW();
  myDelay();
  SCK_0_LOW();
  myDelay();

  //アドレス
  for (ii = 0; ii < 8; ii++) {
    SCK_0_LOW();
    if (((add_p << ii) & 0x80) == 0) {
      SDA_0_LOW();
    } else {
      SDA_0_HIGH();
    }  //endif
    myDelay();
    SCK_0_HIGH();
    myDelay();
  }  //for

  //ack
  SCK_0_LOW();
  SDA_0_HIGH();
  myDelay();
  SCK_0_HIGH();
  myDelay();

  //データ1
  for (ii = 0; ii < 8; ii++) {
    SCK_0_LOW();

    if (((data_p << ii) & 0x80) == 0) {
      SDA_0_LOW();
    } else {
      SDA_0_HIGH();
    }  //endif

    myDelay();
    SCK_0_HIGH();
    myDelay();
  }  //for

  //ack
  SCK_0_LOW();
  SDA_0_HIGH();
  myDelay();
  SCK_0_HIGH();
  myDelay();

  //データ2
  for (ii = 0; ii < 8; ii++) {
    SCK_0_LOW();

    if (((data_q << ii) & 0x80) == 0) {
      SDA_0_LOW();
    } else {
      SDA_0_HIGH();
    }  //endif

    myDelay();
    SCK_0_HIGH();
    myDelay();
  }  //for

  //ack
  SCK_0_LOW();
  SDA_0_HIGH();
  myDelay();
  SCK_0_HIGH();
  myDelay();

  //STOP
  SCK_0_LOW();
  SDA_0_LOW();
  myDelay();
  SCK_0_HIGH();
  myDelay();
  SDA_0_HIGH();
  myDelay();

}  //i2c_lcd_w


//I2Cに一文字を出力
void ns_putc(char ch) {

  data_read[0] = '@';  //データ
  data_read[1] = ch;   //文字

  //I2Cに送信
  i2c_lcd_w(data_read);

}  //ns_putc


//I2Cに文字列を出力
void ns_puts(const char *str1) {

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

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

  }  //while

}  //ns_puts


//カーソルの移動
void setCursor(int col, int rows) {

  data_read[0] = 0x00;                        //コマンド
  data_read[1] = 0x80 | (rows * 0x40) | col;  //カーソルを移動

  //I2Cに送信
  i2c_lcd_w(data_read);

}  //setCursor


void Soft_I2C_begin() {

  //GPIOポートの初期化
  pinMode(PB7, OUTPUT_OPEN_DRAIN);
  pinMode(PC14, OUTPUT_OPEN_DRAIN);
  digitalWrite(PB7, HIGH);
  digitalWrite(PC14, HIGH);

}  //Soft_I2C_begin


//初期化関数
void setup() {

  //I2Cの初期化
  Soft_I2C_begin();

  //液晶の初期化
  for (int ii = 0; ii < 9; ii++) {
    i2c_lcd_w(&INIT_com[ii << 1]);  // ii << 1 = ii * 2
  }  //for

  //画面のクリア
  i2c_lcd_w(INIT_cls);

  //表示 debug
  //setCursor(0, 0); ns_puts("HELLO");
  //setCursor(0, 1); ns_puts("WORLD");
  //delay(1000);  // Wait 1 seconds for next

  //ADCの初期化(3.3Vを4096に分解)
  analogReadResolution(12);  //センサーの分解度4096

}  //setup


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

  //画面のクリア
  //i2c_lcd_w(INIT_cls);


  int sensorValue;  //センサーの値
  int Voltage;      //電圧

  sensorValue = analogRead(A3);          //センサーから値を読み込む A3は、PA3の事
  Voltage = (sensorValue * 3300) >> 12;  //センサーの値から電圧に変換

  //Voltage = 1235;

  //整数から文字列に変換する
  Voltage = Voltage + 1000;
  String thisString1 = String(Voltage);
  char *str1 = (char *)thisString1.c_str();

  //いつ、消えてもおかしく無いので、直下でコピーする。
  char str2[] = "12.34";
  str2[0] = str1[0] - 1;  //1000足したから1引く
  str2[1] = str1[1];
  str2[2] = '.';
  str2[3] = str1[2];
  str2[4] = str1[3];
  str2[5] = 0x00;


  //表示
  setCursor(0, 0); ns_puts(str2);
  delay(1000);  // Wait 1 seconds for next

}  //loop



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