改良型。2017/9/6作動。

Dependencies:   SDFileSystem mbed

Fork of Power_meter_kai_TP2 by Atsumi Toda

Committer:
Joeatsumi
Date:
Wed Sep 06 07:26:35 2017 +0000
Revision:
5:131cbc4990e9
Parent:
4:5858bcd3dc60
????2017/9/6 16:26??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:bdbd3d6fc5d5 1 #include "mbed.h"
mbed_official 0:bdbd3d6fc5d5 2 #include "SDFileSystem.h"
mbed_official 0:bdbd3d6fc5d5 3
Joeatsumi 2:f8284afcee36 4 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
Joeatsumi 2:f8284afcee36 5 SDFileSystem sd(dp2,dp1,dp6,dp13,"sd");
Joeatsumi 2:f8284afcee36 6 Serial pc(dp16,dp15);
Joeatsumi 2:f8284afcee36 7 Timer Time ;
Joeatsumi 2:f8284afcee36 8 Ticker sd_write ;
Joeatsumi 2:f8284afcee36 9 DigitalOut myled1(LED1);
Joeatsumi 4:5858bcd3dc60 10 AnalogIn vin(dp9);
Joeatsumi 2:f8284afcee36 11 //Serial pc(dp16,dp15);
Joeatsumi 2:f8284afcee36 12
Joeatsumi 2:f8284afcee36 13
Joeatsumi 2:f8284afcee36 14 int count =0;//最初はTimeLog[0]から始まる
Joeatsumi 2:f8284afcee36 15
Joeatsumi 3:e7f77ec41f33 16 static unsigned long TimeLog[20];//10個の要素を「箱」を用意する
Joeatsumi 4:5858bcd3dc60 17
Joeatsumi 4:5858bcd3dc60 18 float VinLog[20];
Joeatsumi 4:5858bcd3dc60 19 float v; //mbedはfloat値として電圧を12bitで変換する。
Joeatsumi 4:5858bcd3dc60 20
Joeatsumi 2:f8284afcee36 21 unsigned long nowTime = 0;
Joeatsumi 4:5858bcd3dc60 22
Joeatsumi 4:5858bcd3dc60 23
Joeatsumi 2:f8284afcee36 24
Joeatsumi 2:f8284afcee36 25 void SD(){//この関数を0.05秒ごとに呼び出して、その度に経過時間をTimeLog[]に代入する。
Joeatsumi 2:f8284afcee36 26 //つまり、50ms毎の時間が配列に保存される。
Joeatsumi 2:f8284afcee36 27 //1000ms経過するとそれらの値をまとめてsdカードに保存する。
Joeatsumi 2:f8284afcee36 28
Joeatsumi 2:f8284afcee36 29 __disable_irq();//この関数を最優先に割り込みする。
Joeatsumi 2:f8284afcee36 30 myled1 = 0 ;
Joeatsumi 2:f8284afcee36 31
Joeatsumi 2:f8284afcee36 32 if(count<19){
Joeatsumi 2:f8284afcee36 33
Joeatsumi 2:f8284afcee36 34 TimeLog[count]=nowTime;
Joeatsumi 4:5858bcd3dc60 35 VinLog[count]=v;
Joeatsumi 4:5858bcd3dc60 36 pc.printf("Bridge=%.2f\r\n",VinLog[count]);
Joeatsumi 4:5858bcd3dc60 37
Joeatsumi 2:f8284afcee36 38 count++;
Joeatsumi 2:f8284afcee36 39
Joeatsumi 2:f8284afcee36 40 }else{
Joeatsumi 2:f8284afcee36 41
Joeatsumi 2:f8284afcee36 42 TimeLog[19]=nowTime;
Joeatsumi 4:5858bcd3dc60 43 VinLog[19]=v;
Joeatsumi 2:f8284afcee36 44
Joeatsumi 4:5858bcd3dc60 45 pc.printf("Bridge=%.2f\n",VinLog[count]);
mbed_official 0:bdbd3d6fc5d5 46
Joeatsumi 2:f8284afcee36 47 mkdir("/sd/mydir", 0777);
mbed_official 0:bdbd3d6fc5d5 48
Joeatsumi 2:f8284afcee36 49 FILE *fp = fopen("/sd/mydir/sdtest_kai.csv", "a");
Joeatsumi 2:f8284afcee36 50 if(fp == NULL) {
mbed_official 0:bdbd3d6fc5d5 51 error("Could not open file for write\n");
Joeatsumi 2:f8284afcee36 52 }
Joeatsumi 4:5858bcd3dc60 53 // fprintf(fp, "%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n",TimeLog[0],TimeLog[1],TimeLog[2],TimeLog[3],TimeLog[4],TimeLog[5],TimeLog[6],TimeLog[7],TimeLog[8],TimeLog[9]
Joeatsumi 4:5858bcd3dc60 54 // ,TimeLog[10],TimeLog[11],TimeLog[12],TimeLog[13],TimeLog[14],TimeLog[15],TimeLog[16],TimeLog[17],TimeLog[18],TimeLog[19]);
Joeatsumi 4:5858bcd3dc60 55
Joeatsumi 5:131cbc4990e9 56 fprintf(fp,"%d,%.2f\r\n%d,%.2f\r\n%d,%.2f\r\n",TimeLog[0],VinLog[0],TimeLog[1],VinLog[1],TimeLog[2],VinLog[2]);
Joeatsumi 4:5858bcd3dc60 57
Joeatsumi 2:f8284afcee36 58 myled1 = 1 ;
Joeatsumi 2:f8284afcee36 59 fclose(fp);
Joeatsumi 2:f8284afcee36 60 free(fp);
Joeatsumi 2:f8284afcee36 61
Joeatsumi 4:5858bcd3dc60 62 pc.printf("Noe logging\r\n");
Joeatsumi 2:f8284afcee36 63
Joeatsumi 2:f8284afcee36 64 count=0;//カウントをリセットする
Joeatsumi 2:f8284afcee36 65
Joeatsumi 2:f8284afcee36 66 }//if end
Joeatsumi 2:f8284afcee36 67
Joeatsumi 2:f8284afcee36 68 __enable_irq();
Joeatsumi 2:f8284afcee36 69 }
mbed_official 0:bdbd3d6fc5d5 70
Joeatsumi 2:f8284afcee36 71
Joeatsumi 2:f8284afcee36 72 int main(){
Joeatsumi 2:f8284afcee36 73 Time.start();
Joeatsumi 2:f8284afcee36 74 sd_write.attach(&SD,0.05);//0.05秒ごとにSDという関数を発動し、割り込みする。
Joeatsumi 3:e7f77ec41f33 75
Joeatsumi 2:f8284afcee36 76 while(1){
Joeatsumi 2:f8284afcee36 77 nowTime = Time.read_ms();
Joeatsumi 4:5858bcd3dc60 78 v=vin.read()*3.3;
Joeatsumi 2:f8284afcee36 79 }
Joeatsumi 2:f8284afcee36 80 }