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
main.cpp
- Committer:
- rary
- Date:
- 2020-05-25
- Revision:
- 1:851bc9589d81
- Parent:
- 0:f072b9d2936d
- Child:
- 2:2e35d9358d5e
File content as of revision 1:851bc9589d81:
#include "mbed.h"
LocalFileSystem local("local"); // マウントポイントを定義(ディレクトリパスになる)
Serial pc(USBTX, USBRX);
I2C i2c(p9, p10); //sda,scl
//LPS33HW slave address
#define S_add 0xB8
//Check adress
#define WHO_AM_I 0x0F
//CTRL_REG1
#define CTRL_REG1 0x10
//Start Up normal
#define Normal 0x10
//data register
#define press_msb 0x2A
#define press_lsb 0x29
#define press_xlsb 0x28
//define function
char cmd[2];
void i2c_write(int addr,char regist,char data)
{
cmd[0]=regist;
cmd[1]=data;
i2c.write(addr,cmd,2);
}
char i2c_read(int addr,char regist)
{
cmd[0]=regist;
i2c.write(addr,cmd,1);
i2c.read(addr,cmd,1);
return cmd[0];
}
//main program
int main(){
unsigned long ID, M, L, XL;
double press;
double P;
//Communication Check
ID = i2c_read(S_add, WHO_AM_I);
pc.printf("ID = 0x%x\r\n", ID);
//Start Up
i2c_write(S_add, CTRL_REG1, Normal);
int i = 0; //回数変数
double data[100]; //データ格納
while(i < 100)
{
//data read
M = i2c_read(S_add, press_msb);
L = i2c_read(S_add, press_lsb);
XL = i2c_read(S_add, press_xlsb);
press = (M << 16) | (L << 8) | XL;
wait(0.1);
P = press/4096;
pc.printf("P = %lf [hPa] \r\n", P);
data[i] = P;
i++;
}
//データ保存
pc.printf("Save start!\r\n");
FILE *fp;
fp = fopen("/local/pressure.csv", "w"); // ファイルを書き込みモードで開く
for(i = 0; i < 100; i++){
fprintf(fp, "%lf\n", data[i]);
wait(0.1);
}
fclose(fp);
pc.printf("finish!");
}