目的
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
いろいろ まだ試していない あとで消す
//ir_length_s_031_1
#include <Arduino.h>
//#include <Wire.h>
//10の割り算 0から1028までは、正しい。主に0から999
#define DIV10(n) ((n*205)>>11)
#include "SER_9600_031_1.h"
//80-20
unsigned char qq[] = {
80, 78, 77, 76, 75, 75, 74, 73, 72, 71,
70, 69, 68, 68, 67, 66, 65, 65, 64, 63,
63, 62, 61, 61, 60, 60, 59, 58, 58, 57,
57, 56, 56, 55, 55, 54, 54, 53, 53, 52,
52, 51, 51, 50, 50, 50, 49, 49, 48, 48,
47, 47, 47, 46, 46, 46, 45, 45, 45, 44,
44, 44, 43, 43, 43, 42, 42, 42, 41, 41,
41, 41, 40, 40, 40, 40, 39, 39, 39, 38,
38, 38, 38, 37, 37, 37, 37, 37, 36, 36,
36, 36, 35, 35, 35, 35, 35, 34, 34, 34,
34, 34, 33, 33, 33, 33, 33, 32, 32, 32,
32, 32, 32, 31, 31, 31, 31, 31, 31, 30,
30, 30, 30, 30, 30, 30, 29, 29, 29, 29,
29, 29, 28, 28, 28, 28, 28, 28, 28, 28,
27, 27, 27, 27, 27, 27, 27, 27, 26, 26,
26, 26, 26, 26, 26, 26, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20
};
//20-10
unsigned char pp[] = {
200, 199, 198, 197, 196, 196, 195, 194, 193, 193,
192, 191, 190, 190, 189, 188, 187, 187, 186, 185,
185, 184, 183, 183, 182, 181, 181, 180, 179, 179,
178, 177, 177, 176, 176, 175, 174, 174, 173, 173,
172, 171, 171, 170, 170, 169, 168, 168, 167, 167,
166, 166, 165, 165, 164, 163, 163, 162, 162, 161,
161, 160, 160, 159, 159, 158, 158, 157, 157, 156,
156, 155, 155, 154, 154, 153, 153, 152, 152, 151,
151, 151, 150, 150, 149, 149, 148, 148, 147, 147,
147, 146, 146, 145, 145, 144, 144, 144, 143, 143,
142, 142, 142, 141, 141, 140, 140, 140, 139, 139,
138, 138, 138, 137, 137, 136, 136, 136, 135, 135,
135, 134, 134, 134, 133, 133, 132, 132, 132, 131,
131, 131, 130, 130, 130, 129, 129, 129, 128, 128,
128, 127, 127, 127, 126, 126, 126, 125, 125, 125,
125, 124, 124, 124, 123, 123, 123, 122, 122, 122,
121, 121, 121, 121, 120, 120, 120, 119, 119, 119,
119, 118, 118, 118, 117, 117, 117, 117, 116, 116,
116, 116, 115, 115, 115, 114, 114, 114, 114, 113,
113, 113, 113, 112, 112, 112, 112, 111, 111, 111,
111, 110, 110, 110, 110, 109, 109, 109, 109, 108,
108, 108, 108, 107, 107, 107, 107, 107, 106, 106,
106, 106, 105, 105, 105, 105, 105, 104, 104, 104,
104, 103, 103, 103, 103, 103, 102, 102, 102, 102,
102, 101, 101, 101, 101, 101, 100, 100, 100, 100,
100
};
//10-6
unsigned char oo[] = {
100, 99, 98, 98, 97, 97, 96, 96, 95, 95,
94, 94, 93, 93, 92, 92, 91, 91, 90, 90,
90, 89, 89, 88, 88, 87, 87, 86, 86, 86,
85, 85, 84, 84, 84, 83, 83, 82, 82, 82,
81, 81, 81, 80, 80, 80, 79, 79, 78, 78,
77, 77, 76, 76, 75, 75, 74, 74, 74, 73,
73, 72, 72, 71, 71, 71, 70, 70, 69, 69,
68, 67, 66, 66, 65, 64, 63, 63, 62, 61,
61, 60, 60
};
//赤外線距離センサー(GP2Y0A21YK)の電圧から距離を求める。
//Voltage //電圧
int ir_len_i(int Voltage)
{
int ir_length; //長さ
if (Voltage >= 0 && Voltage <= 400 ) {
return (800);
}
if (Voltage >= 3120 && Voltage <= 5000) {
return (60);
}
if (Voltage >= 400 && Voltage <= 1300 ) { //80-20
ir_length = (int)qq[(Voltage - 400) >> 2];
ir_length = ir_length * 10;
} else if (Voltage >= 1300 && Voltage <= 2300 ) { //20-10
ir_length = (int)pp[(Voltage - 1300) >> 2];
} else if (Voltage >= 2300 && Voltage <= 3120 ) { //10-6
//ir_length = (int)oo[(Voltage - 2300) / 10];
ir_length = (int)oo[ ((Voltage - 2300)*205)>>11 ]; //10の割り算
}//end if
//戻り値
return (ir_length);
} //ir_le_i
//初期化
void setup() {
//i2cの初期化
pc.beginNS(9600);
}//setup
//メインループ
void loop() {
int sensorValue; //センサーの読み取り値
int Voltage; //電圧
int ir_length; //長さ
//アナログ値の読み込み
analogReadResolution(12); //adc 12bit mode
//sensorValue = analogRead(A3); // PA11 PIN5 031
sensorValue = analogRead(A9); // PB7 PIN1 031
//sensorValue = analogRead(A0); //UNO
//アナログ値を電圧に変換
Voltage = (sensorValue * 3300) >> 12; //stm32 (s*3300)/4096
//V <- S*4.882 UNO +-1mV
//Voltage = ((sensorValue * 8) + ( (sensorValue * 8) / 10 ) + ( (sensorValue * 2) / 100 )) / 10 + (sensorValue * 4);
//赤外線距離センサー(GP2Y0A21YK)の電圧から距離を求める。
ir_length = ir_len_i(Voltage);
//値の表示
pc.printNS("800");
pc.printNS(",");
//表示
int b = ir_length;
char data_read[8]; //バッファーの定義
data_read[3] = 0;
data_read[2] = '0' + ( b - (DIV10(b) * 10) ); // '0'+(b%10)
b = DIV10(b);
data_read[1] = '0' + ( b - (DIV10(b) * 10) ); // '0'+(b%10)
data_read[0] = '0' + DIV10(b); // '0'+(b/10)
//前の0を削る
// 012 add
// [123] data
int yy;
if (ir_length >= 100) {yy = 0;}
else if (ir_length >= 10) {yy = 1;}
else {yy = 2;}
pc.printNS( (char *)(data_read + yy) );
pc.printNS(",");
pc.printNS("0");
pc.printNS("\r\n");//リターン
delay(10);// 1/100秒待つ
}//loop