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とMCP3425とGP2Y0A21YK(赤外線距離センサー)で約10cmから80cmを求める。

Last updated at Posted at 2021-05-24

目的
adcのテスト用

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


#include <Wire.h>

//STM32G031J6M6 i2cピンの定義
#define sdaPin PA12    // ArduinoA4
#define sclPin PA11    // ArduinoA5

//MCP3425A0T-E/CHのアドレス
#define ADD 0x68

//#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)

//16ビットadcの4ビットの変換テーブル
#define ADC4BIT { 0   , 62 ,125 ,187 ,250 ,312 ,375 ,437 ,500 ,562 ,625 ,687 ,750 ,812 ,875 ,937 }

//仮想シリアルへの一文字出力 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


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

  //while(1){pc_putc('A');delay(1000);} //debug
  //pc_printf("\r\nSTART\r\n"); //debug

  //Wire.begin();         //i2cの初期化 uno
  Wire.begin(sdaPin,sclPin); //STM32G031J6M6

  //Serial.begin(9600);

    // 7 *(1)待ち
    // 6 *(0)
    // 5 *(0)
    // 4 (0)ワンショット,*(1)連続
    // 3 | (00)12ビット,(01)14ビット
    // 2 | *(10)16ビット
    // 1 I *(00)x1,(01)x2
    // 0 I (10)x4,(11)x8

  //初期値の書き込み
  Wire.beginTransmission(ADD);
    Wire.write(0b10011000); //16bit 15sps PGA x1
  Wire.endTransmission();

} //setup

int read_data()
{
  //2文字の読み込み
  Wire.requestFrom(ADD, 2);

  //戻し
  return ( (Wire.read() << 8 ) + Wire.read() );
}//read_data


int s;   //101
int ii;     //ループカウンタ
int jj;     //ループカウンタ
int bit4[]=ADC4BIT;

float Voltage;     //電圧
float ir_length;   //長さ

void loop()
{
  s = read_data();
  //s = (65536/2)-1;
  //s = 65536/4;

char *gk;

ii=(s >> 4);
if     ( ii >= 2000 ) { ii = ii - 2000; gk="2.";  jj=2;  }
else if( ii >= 1000 ) { ii = ii - 1000; gk="1.";  jj=1;  }
else                  {                 gk="0.";  jj=0;  }

  //Serial.print( gk ); //uno
  //pc_printf( gk ); //debug_p

  //Serial.print( ch_hex_a( ii ) ); //uno
  //pc_printf( ch_hex_a( ii ) ); //debug_p
  
  s = bit4[ s & 0xf ];
  //Serial.print( ch_hex_a( s ) ); //uno
  //pc_printf( ch_hex_a( s ) ); //debug_p

  //pc_printf( "," ); //debug_p

  Voltage=jj + (  (float)(ii) / 1000.0  ) + (  (float)(s) / 1000000.0  );

//d  String thisString2 = String(Voltage);     // debug_o
//d  pc_printf( (char *)thisString2.c_str() ); // debug_o
//d  pc_printf( "," );                         // debug_o

//start 変換 変換 変換 変換 変換 変換 

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

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

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

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

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

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

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

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

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

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

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

  }

//end 変換 変換 変換 変換 変換 変換 

  //pc_printf( "," ); //debug_i
  String thisString3 = String(ir_length);
  pc_printf( (char *)thisString3.c_str() );

  
  //Serial.println(""); //uno
  pc_printf("\r\n");



  delay(1000);
} //loop




com_1_c.jpg

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?