Created
July 23, 2017 02:19
-
-
Save anonymous/e978ca1af0d4cff3dfca839f87311778 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//RXduinoでアナログ電力計測試行 | |
#include <rxduino.h> | |
//割り込み関数 | |
extern void read(void); | |
void setup() | |
{ | |
//シリアル通信 | |
Serial.begin(38400); //USBシリアルを使う | |
Serial.setDefault(); //printfにシリアルを割り当て | |
double power=10.0; | |
Serial.println(power,2); //電力値送信のテスト。 | |
//LED出力 | |
pinMode(PIN_LED0,OUTPUT); | |
pinMode(PIN_LED1,OUTPUT); | |
pinMode(PIN_LED2,OUTPUT); | |
pinMode(PIN_LED3,OUTPUT); | |
digitalWrite(PIN_LED0, 1); | |
digitalWrite(PIN_LED1, 1); | |
digitalWrite(PIN_LED2, 1); | |
digitalWrite(PIN_LED3, 1); | |
//ADC | |
analogReference(RAW12BIT); //0-3.3Vを0-4096で返す?合ってるか微妙。 | |
//割り込み | |
timer_regist_userfunc( read ); //1msごとにタイマ割り込みを実行 | |
} | |
int value[6][100]; //計測結果 | |
int count = 0; //1ms割り込み回数のカウント | |
int statusLED1 = 0; //LED1の状態 | |
int measured = 0; //100msの計測完了フラグ | |
void loop() | |
{ | |
int i,ch; //ループ変数 | |
while(1) | |
{ | |
//計測完了したらデータ処理開始 | |
if(measured == 1) | |
{ | |
digitalWrite(PIN_LED0, 1); | |
measured = 0; | |
float sum[6]={0., 0., 0., 0., 0., 0.}; //2乗和 | |
//データ処理(半波電流のRMS値^2算出)して送信 | |
for(ch=0;ch<6;ch++){ | |
for(i=0; i<100; i++) | |
{ | |
sum[ch] += value[ch][i]*value[ch][i]; | |
} | |
sum[ch] /= 100.; | |
sum[ch] /= (4096./3.3*4096./3.3); //ADCの変換係数 | |
sum[ch] *= 2.; | |
} | |
printf("%f, %f, %f, %f, %f, %f\r\n", sum[0], sum[1], sum[2], sum[3], sum[4], sum[5]); | |
digitalWrite(PIN_LED0, 0); | |
} | |
} | |
} | |
//1msごとに実行される | |
void read(void) | |
{ | |
digitalWrite(PIN_LED2, 1); | |
//1secに1回100サンプル=100msデータ=5サイクル分取得 | |
if(count<100){ | |
value[0][count] = analogRead(0); | |
value[1][count] = analogRead(1); | |
value[2][count] = analogRead(2); | |
value[3][count] = analogRead(3); | |
value[4][count] = analogRead(4); | |
value[5][count] = analogRead(5); | |
}else if(count==100){ | |
measured = 1; | |
} | |
if(count++>=1000){ | |
count=0; | |
if(statusLED1==0){ | |
digitalWrite(PIN_LED1, 1); | |
statusLED1 = 1; | |
}else{ | |
digitalWrite(PIN_LED1, 0); | |
statusLED1 = 0; | |
} | |
} | |
digitalWrite(PIN_LED2, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment