サウンドオシロスコープ(液晶)(ILI9341)(STM32G071)(入力ランダム)(+-オシロ風表示)


x めんどいので、過去ログを見よ　(Arduino 関連)

x STMから抽選のプレゼントで貰ったやつ

![o_coq182.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/b931077a-1a9d-abce-21bf-87841db5e019.jpeg)


```c++


//ili9341_Sound_Oscilloscope1_071_1


//定義
//GPIO
//GPIOの設定1　開始
//GPIO
#define DW    digitalWrite
#define TFT_RST   A1
#define TFT_RS    3
#define TFT_CS    4
#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 11
#define TFT_SCK  13
#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 GPIO_8BIT(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
} //GPIO_D0_D7


//TFT_RST   A1 PA1
//TFT_RS    3  PB3
//TFT_CS    4  PB5
//                                                 m C   R
//                                         5432109876543210
#define REG_MOSI_ON           GPIOA->ODR=0b0000000010000010;
//                                                 m C   R
//                                         5432109876543210
#define REG_CLK_ON__MOSI_1    GPIOA->ODR=0b0000000010100010;
//                                                 m C   R
//                                         5432109876543210
#define REG_MOSI_OFF          GPIOA->ODR=0b0000000000000010;
//                                                 m C   R
//                                         5432109876543210
#define REG_CLK_ON__MOSI_0    GPIOA->ODR=0b0000000000100010;
//                                                 m C   R
//                                         5432109876543210
#define REG_CLK_OFF           GPIOA->ODR=0b0000000000000010;


void GPIO_8BIT_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 LCD_Write_CMD(int 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 LCD_Write_Data(int 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


//TFT_RST   A1 PA1
//TFT_RS    3  PB3
//TFT_CS    4  PB5
//                                         C R
//                               5432109876543210
#define REG_CS_ON   GPIOB->ODR=0b0000000000101000
//                                         C R
//                               5432109876543210
#define REG_CS_OFF  GPIOB->ODR=0b0000000000001000

int SO_X = 0;
int SO_Y = 0;

//画面の書き込み
void Sound_Oscilloscope(int L1)
{

  if (SO_Y >= 320) {

    //画面の書き込み開始
    LCD_Write_CMD(0x2C); //memory write
    GPIO_RS(1);//RS=0; 12
    delay(2);

    SO_Y = 0;

    //while(1){} //debug
  
  }//endif

  //
  for (int x = 0; x < (120 - L1); x++) {

    REG_CS_OFF;
    GPIO_8BIT_A(0xff);
    REG_CS_ON;

    REG_CS_OFF;
    GPIO_8BIT_A(0xff);
    REG_CS_ON;

  }//for

  //L1
  for (int x = 0; x < (L1*2); x++) {

    REG_CS_OFF;
    GPIO_8BIT_A(0x00);
    REG_CS_ON;

    REG_CS_OFF;
    GPIO_8BIT_A(0x00);
    REG_CS_ON;

  }//for

  //
  for (int x = 0; x < (120 - L1); x++) {

    REG_CS_OFF;
    GPIO_8BIT_A(0xff);
    REG_CS_ON;

    REG_CS_OFF;
    GPIO_8BIT_A(0xff);
    REG_CS_ON;

  }//for

  SO_Y++;

}//Sound_Oscilloscope


//液晶の初期化処理
void 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(0x3a);//Interface pixel format
  LCD_Write_Data(0x55);//16bit mode
  delay(100);

  LCD_Write_CMD(0x36);//RGB-RGR format
//                 MY
//                 |MX
//                 ||MV
//                 |||ML
//                 ||||BGR
//                 |||||MH
//                 ||||||00      
  LCD_Write_Data(0b01001000);//RGB mode
  delay(100);


  //画面の書き込み開始
  LCD_Write_CMD(0x2C); //memory write
  GPIO_RS(1);//RS=0; 12
  delay(2);


} //TXDT144TF_ST7735S_Init


//初期化
void setup() {

  //ポートのモード設定　アウトプットモード
  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();

} //setup


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

  int L1 = random(120);

  Sound_Oscilloscope(L1); //画面の再表示

  //delay(1000); //1秒待つ debug

} //loop



```