read pressure and temperature from LPS25h.

Dependencies:   mbed

Committer:
onaka
Date:
Sun May 10 07:00:39 2015 +0000
Revision:
3:844fb47ba7a2
Parent:
1:813b3c3eb644
revision 3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
onaka 0:955628fdbf95 1 #include "mbed.h"
onaka 0:955628fdbf95 2 #include "LPS25H.h"
onaka 0:955628fdbf95 3
onaka 3:844fb47ba7a2 4 #define FREQUENCY 0.10
onaka 3:844fb47ba7a2 5
onaka 0:955628fdbf95 6 I2C i2c(I2C_SDA, I2C_SCL);
onaka 0:955628fdbf95 7
onaka 0:955628fdbf95 8 DigitalOut myled(LED1);
onaka 3:844fb47ba7a2 9 DigitalIn mybutton(USER_BUTTON);
onaka 0:955628fdbf95 10
onaka 0:955628fdbf95 11 Serial pc(SERIAL_TX, SERIAL_RX);
onaka 0:955628fdbf95 12
onaka 0:955628fdbf95 13 LPS25H lps25h(i2c);
onaka 0:955628fdbf95 14
onaka 3:844fb47ba7a2 15 Ticker timer;
onaka 3:844fb47ba7a2 16 void Int_Timer();
onaka 3:844fb47ba7a2 17
onaka 3:844fb47ba7a2 18 double pres, temp, h, p0=0.0;
onaka 3:844fb47ba7a2 19 double t;
onaka 3:844fb47ba7a2 20
onaka 0:955628fdbf95 21 int main(){
onaka 3:844fb47ba7a2 22 //pc.printf("Start!\n");
onaka 3:844fb47ba7a2 23 pc.printf("time,pressure,tempreture,height\n");
onaka 0:955628fdbf95 24 // Set reference value
onaka 3:844fb47ba7a2 25 int i;
onaka 3:844fb47ba7a2 26 for(i=0; i<10; i++){
onaka 3:844fb47ba7a2 27 p0 += (double)lps25h.pressure()/4096.0;
onaka 3:844fb47ba7a2 28 wait(0.1);
onaka 0:955628fdbf95 29 }
onaka 3:844fb47ba7a2 30 p0/=10;
onaka 3:844fb47ba7a2 31
onaka 3:844fb47ba7a2 32 //(FREQUENCY)s間隔のタイマー割り込み
onaka 3:844fb47ba7a2 33 timer.attach(&Int_Timer, FREQUENCY);
onaka 3:844fb47ba7a2 34
onaka 3:844fb47ba7a2 35 t = 0.0;
onaka 3:844fb47ba7a2 36 //ボタンで終了
onaka 3:844fb47ba7a2 37 while(1){
onaka 3:844fb47ba7a2 38 if(mybutton==0){
onaka 3:844fb47ba7a2 39 timer.detach();
onaka 3:844fb47ba7a2 40 break;
onaka 3:844fb47ba7a2 41 }
onaka 3:844fb47ba7a2 42 myled=!myled;
onaka 3:844fb47ba7a2 43 wait(0.2);
onaka 3:844fb47ba7a2 44 }
onaka 3:844fb47ba7a2 45 //pc.printf("Finish!\n");
onaka 0:955628fdbf95 46 }
onaka 0:955628fdbf95 47
onaka 3:844fb47ba7a2 48 /** タイマー割り込み **/
onaka 3:844fb47ba7a2 49 void Int_Timer() {
onaka 3:844fb47ba7a2 50 //char buf[50];
onaka 3:844fb47ba7a2 51 pres = (double)lps25h.pressure()/4096.0;
onaka 3:844fb47ba7a2 52 temp = 42.5 + (double)lps25h.temperature()/480.0;
onaka 3:844fb47ba7a2 53 h = (pow((p0/pres),0.1902)-1.0)*(temp+273.15)/0.0065;
onaka 0:955628fdbf95 54
onaka 3:844fb47ba7a2 55 pc.printf("%.1f,%.2f,%.2f,%.2f\n", t, pres, temp, h);
onaka 3:844fb47ba7a2 56 //sprintf(buf, "%.2f,%.2f,%.2f,%.2f\n", t, pres, temp, h);
onaka 3:844fb47ba7a2 57 //fprintf(fp, buf);
onaka 3:844fb47ba7a2 58 //logger.puts(buf);
onaka 3:844fb47ba7a2 59 t += FREQUENCY;
onaka 3:844fb47ba7a2 60 }