STM32G031とGP2Y0A21YK(距離センサー)で6cmから80cmを求める。 ソフトウェアシリアル出力


目的
adcのテスト用

どうしてこうなったかは、6-10,10-20,20-80を参考



//#define in7 1 //uno
#define in7      PB7  // 1pin

#define DW   digitalWrite

//#define UART_DELAY 832 //  1200bps ok 031
#define UART_DELAY 102   //  9600bps ok 031

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

//仮想シリアルへの一文字出力 9600bps
int pc_putc(char ch) {

  DW(in7, HIGH);

    //q_st =  micros(); //debug

  DW(in7, LOW);//START
  delayMicroseconds(UART_DELAY);

  for(int ii=0;ii<8;ii++){
    DW(in7, (ch>>ii)&1  );
    delayMicroseconds(UART_DELAY);
  }//for

  DW(in7, HIGH);//Stop
  delayMicroseconds(UART_DELAY);

    //q_et =  micros(); //debug 引いた数が0で1041usなら正解

  return(0);

}//pc_putc

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

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

        //一文字出力
        pc_putc(*str1 ++);
        //Serial.print( *str1 ++ ); //uno

    } //while

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

//0から999まで文字列に変換
char ch_hex_a_b[5];
char *ch_hex_a(int l_num)
{
  int a,b,c;

  b=l_num/10;
  c=l_num-(b*10); //1の桁
  a=b/10;         //100の桁
  b=b-(a*10);     //10の桁

  ch_hex_a_b[0] = '0' + a;
  ch_hex_a_b[1] = '0' + b;
  ch_hex_a_b[2] = '0' + c;
  ch_hex_a_b[3] = 0;

  return(ch_hex_a_b);
}//ch_hex_a

//赤外線距離センサー(GP2Y0A21YK)の電圧から距離を求める。
//Voltage //電圧
float ir_len(float Voltage)
{

    float ir_length;   //長さ

    if (Voltage >= 0.0f  && Voltage <= 0.4f) {Voltage = 0.4f;}
    if (Voltage >= 3.12f && Voltage <= 5.0f) {Voltage = 3.12f;}

    if        (Voltage >= 0.4f && Voltage <= 1.3f ) { //80-20

        ir_length = 1.0f/ ( 0.0125f + ( ( Voltage - 0.4f) *  (0.0375f/0.9f) ) );

    } else if (Voltage >= 1.3f && Voltage <= 2.3f ) { //20-10

        ir_length = 1.0f/ ( 0.05f   + ( ( Voltage - 1.3f) * 0.05f ) );

    } else if (Voltage >= 2.3f && Voltage <= 2.75f)  { //10-8

        ir_length = 1.0f/ ( 0.1f + ( ( Voltage - 2.3f) * (0.025f/0.45f) ) );

    } else if (Voltage >= 2.75f && Voltage <= 2.98f) { //8-7

        ir_length = 1.0f/ ( 0.125f + ( ( Voltage - 2.75f) * (0.0179f/0.23f) ) );

    } else if (Voltage >= 2.98f && Voltage <= 3.12f) { //7-6

        ir_length = 1.0f/ ( 0.1429f + ( ( Voltage - 2.98f) * (0.0238f/0.14f) ) );

    }//end if

    //戻り値
    return(ir_length);

} //ir_len

//初期化
void setup()
{
  //ポートをhiにする 初期化
  pinMode(in7, OUTPUT);
  DW(in7, HIGH);
  delayMicroseconds(UART_DELAY*10);

  //Serial.begin(9600);

} //setup

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

  int s; //センサーの値  //101

  float Voltage;      //電圧
  float ir_length;    //長さ
  int ir_length_i;    //長さ
  int ir_length_s;    //長さ 小数点以下

  s = analogRead(A3); // PA11 PIN5
  //s = (65536/2)-1;
  //s = 65536/4;
  //s=(4096/2);
  //s=(1024/2);

//d  //pc_printf( "," ); //debug_i
//d  String thisString1 = String(s);
//d  pc_printf( (char *)thisString1.c_str() );
//d  pc_printf("\r\n");

  //電圧に変換 ex 3.300 -> 3300 mVを出力
  s=(  (s*4)  *6600)>>13;

//d  //pc_printf( "," ); //debug_i
//d  String thisString2 = String(s);
//d  pc_printf( (char *)thisString2.c_str() );
//d  pc_printf("\r\n");

  //電圧に変換 3300mv 3.300V
  Voltage = (float)s / 1000.0f;
  //赤外線距離センサー(GP2Y0A21YK)の電圧から距離を求める。
  ir_length = ir_len(Voltage);

  //小数点以上と小数点以下に分離
  ir_length_i = (int)ir_length;
  ir_length_s = (int)(ir_length*1000);
  ir_length_s = ir_length_s - (ir_length_i * 1000);

  //距離の小数点以上の表示
  pc_printf(  ch_hex_a(ir_length_i)  );

  //小数点
  pc_printf(  "."  );

  //距離の小数点以下の表示
  pc_printf(  ch_hex_a(ir_length_s)  );
  pc_printf(  "\r\n"  );

  //1秒の待ち
  delay(1000);

} //loop