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

More than 3 years have passed since last update.

STM32G031で半二重通信で遊ぶ

Last updated at Posted at 2022-05-28

x 動作確認済み2022/5/28 19:26

x OBでリセットオンリーがわかる人むけ

目的
RXまたは、TXの1ピン削減する。
動くかどうかわからない


    void setHalfDuplex(void);
    bool isHalfDuplex(void) const;
    void enableHalfDuplexRx(void);

(成功)

o_con497.jpg

名前は、まだない。




//SER_HalfDuplex_test_031_1


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


//初期化
void setup() {

  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





(成功)

o_con498.jpg



//SER_HalfDuplex_re_test_031_1

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

//プロトタイプ宣言
void pc_putc(char ch);
int pc_printf(char *str1);
#define TX1        PA11  // 6pin


int u_key;      // 受信データを格納するchar型の変数
int ch_in_key() {

  // 受信データがあった時だけ、処理を行う
  while (!Serial.available()) {}      // 受信データがあるか?
  u_key = Serial.read();            // 1文字だけ読み込む

  return (u_key);
}


//初期化
void setup() {

  Serial.setHalfDuplex();
  Serial.begin(9600);
  Serial.enableHalfDuplexRx(); //オープンした後で切り替え

  pinMode(PB7, OUTPUT);

  //TXポートの初期化
  pinMode(TX1, OUTPUT);
  digitalWrite(TX1, HIGH);


  pc_printf("<ST>\r\n");

}//setup



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

  pc_printf("<INPUT>\r\n");
  int ch = ch_in_key();
  pc_printf("<CH>\r\n");

  if (ch == 's') {
    digitalWrite(PB7, HIGH);
  } else if (ch == 'r') {
    digitalWrite(PB7, LOW);
  }//endif スカは、何もしない スカとは、「はずれ」のことである。

  //delay(2000); //debug

}//loop


//10の割り算 0から1028までは、正しい。主に0から999
#define DIV10(n) ((n*205)>>11)

#define DW   digitalWrite
//#define TX1        PA11  // 6pin
#define UART_DELAY 106   // 002-110 9600bps ok 031

//仮想シリアルへの一文字出力 9600bps
void pc_putc(char ch) {
  
  DW(TX1, HIGH);

  DW(TX1, LOW);//START
  delayMicroseconds(UART_DELAY); //START BIT WAIT

  for (int ii = 0; ii < 8; ii++) {
    DW(TX1, (ch >> ii) & 1  );
    delayMicroseconds(UART_DELAY); //DATA 1-8BIT WAIT
  }//for

  DW(TX1, HIGH);//Stop
  delayMicroseconds(UART_DELAY); //StOP BIT WAIT

}//pc_putc


//文字列の表示
int pc_printf(char *str1) {

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

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

  } //while

  //戻り値
  return (0);
}//pc_printf



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