Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed SDFileSystem_
Diff: main.cpp
- Revision:
- 28:8bda44be0dd0
- Parent:
- 26:742b77a683af
- Child:
- 29:ddf05802b598
--- a/main.cpp Fri Sep 03 10:07:14 2021 +0000
+++ b/main.cpp Mon Sep 06 07:12:03 2021 +0000
@@ -1,6 +1,7 @@
#include "mbed.h"
#include "SDFileSystem.h"
#include <time.h>
+#include <errno.h>
/*
This basic example just shows how to read the ADC internal channels raw values.
@@ -64,8 +65,8 @@
unsigned long timecount = 0;
int oldcount = 0;
//uint8_t maxacc = 0;
-float max_g=0;
-float now_g=0;
+long max_g=0;
+long now_g=0;
int update=0;
char buffer1[512] = {};
char buffer2[512] = {};
@@ -74,13 +75,9 @@
char filename1[32]= {};
char filename2[32]= {};
int ret = 0,init=1;
-//int timer_flag = 0;
/*
void timer(){
- timer_flag = 1;
- timecount++;
- oldcount++;
}
*/
@@ -106,16 +103,22 @@
//az+= tz;
//as+= scr;
//cnt++;
- pc.printf("new x:%2.2f y:%2.2f,z:%2.2f scaler = %2.2f \r\n",(float)tx*0.005,(float)ty*0.005,(float)tz*0.005,(float)scr*0.005);
+ pc.printf("time(%d)new x:%2.2f y:%2.2f,z:%2.2f scaler = %2.2f \r\n",time(NULL),(float)tx*0.005,(float)ty*0.005,(float)tz*0.005,(float)scr*0.005);
//pc.printf("new x:%d y:%d,z:%d scaler = %d \r\n",tx,ty,tz,scr);
if ( max_g < scr) {
max_g = scr;
update=1;
- //pc.printf("x:%d y:%d,z:%d scaler = %d max g=%2.2fG \r\n",tx,ty,tz,scr, (float)scr/11.0f);
- //pc.printf("new x:%d y:%d,z:%d scaler = %d max g=%2.2fG \r\n",tx,ty,tz,scr, (float)max_g/11.0f);
+ pc.printf("%max g=%d \r\n",max_g);
//Save EEPROM
- writeEEPROMByte(0, max_g);
+ //writeEEPROMByte(0, max_g);
+ for(int i = 0; i < 4; i++){
+ pc.printf("writerom 0x%x\r\n",(max_g >> 8*i) & 0xFF);
+ writeEEPROMByte(i, (max_g >> 8*i) & 0xFF);
+ }
+ //for(int i = 0; i < 4; i++){
+ //pc.printf("readEEPROM:0x%x\r\n",readEEPROMByte(i) << 8*i);
+ //}
}
sprintf(stracc1,"%d,%2.2f,%2.2f,%2.2f,%2.2f\r\n",timecount,(float)tx*0.005,(float)ty*0.005,(float)tz*0.005,(float)scr*0.005);
@@ -124,15 +127,13 @@
//pc.printf("buffer over. write file2\r\n");
ret = fprintf(fp1,"%s",buffer1);
- if(ret == 0){
- pc.printf("fp1 write failed. Reboot!!!\r\n");
+ if(ret <= 0){
+ pc.printf("fp1 write failed:%d. errno:%d. Reboot!!!\r\n",ret,errno);
NVIC_SystemReset();
}
-
memset(buffer1,0,sizeof(buffer1));
sprintf(buffer1,"%s",stracc1);
-
} else {
sprintf(buffer1,"%s%s",buffer1,stracc1);
}
@@ -147,8 +148,8 @@
if(strlen(buffer2)+ strlen(stracc2)> 512) {
ret = fprintf(fp2,"%s",buffer2);
- if(ret == 0){
- pc.printf("fp2 write failed. Reboot!!!\r\n");
+ if(ret <= 0){
+ pc.printf("fp2 write failed:%d. errno:%d. Reboot!!!\r\n",ret,errno);
NVIC_SystemReset();
}
@@ -163,48 +164,51 @@
if(time(NULL) > 86400){
//Create New File
set_time(NULL);
+ //pc.printf("time over:%d\r\n",time(NULL));
timecount = 0;
fclose(fp1);
+ fp1 = NULL;
fclose(fp2);
+ fp2 = NULL;
lognum1++;
lognum2++;
sprintf(filename1,"/sd/new_%d",lognum1);
sprintf(filename2,"/sd/old_%d",lognum2);
fp1 = fopen(filename1,"a");
if(!fp1){
- pc.printf("fp1 create file failed\r\n");
+ pc.printf("fp1 create file failed:%d\r\n",errno);
NVIC_SystemReset();
}
fp2 = fopen(filename2,"a");
if(!fp2){
- pc.printf("fp2 create file failed\r\n");
+ pc.printf("fp2 create file failed:%d\r\n",errno);
NVIC_SystemReset();
}
}
-
-
- //timer_flag = 0;
}
int main()
{
- //h3dacc.init(H3LIS331DL_ODR_50Hz, H3LIS331DL_NORMAL,H3LIS331DL_FULLSCALE_8);//これで初期化している FULLSCALE_8=400G
- //h3dacc.setHPFMode(H3LIS331DL_HPM_NORMAL_MODE_RES);//High Pass Filter ON
- //h3dacc.setHPFCutOFF(H3LIS331DL_HPFCF_1);
-
sprintf(filename1,"/sd/new_%d",lognum1);
sprintf(filename2,"/sd/old_%d",lognum2);
fp1 = fopen(filename1,"a");
if(!fp1) {
- pc.printf("fp1(%s) open failed\r\n",filename1);
+ pc.printf("fp1(%s) open failed errno:%d\r\n",filename1,errno);
//NVIC_SystemReset();
}
fp2 = fopen(filename2,"a");
if(!fp2) {
- pc.printf("fp2(%s) open failed\r\n",filename2);
+ pc.printf("fp2(%s) open failed errno:%d\r\n",filename2,errno);
//NVIC_SystemReset();
}
+
+ //Read Max G
+ for(int i = 0; i < 4; i++){
+ //pc.printf("readEEPROM:0x%x\r\n",readEEPROMByte(i));
+ max_g |= readEEPROMByte(i) << 8*i;
+ }
+ pc.printf("read max_g from eeprom:%d\r\n",max_g);
while(init) {
init = initLIS331();
@@ -215,12 +219,10 @@
//interrupt.attach_us(&timer,20000);//20ms
while(1)
{
- //if(timer_flag){
- sub();
- timecount++;
- oldcount++;
- //}
- wait_ms(200);
+ sub();
+ timecount++;
+ oldcount++;
+ wait_ms(20);
}
}