STM32G031J6M6のPA2ハードウェアシリアルから出力(2日掛けて完成)


x 動作確認済み2022/5/28 20:16

x OBでPA0をGPIOにするがわかる人むけ

x 場合によっては、二度と書き込めなくなります。


目的
動くかどうかわからないがやってみる
結果動いた


答え合わせ


```c++


  Serial.setTx(PA2_ALT1);
  //Serial.setTx(PA14);
  //Serial.setTx(PB6);
  Serial.setHalfDuplex();
  Serial.begin(9600);


```


![o_con501.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/9bdff2c0-5b3e-5063-0cb9-a6fda29b6cdb.jpeg)


![o_con502.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/246f7ac1-7b74-3416-7d3c-f39788ca9f31.jpeg)


![o_con503.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/29b2f127-1b33-29b7-96fd-16e5bf5d5c99.jpeg)



```


8

  PB6  USART1_TX

  PA14 USART2_TX 
  PA15 USART2_RX

1

  PB7 USART1_RX

4

  PA2 USART2_TX

  PA2 LPUART_TX



```



名前は、まだない


```c++



STM32G031-DISCO

Enabled(generic Serial)
 Serial1.begin(9600);    OK?->NG
 Serial2.begin(9600);    NG
 SerialLP1.begin(9600);  NG

Enabled(no generic Serial)
 Serial1.begin(9600);    NG
 Serial2.begin(9600);    NG
 SerialLP1.begin(9600);  NG

Disabled(no Serial support)
 Serial1.begin(9600);    NG
 Serial2.begin(9600);    NG
 SerialLP1.begin(9600);  NG



```


```c++


Nucleo G031K8

Enabled(generic Serial)
 Serial1.begin(9600);    NG
 Serial2.begin(9600);    NG
 SerialLP1.begin(9600);  OK?->NG

Enabled(no generic Serial)
 Serial1.begin(9600);    NG
 Serial2.begin(9600);    NG
 SerialLP1.begin(9600);  NG

Disabled(no Serial support)
 Serial1.begin(9600);    NG
 Serial2.begin(9600);    NG
 SerialLP1.begin(9600);  NG


```

検証プログラム


```c++


//SER_PA_2_test_031_2

#include <Arduino.h>
#include <HardwareSerial.h>

//初期化
void setup() {

  delay(3000); //not Delete

 //extern HardwareSerial Serial;
 //Serial.setTx(PA2);
 //Serial.setRx(PA2);
 //Serial.setHalfDuplex();
 //Serial.begin(9600);
 //while(1){Serial.println("0");delay(2000);}


 //extern HardwareSerial Serial1;
 //Serial1.setTx(PA2);
 //Serial1.setRx(PA2);
 //Serial1.setHalfDuplex();
 //Serial1.begin(9600);
 //while(1){Serial1.println("0");delay(2000);}


 //extern HardwareSerial Serial2;
 //Serial2.setTx(PA2);
 //Serial2.setRx(PA2);
 //Serial2.setHalfDuplex();
 //Serial2.begin(9600);
 //while(1){Serial2.println('0');delay(2000);}


 //extern HardwareSerial SerialLP1;
 SerialLP1.setTx(PA2);
 SerialLP1.setRx(PA2);
 SerialLP1.setHalfDuplex();
 SerialLP1.begin(9600);
 while(1){SerialLP1.println('0');delay(2000);}

}//setup

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



```


---------------------------------------------------

(ちょとだけ前進)

PA14に注目

![o_con499.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/7b79762b-2bc4-bf4a-f0e9-baa701da4d50.jpeg)


```c++


//SER_HalfDuplex_test_031_2


#include <Arduino.h>
#include <HardwareSerial.h>


//初期化
void setup() {

  Serial.setTx(PA14);
  //Serial.setTx(PB6);
  Serial.setHalfDuplex();
  Serial.begin(9600);

  pinMode(PB7, OUTPUT);

}//setup


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

  Serial.println("Hello World");

  digitalWrite(PB7, HIGH);
  delay(1000);
  digitalWrite(PB7, LOW);
  delay(1000);

}//loop


```

-----------------------------------------------------

(完成) PA2からハローワールドの出力


![o_con500.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/56a8bf0d-4596-d7c9-e1e9-7e6f10f606eb.jpeg)



```c++



//SER_HalfDuplex_test_031_2


#include <Arduino.h>
#include <HardwareSerial.h>


//初期化
void setup() {

  delay(3000);

  Serial.setTx(PA2_ALT1);
  //Serial.setTx(PA14);
  //Serial.setTx(PB6);
  Serial.setHalfDuplex();
  Serial.begin(9600);

  pinMode(PB7, OUTPUT);

}//setup


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

  Serial.println("Hello World");

  digitalWrite(PB7, HIGH);
  delay(1000);
  digitalWrite(PB7, LOW);
  delay(1000);

}//loop



```