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 1 year has passed since last update.

STM32G031で超音波距離センサーHC-SR04 PA2ハードウェアシリアル出力

Last updated at Posted at 2022-05-31

x 動作確認済み2022/6/1 7:06

目的
超音波距離センサーのテスト

仕様

1
2 VDD
3 GND
4 GPIO ハードウェアシリアル

8 SWD
7 SWD
6 エコー
5 トリガー

o_con510.jpg

親切な人がNRSTにつてい書いてある





//SER_HC-SR04_031_2


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

#define ECHO PA12
#define TRIG PA11

unsigned long times; //オン時間
int distance;        //距離


//初期化処理
void setup() {

  delay(3000); //not delete

  //シリアルの初期化
  Serial.setTx(PA2_ALT1);
  Serial.setHalfDuplex();
  Serial.begin(9600);

  //pinMode(PB7, OUTPUT);

  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);

}//setup


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

  // 超音波を発生させる
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);

  // 超音波を受け取る
  times = pulseIn(ECHO, HIGH,2000000);

  //times = (unsigned long)7700; //debug

  //distance = (int)(times * 0.017);
  distance = (int)((times * ((unsigned long)(17))) / 100);
  
  //データの表示
  Serial.println( distance );

  delay(500); //0.5秒待つ
  
}//loop


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?