(高精度)電圧の表示(ST7789,STM32G031,MCP3425)


(高精度)電圧の表示(ST7789,STM32G031,MCP3425)


x わかる人むけ
x 過去ログを見よ
x 4PINをOBでGPIO
x 書き換え前に3秒いないにコネクトしてイレース
x 6GPIOの為に開発に難あり


目的
液晶のテスト
0-2.048000Vで高精度


![o_cop818.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/7bdc5e58-9970-86f3-6ac9-51507aa7dd16.jpeg)



![o_cop819.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/f9777a30-db41-8210-1380-3219ec68b817.jpeg)




i2c_run.h


```c++:i2c_run.h



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


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


void i2c_setup(){

  //i2cの初期化
  Wire.setSDA(PA12);
  Wire.setSCL(PA11);
  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 i2c_run(NA_ST7735_P *display_bk){

  NA_ST7735_P display = *display_bk;

  //変換、表示
  //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;

  // テキストサイズを設定
  display.setTextSize(3);
  // テキスト色を設定
  display.setTextColor(WHITE);
  // テキストの開始位置を設定
  display.setCursor(0, 0);
 
  // 1行目に46を表示
  display.println();
  display.println("-----------");
  display.println("Voltage");
  display.println();
  display.print(" ");
  display.print(str1);
  display.print("V");
  display.println();
  display.println("-----------");

}//i2c_run


```

メインプログラム

lil9341_SPI_monotone_REG5_031


```c++:lil9341_SPI_monotone_REG5_031



//ili9341_SPI_monotone_REG5_031

//ヘッダーファイル
#include <Adafruit_GFX.h>
#include "hh.h"
#include "i2c_run.h"

//定義
#define SCREEN_WIDTH  (240-16-16) // OLED display width, in pixels
#define SCREEN_HEIGHT 200 // OLED display height, in pixels
NA_ST7735_P display(SCREEN_WIDTH, SCREEN_HEIGHT);


//初期化
void setup() {

  delay(3000); //do not delete

  // LCDの初期化
  display.begin();

  //i2cの初期化
  i2c_setup();

}//setup


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

  // 画面表示をクリア
  display.clearDisplay();

  i2c_run(&display);

  // 描画バッファの内容を画面に表示
  display.display();

  delay(3000); //1秒待つ

}//loop 



```

hh.cpp


```c++:hh.cpp


 
//インクルド
#include "hh.h"
#include <Adafruit_GFX.h>


//定義
#define NA_ST7735_P_swap(a, b)                                                     \
  (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) ///< No-temp-var swap operation
//GPIOの設定1　開始
#define DW    digitalWrite
//#define TFT_RST   A1
//#define TFT_RS    PA11
#define TFT_RS    PB7
#define TFT_CS    PA0
//#define TFT_CS    PA12
//#define GPIO_RESET(s)  DW(TFT_RST,s)
#define GPIO_RS(s)     DW(TFT_RS,s)
#define GPIO_CS(s)     DW(TFT_CS,s)

//#define TFT_MOSI PA12
//#define TFT_SCK  PB7

//honban
#define TFT_MOSI PA13
#define TFT_SCK  PA14

//debug
//#define TFT_MOSI PA12
//#define TFT_SCK  PA11

#define SPI_MOSI_HIGH() DW(TFT_MOSI,HIGH)
#define SPI_MOSI_LOW()  DW(TFT_MOSI,LOW)
#define SPI_SCK_HIGH()  DW(TFT_SCK,HIGH)
#define SPI_SCK_LOW()   DW(TFT_SCK,LOW)


void SPI_WRITE8(int x)
{
  for (int bit = 0; bit < 8; bit++) {
    if (x & 0x80) {
      SPI_MOSI_HIGH();
      SPI_SCK_HIGH();
      SPI_SCK_LOW();
    } else {
      SPI_MOSI_LOW();
      SPI_SCK_HIGH();
      SPI_SCK_LOW();
    }//end if
    x <<= 1;
  }//for
}//SPI_WRITE8

//c = CS,m=MOSI,C=SPICLK
//TFT_SPICLK  PA14
//TFT_MOSI    PA13
//TFT_CS      PA12(仮)
//TFT_CS      PA0(本番)
//                                          Cmc
//                                         5432109876543210
#define REG_MOSI_ON           GPIOA->ODR=0b0010000000000000;
//                                          Cmc
//                                         5432109876543210
#define REG_CLK_ON__MOSI_1    GPIOA->ODR=0b0110000000000000;
//                                          Cmc
//                                         5432109876543210
#define REG_MOSI_OFF          GPIOA->ODR=0b0000000000000000;
//                                          Cmc
//                                         5432109876543210
#define REG_CLK_ON__MOSI_0    GPIOA->ODR=0b0100000000000000;
//                                          Cmc
//                                         5432109876543210
#define REG_CLK_OFF           GPIOA->ODR=0b0000000000000000;


void SPI_WRITE8_A(int s)
{
  //1
  if (s & 0x80) {
    REG_MOSI_ON;
    REG_CLK_ON__MOSI_1;
    REG_CLK_OFF;
  } else {
    REG_MOSI_OFF;
    REG_CLK_ON__MOSI_0;
    REG_CLK_OFF;
  }//end if
  //2
  if (s & 0x40) {
    REG_MOSI_ON;
    REG_CLK_ON__MOSI_1;
    REG_CLK_OFF;
  } else {
    REG_MOSI_OFF;
    REG_CLK_ON__MOSI_0;
    REG_CLK_OFF;
  }//end if
  //3
  if (s & 0x20) {
    REG_MOSI_ON;
    REG_CLK_ON__MOSI_1;
    REG_CLK_OFF;
  } else {
    REG_MOSI_OFF;
    REG_CLK_ON__MOSI_0;
    REG_CLK_OFF;
  }//end if
  //4
  if (s & 0x10) {
    REG_MOSI_ON;
    REG_CLK_ON__MOSI_1;
    REG_CLK_OFF;
  } else {
    REG_MOSI_OFF;
    REG_CLK_ON__MOSI_0;
    REG_CLK_OFF;
  }//end if
  //5
  if (s & 0x08) {
    REG_MOSI_ON;
    REG_CLK_ON__MOSI_1;
    REG_CLK_OFF;
  } else {
    REG_MOSI_OFF;
    REG_CLK_ON__MOSI_0;
    REG_CLK_OFF;
  }//end if
  //6
  if (s & 0x04) {
    REG_MOSI_ON;
    REG_CLK_ON__MOSI_1;
    REG_CLK_OFF;
  } else {
    REG_MOSI_OFF;
    REG_CLK_ON__MOSI_0;
    REG_CLK_OFF;
  }//end if
  //7
  if (s & 0x02) {
    REG_MOSI_ON;
    REG_CLK_ON__MOSI_1;
    REG_CLK_OFF;
  } else {
    REG_MOSI_OFF;
    REG_CLK_ON__MOSI_0;
    REG_CLK_OFF;
  }//end if
  //8
  if (s & 0x01) {
    REG_MOSI_ON;
    REG_CLK_ON__MOSI_1;
    REG_CLK_OFF;
  } else {
    REG_MOSI_OFF;
    REG_CLK_ON__MOSI_0;
    REG_CLK_OFF;
  }//end if
}//SPI_WRITE8


void NA_ST7735_P::GPIO_8BIT(uint8_t s)
{
  SPI_WRITE8(s);
} //GPIO_8BIT


//コマンドの書き込み
void NA_ST7735_P::LCD_Write_CMD(uint8_t a)
{
  GPIO_CS(0);//CS=0; 12
  GPIO_RS(0); //A0=0; 9
  GPIO_8BIT(a);//P1=a; data SPI SPI
  GPIO_CS(1);//CS=1; 12
} //LCD_Write_CMD


//データ書き込み
void NA_ST7735_P::LCD_Write_Data(uint8_t a)
{
  GPIO_CS(0);//CS=0; 12
  GPIO_RS(1); //A0=1; 9
  GPIO_8BIT(a);//P1=a; data SPI SPI
  GPIO_CS(1);//CS=1; 12
} //LCD_Write_Data


//液晶の初期化処理
void NA_ST7735_P::TXDT144TF_ST7735S_Init(void)
{

  //GPIO_RESET(1);//LCD_RESET=1;
  //delay(1); //Delay 1ms

  //GPIO_RESET(0);//LCD_RESET=0;
  //delay(1); //Delay 1ms

  //GPIO_RESET(1);//LCD_RESET=1;
  //delay(120); //Delay 120ms

  LCD_Write_CMD(0x01);//SOFTWARE RESET
  delay(50);

  LCD_Write_CMD(0x01);//SOFTWARE RESET
  delay(50);

  LCD_Write_CMD(0x11);//SLEEP OUT
  delay(200);

  LCD_Write_CMD(0x29);//display on
  delay(100);

  LCD_Write_CMD(0x36);//RGB-RGR format
  LCD_Write_Data(0x00);//RGB mode
  delay(100);

  LCD_Write_CMD(0x3a);//Interface pixel format
  LCD_Write_Data(0x55);//16bit mode
  delay(100);

  LCD_Write_CMD(0x21); //Reverse
  delay(100);

} //TXDT144TF_ST7735S_Init


NA_ST7735_P::NA_ST7735_P(uint16_t w, uint16_t h)
  : Adafruit_GFX(w, h), buffer(NULL)

{
}


//バッファのクリア
NA_ST7735_P::~NA_ST7735_P(void) {
  if (buffer) {
    free(buffer);
    buffer = NULL;
  }
}//~NA_ST7735_P


//初期処理
bool NA_ST7735_P::begin(void) {

  if ((!buffer) && !(buffer = (uint8_t *)malloc( (((WIDTH)/8)*HEIGHT) ) )    )
    return false;

  //バッファーのクリア
  clearDisplay();

  //ポートのモード設定　アウトプットモード
  pinMode(TFT_CS, OUTPUT);
  //pinMode(TFT_RST, OUTPUT);
  pinMode(TFT_RS, OUTPUT);

  pinMode(TFT_MOSI, OUTPUT);
  pinMode(TFT_SCK , OUTPUT);


  //ポートの初期化
  GPIO_CS(1);//CS=1
  //GPIO_RESET(1);//RESET=1
  GPIO_RS(0);//RS=0


  delay(500); //0.5秒待つ

  //液晶の初期化処理
  TXDT144TF_ST7735S_Init();

  //画面の書き込み開始
  //display();

  return true; // Success
}//begin


//点の表示
void NA_ST7735_P::drawPixel(int16_t x, int16_t y, uint16_t color) {
  if ((x >= 0) && (x < width()) && (y >= 0) && (y < height())) {
    // Pixel is in-bounds. Rotate coordinates if needed.
    switch (getRotation()) {
      case 1:
        NA_ST7735_P_swap(x, y);
        x = WIDTH - x - 1;
        break;
      case 2:
        x = WIDTH - x - 1;
        y = HEIGHT - y - 1;
        break;
      case 3:
        NA_ST7735_P_swap(x, y);
        y = HEIGHT - y - 1;
        break;
    }
    switch (color) {
      case NA_ST7735_P_WHITE:
        //0      239
        //1      238
        //2      237
        //x = (WIDTH - 1) - x;
        buffer[(x / 8) +    (y * 26)  ] |=  (1 << (7 - (x & 7)));
        break;
      case NA_ST7735_P_BLACK:
        //x = (WIDTH - 1) - x;
        buffer[(x / 8) +    (y * 26)  ] &= ~(1 << (7 - (x & 7)));
        break;
      case NA_ST7735_P_INVERSE:
        //x = (WIDTH - 1) - x;
        buffer[(x / 8) +    (y * 26)  ] ^=  (1 << (7 - (x & 7)));
        break;
    }
  }
}


//バッファのクリア
void NA_ST7735_P::clearDisplay(void) {
  for(int ii=0;ii<( ((WIDTH)/8)*HEIGHT   );ii++){
    buffer[ii]=0;
  }//for
}


bool NA_ST7735_P::getPixel(int16_t x, int16_t y) {
  if ((x >= 0) && (x < width()) && (y >= 0) && (y < height())) {
    // Pixel is in-bounds. Rotate coordinates if needed.
    switch (getRotation()) {
      case 1:
        NA_ST7735_P_swap(x, y);
        x = WIDTH - x - 1;
        break;
      case 2:
        x = WIDTH - x - 1;
        y = HEIGHT - y - 1;
        break;
      case 3:
        NA_ST7735_P_swap(x, y);
        y = HEIGHT - y - 1;
        break;
    }
    //x = (WIDTH - 1) - x;
    return (buffer[(x / 8) +    (y * 26)  ] &  (1 << (7 - (x & 7))));
  }
  return false; // Pixel out of bounds
}


uint8_t *NA_ST7735_P::getBuffer(void) {
  return buffer;
}


//TFT_CS      PA12(仮)
//TFT_CS      PA0(本番)
//                                          Cm            c
//                                         5432109876543210
#define GPIO_CS_0()           GPIOA->ODR=0b0000000000000000;
//                                          Cm            c
//                                         5432109876543210
#define GPIO_CS_1()           GPIOA->ODR=0b0000000000000001;

//#define WR_D(xx)  GPIO_CS(0);SPI_WRITE8(xx);GPIO_CS(1);
#define WR_D(xx)  GPIO_CS_0();SPI_WRITE8_A(xx);GPIO_CS_1();


void g_line20() {
  //16dot
  for (int ooi = 0; ooi < 15; ooi++) {
    WR_D(0b00000110);
    WR_D(0b11100000);
  }//ooi
  WR_D(0b00000000);
  WR_D(0b0000000);
  for (int ppj = 0; ppj <  240 - 16 - 16; ppj++) {
    WR_D(0);
    WR_D(0);
  } //j
  //16dot
  WR_D(0b00000000);
  WR_D(0b00000000);
  for (int ooi = 0; ooi < 15; ooi++) {
    WR_D(0b00000110);
    WR_D(0b11100000);
  }//ooi
}//g_line20()


void NA_ST7735_P::display(void) {

  //画面の書き込み開始
  LCD_Write_CMD(0x2C); //memory write

  GPIO_RS(1); //A0=1; 9

  delay(4);

  int jjk = 0, vg = 0;
  int hp[8] = {0x80, 0x40, 0x20, 0x10,  0x08, 0x04, 0x02, 0x01};

  //20line
  for (int ooi = 0; ooi < (20); ooi++) {
    g_line20();
  }

  for (int i = 0; i < HEIGHT; i++) {

    //16dot
    for (int ooi = 0; ooi < 15; ooi++) {
      WR_D(0b00000110);
      WR_D(0b11100000);
    }//ooi
    WR_D(0b00000000);
    WR_D(0b0000000);

    //240-16-16
    for (int j = 0; j < ((WIDTH / 8)); j++) {
      //8dot
      vg = buffer[jjk];
      for (int k = 0; k < 8; k++) {
        if ( ( vg & hp[k]) == 0 ) {
          WR_D(0);
          WR_D(0);
        } else {
          WR_D(0xff);
          WR_D(0xff);
        }//end if
      }//k
      jjk++;
    } //j

    //16dot
    WR_D(0b00000000);
    WR_D(0b00000000);
    for (int ooi = 0; ooi < 15; ooi++) {
      WR_D(0b00000110);
      WR_D(0b11100000);
    }//ooi

  }//i

  //20line
  for (int ooi = 0; ooi < (20); ooi++) {
    g_line20();
  }//ooi

}//display



```

hh.h


```c++:hh.h



 
#ifndef _NA_ST7735_P_H_
#define _NA_ST7735_P_H_

#include <Adafruit_GFX.h>

#ifndef NO_ADAFRUIT_NA_ST7735_P_COLOR_COMPATIBILITY
#define BLACK NA_ST7735_P_BLACK     ///< Draw 'off' pixels
#define WHITE NA_ST7735_P_WHITE     ///< Draw 'on' pixels
#define INVERSE NA_ST7735_P_INVERSE ///< Invert pixels
#endif
/// fit into the SSD1306_ naming scheme
#define NA_ST7735_P_BLACK 0   ///< Draw 'off' pixels
#define NA_ST7735_P_WHITE 1   ///< Draw 'on' pixels
#define NA_ST7735_P_INVERSE 2 ///< Invert pixels


class NA_ST7735_P : public Adafruit_GFX {
  public:

    NA_ST7735_P(uint16_t w, uint16_t h);

    ~NA_ST7735_P(void);

    bool begin(void);
    void display(void);
    void clearDisplay(void);
    void drawPixel(int16_t x, int16_t y, uint16_t color);
    bool getPixel(int16_t x, int16_t y);
    uint8_t *getBuffer(void);

    void GPIO_8BIT(uint8_t s);
    void LCD_Write_CMD(uint8_t ww);
    void LCD_Write_Data(uint8_t ii);
    void TXDT144TF_ST7735S_Init(void);


  protected:


    uint8_t *buffer; ///< Buffer data used for display buffer. Allocated when
    ///< begin method is called.

};

#endif // _NA_ST7735_P_H_



```