LoginSignup
0
0

More than 1 year has passed since last update.

STM32G031でアナログサーボ信号のモニター(ソフトウェアシリアル出力)

Last updated at Posted at 2022-05-23

x 動作確認済み 2022/5/24 9:22

目的
アナログサーボ信号をモニターしたい。
既存のプログラムの使い回し

1
2 VDD
3 GND
4 GPIO (したかったからした 特に指定なし)

8 SWD SWCLK
7 SWD SWDIO
6 デバッグ出力 9600bpsソフトウェアシリアル
5 PWM(IN) 400uS=0,2400uS=255

o_con487.jpg

o_con488.jpg

送信 138 ( 1500us )を固定で送っている。 PWM_servo_test_031_P

プログラム



//PWM_to_MON_031_1

#include <Arduino.h>

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

#define in_pwm   PA11 // 5pin

#define DW   digitalWrite
#define TX1        PA12  // 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


//初期化
void setup() {

  delay(3000); //not Delete

  pinMode(in_pwm, INPUT); //pa11

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

} //setup


//メインループ
void loop()
{
 
  //PWMでデータ取得
  int pwmco2=pulseIn(in_pwm,HIGH,2000000);

/*
  //データの表示 debug
  String thisString9 = String( pwmco2 );
  pc_printf( (char *)thisString9.c_str() );
  pc_printf( "\r\n" );
*/

  if(pwmco2 <= 400  ) { pwmco2 = 400; }
  if(pwmco2 >= 2400 ) { pwmco2 = 2400; }

  pwmco2 = pwmco2 - 400;
  
  int vel = (pwmco2 * 523) >> 12;
 
  //表示 debug
  //integer to String
  char out_buff[8];
  out_buff[3] = 0;
  out_buff[2] = '0' + (  vel - (DIV10(vel) * 10)  );  // '0'+(b%10)
  vel = DIV10(vel);                                   //  b/10
  out_buff[1] = '0' + (  vel - (DIV10(vel) * 10)  );  // '0'+(b%10)
  out_buff[0] = '0' +  DIV10(vel);                    // '0'+(s/10)

  //送信処理
  //send 8 byte
  pc_printf(out_buff);
  pc_printf("\r\n");

  //0.1秒の待ち
  delay(100); //←ここで送信間隔を調整する

} //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