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.

STM32F767とGP2Y0A21YK(赤外線距離センサー)で6cmから80cmを求める。

Last updated at Posted at 2021-05-25

目的
adcのテスト用

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


#include "mbed.h"

//アナログ入力の設定
//AnalogIn adc_vbat(A3); //PA_4
AnalogIn adc_vbat(A0); //767


//Serial pc(USBTX, USBRX); // tx, rx
Serial pc(SERIAL_TX, SERIAL_RX); //767
//Serial pc(PA_2, PA_3); //010
//Serial pc(PA_9, PA_10); //010
//RawSerial pc(PA_2, PA_3); //010

//赤外線距離センサー(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

//メイン関数
int main()
{
    
    float Voltage;     //電圧
    float ir_length;   //長さ

    //無限ループ
    while(1){
        
        //adcの読み込み 0から4096
        int s = (adc_vbat.read_u16()>>4);
//d        s = (4096/2)-1;
        
//d        pc.printf("  -S=%d",s); //767
        
        //電圧に変換 ex 3.300 -> 3300 mVを出力
        s=(s*6600)>>13;
        
        //s = 3300;
//d        pc.printf("\t c=%d",s); //767
        
        Voltage = (float)s / 1000.0f;
        
//d        pc.printf("\t v=%f",Voltage); //767
        
        //赤外線距離センサー(GP2Y0A21YK)の電圧から距離を求める。
        ir_length = ir_len(Voltage);
        
//d        pc.printf("\t l=%f",ir_length); //767
        
//d        pc.printf("\r\n"); //767 リターン
        
        //距離の出力
        pc.printf("%f\r\n",ir_length); //767
    
        wait_ms(1000);
    
    }//while
}//main



com_1_d.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?