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_
main.cpp
- Committer:
- tnanbu
- Date:
- 2021-08-26
- Revision:
- 8:7f80139df48d
- Parent:
- 7:b69fa9bb320d
- Child:
- 11:2d5fcf102778
File content as of revision 8:7f80139df48d:
#include "mbed.h" #include "SDFileSystem.h" #include <time.h> //#include "mbed_rtc_time.h" /* This basic example just shows how to read the ADC internal channels raw values. Please look in the corresponding device reference manual for a complete description of how to make a temperature sensor, VBat or Vref measurement. */ AnalogIn adc_temp(ADC_TEMP); AnalogIn adc_vref(ADC_VREF); /* UD-GS2 H/W define PIO_SWin PB_4 PIO_wkup PA_4 PIO_enable PB_0 PIO_intout1 PB_2 PIO_led PB_5 PIO_v20v PC_13 PIO_v18v PH_0 PIO_intout2 PH_1 PIO_spics PB_12 PIO_battryMonEn PA_5 PB_7 SDA PB_6 SCL */ //DigitalOut hx_clk(PB_7); //DigitalIn hx_dt(PB_6); //I2C i2cacc(p_sda, p_scl) #include "H3LIS331DL.h" H3LIS331DL h3dacc(PB_7,PB_6); RawSerial pc(PA_9, PA_10,115200); //console UART //LowPowerTicker interrput; Ticker interrupt; SPI STSPI(PB_15, PB_14, PB_13); //mosi,miso,clk DigitalOut STSPICS(PB_12); SDFileSystem *sd = new SDFileSystem(PA_12, PA_11, PB_3, PA_15, "sd"); // mosi, miso, sclk, cs, name DigitalOut led(PB_5); //int gettimeofday(struct timeval *tv, void *tz); int initLIS3DH(); int read3axes(short *tx,short *ty,short *tz); //int readTemp(short *tmp); FILE *fp1,*fp2,*fp;//fp1:LIS3DH, fp2:H3LIS331DL int timecount = 0; //static uint8_t buffer[512] = {}; char buffer1[512] = {}; char buffer2[512] = {}; char buffer[512] = {}; char stracc[32] = {}; char stracc1[32] = {}; char stracc2[32] = {}; bool write1 = false,write2 = false,writeflag = false; void timer() { short x=0,y=0,z=0; float lis3dh_acc = 0; double xyz[3]={},h3lis331dl_acc=0; //char stracc[32] = {}; //H3LIS331DL h3dacc.getAcceleration(xyz); pc.printf("x:%lf,y:%lf,z:%lf\r\n",xyz[0],xyz[1],xyz[2]); //!!!If you read here, you cannot get acceleration!!! //h3lis331dl_acc = sqrt(xyz[0]*xyz[0] + xyz[1]*xyz[1] + xyz[2]*xyz[2]); //sprintf(stracc2,"%ld,%lf\r\n",time(NULL),h3lis331dl_acc); //pc.printf("%s",stracc2); //if(strlen(buffer)+strlen(stracc2) > 512){ //pc.printf("buffer over. write file2\r\n"); //writeflag = true; //} //else{ //sprintf(buffer,"%s%s",buffer,stracc2); //} //if(timecount == 10){ //pc.printf("calc lis3dh acc\r\n"); //LIS3DH /* read3axes(&x,&y,&z); lis3dh_acc = sqrt( (float)x/1024.0f * (float)x/1024 + (float)y/1024.0f * (float)y/1024 + (float)z/1024.0f * (float)z/1024); sprintf(stracc1,"%ld,%lf\r\n",time(NULL),lis3dh_acc); pc.printf("lis3dh:%s",stracc1); //if(strlen(buffer1)+ strlen(stracc1)> 512){ if(strlen(buffer)+ strlen(stracc1)> 512){ //pc.printf("buffer1 over. write file1\r\n"); //write1 = true; writeflag = true; } else{ sprintf(buffer1,"%s%s",buffer1,stracc1); } */ //timecount = 0; //} //timecount++; } int main() { int lognum1 = 0, lognum2 = 0,lognum=0; char filename[20] = {}; //char filename1[20]={}; //char filename2[20]={}; int ret = 0; double abc[3]={}; initLIS3DH(); h3dacc.init(H3LIS331DL_ODR_100Hz, H3LIS331DL_NORMAL,H3LIS331DL_FULLSCALE_8);//これで初期化している FULLSCALE_8=400G h3dacc.setHPFMode(H3LIS331DL_HPM_NORMAL_MODE_RES);//High Pass Filter ON //h3dacc.setHPFCutOFF(H3LIS331DL_HPFCF_1); //pc.printf("\nSTM32 ADC internal channels reading example\r\n"); //pc.printf("\nSTM32 3D acc sensor example\r\n"); //sprintf(filename,"/sd/acclog_%d",lognum); //sprintf(filename1,"/sd/lis3dh_%d",lognum1); //sprintf(filename2,"/sd/h3lis331dl_%d",lognum2); //sprintf(filename2,"/sd/h3lis_%d",lognum2); /* fp = fopen(filename,"a"); if(!fp){ pc.printf("fp open failed:%s\r\n",filename); } */ /* fp1 = fopen(filename1,"a"); if(!fp1){ pc.printf("fp1(%s) open failed\r\n",filename1); } fp2 = fopen(filename2,"a"); if(!fp2){ pc.printf("fp2(%s) open failed\r\n",filename2); } */ interrupt.attach_us(&timer,200000);//200ms while(1) { //On board ACC sensor //pc.printf("write1:%d,write2:%d\r\n",write1,write2); /* if(write1){ pc.printf("write1\r\n"); ret = fprintf(fp1,"%s",buffer1); //pc.printf("fp1 %d outputed\r\n",ret); pc.printf("write buffer1:%d\r\n",ret); memset(buffer1,0,sizeof(buffer1)); sprintf(buffer1,"%s",stracc1); write1 = false; } //else{ //pc.printf("%d,%d\r\n",strlen(buffer1),timecount); //} */ // Extra ACC sensor //h3dacc.getAcceleration(abc); //If you read here, you can get acceleration //pc.printf("new x:%lf,y:%lf,z:%lf\r\n",abc[0],abc[1],abc[2]); /* if(writeflag){ ret = fprintf(fp,"%s",buffer); pc.printf("write buffer:%d\r\n",ret); memset(buffer,0,sizeof(buffer)); sprintf(buffer,"%s",stracc2); writeflag = false; } else{ pc.printf("%d,%d\r\n",strlen(buffer),timecount); } */ led = !led; /* if(time(NULL) > 86400){ //Create New File set_time(NULL); fclose(fp1); fclose(fp2); lognum1++; lognum2++; sprintf(filename1,"/sd/lis3dh_%d",lognum1); sprintf(filename2,"/sd/h3lis331dl_%d",lognum2); fp1 = fopen(filename1,"a"); if(!fp1){ pc.printf("fp1") } fp2 = fopen(filename2,"a"); } */ wait_ms(200); //wait(1); } } /*********** porting **************/ void spiFormat(int b,int m) { STSPI.format(b,m); /* 8bit */ } void spiFrequency(int f){ STSPI.frequency(f); /* 1Mbps */ } void spiWriteCS(int cs) { STSPICS=cs; } int spiWrite(int wd) { return ( STSPI.write(wd)); } void error(const char* format, ...) { return; }