read pressure and temperature from LPS25h.

Dependencies:   mbed

Revision:
3:844fb47ba7a2
Parent:
1:813b3c3eb644
diff -r 0c2bb6fe6885 -r 844fb47ba7a2 main.cpp
--- a/main.cpp	Mon Apr 13 12:54:45 2015 +0000
+++ b/main.cpp	Sun May 10 07:00:39 2015 +0000
@@ -1,36 +1,60 @@
 #include "mbed.h"
 #include "LPS25H.h"
 
+#define FREQUENCY 0.10
+
 I2C i2c(I2C_SDA, I2C_SCL);
  
 DigitalOut myled(LED1);
+DigitalIn mybutton(USER_BUTTON);
  
 Serial pc(SERIAL_TX, SERIAL_RX);
 
 LPS25H lps25h(i2c);
 
+Ticker timer;
+void Int_Timer();
+
+double pres, temp, h, p0=0.0;
+double t;
+
 int main(){
- 
-    double p, t, h, p0;
-    
+    //pc.printf("Start!\n");
+    pc.printf("time,pressure,tempreture,height\n");
     // Set reference value
-    p0 = (double)lps25h.pressure()/4096.0;
-    pc.printf("Set p0 = %7.2fhPa\n", p0);
- 
-    while (1) {
-        // Read pressure & temperature
-        p = (double)lps25h.pressure()/4096.0;
-        t = 42.5 + (double)lps25h.temperature()/480.0;
- 
-        // Calculate height
-        h = (pow((p0/p),0.1902)-1.0)*(t+273.15)/0.0065;
- 
-        // Display result
-        pc.printf("height = %4.1fm, pressure = %7.2fhPa, temperature = %5.2fC\n", h, p, t);
-        myled = !myled;
-        wait(1.0);
+    int i;
+    for(i=0; i<10; i++){
+        p0 += (double)lps25h.pressure()/4096.0;
+        wait(0.1);
     }
- 
+    p0/=10;
+
+    //(FREQUENCY)s間隔のタイマー割り込み
+    timer.attach(&Int_Timer, FREQUENCY);
+    
+    t = 0.0;
+    //ボタンで終了
+    while(1){
+        if(mybutton==0){
+            timer.detach();
+            break;
+        }
+        myled=!myled;
+        wait(0.2);
+    } 
+    //pc.printf("Finish!\n");
 }
  
+/** タイマー割り込み **/
+void Int_Timer() {
+    //char buf[50];
+    pres = (double)lps25h.pressure()/4096.0;
+    temp = 42.5 + (double)lps25h.temperature()/480.0;
+    h = (pow((p0/pres),0.1902)-1.0)*(temp+273.15)/0.0065;
 
+    pc.printf("%.1f,%.2f,%.2f,%.2f\n", t, pres, temp, h);
+    //sprintf(buf, "%.2f,%.2f,%.2f,%.2f\n", t, pres, temp, h);
+    //fprintf(fp, buf);
+    //logger.puts(buf);
+    t += FREQUENCY;
+}