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


x 過去ログをみよ


![o_coq183.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/ce6d3a4c-101a-ed0f-7955-841de3533d61.jpeg)


```c++



//ili9341_Sound_Oscilloscope2_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)


//8ビット転送(通常)
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;


//レジ直の高速タイプ 8ビット転送
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, int L2)
{

  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

  //L1 L1  L1 L1 L1 L1 L1 L1 L1 L1 L1 L1 L1 L1

  //
  for (int x = 0; x < (60 - 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 < (60 - L1); x++) {

    REG_CS_OFF;
    GPIO_8BIT_A(0xff);
    REG_CS_ON;

    REG_CS_OFF;
    GPIO_8BIT_A(0xff);
    REG_CS_ON;

  }//for

  //L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2 L2

  //
  for (int x = 0; x < (60 - L2); x++) {

    REG_CS_OFF;
    GPIO_8BIT_A(0xff);
    REG_CS_ON;

    REG_CS_OFF;
    GPIO_8BIT_A(0xff);
    REG_CS_ON;

  }//for

  //L2
  for (int x = 0; x < (L2 * 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 < (60 - L2); 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(60);
  int L2 = random(60);

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

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

} //loop


```