Wim Huiskamp / Mbed 2 deprecated mbed_HP03SA_LM77

Dependencies:   HP03SA LM77 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed HP03SA I2C Pressure and Temperature sensor Test
00002  *      LM77 Temperature sensor Test
00003  *
00004  * Copyright (c) 2015 Wim Huiskamp
00005  * Released under the MIT License: http://mbed.org/license/mit
00006  *
00007  * version 0.1 Initial Release
00008  */
00009 #define PCF8593_TST            0
00010 #define LM77_TST               1
00011 #define HP03SA_TST             1
00012 
00013 #include "mbed.h"
00014 
00015 #if (HP03SA_TST == 1)
00016 #include "HP03SA.h"
00017 #endif
00018 
00019 #if (LM77_TST == 1)
00020 #include "LM77.h"
00021 #endif
00022  
00023 //Pin Defines for I2C Bus
00024 #define D_SDA                  p9
00025 #define D_SCL                  p10
00026 //#define D_SDA                  p28
00027 //#define D_SCL                  p27
00028 I2C i2c(D_SDA, D_SCL);
00029 
00030 //Pin Defines for XCLR
00031 #define D_XCLR                 p11  
00032 
00033 //Host PC Baudrate (Virtual Com Port on USB)
00034 #define D_BAUDRATE            9600
00035 //#define D_BAUDRATE            57600
00036 
00037 // mbed Interface Hardware definitions
00038 DigitalOut myled1(LED1);
00039 DigitalOut myled2(LED2);
00040 DigitalOut myled3(LED3);
00041 DigitalOut heartbeatLED(LED4);
00042 
00043 // Host PC Communication channels
00044 Serial pc(USBTX, USBRX); // tx, rx
00045 
00046 #if (HP03SA_TST == 1)
00047 // Generate the 32kHz master clock for HP03SA
00048 PwmOut mclk(p21);
00049 
00050 // Instantiate HP03SA
00051 HP03SA hp03sa(&i2c, D_XCLR); // I2C bus, XCLK Pin
00052 #endif
00053 
00054 #if (LM77_TST == 1)
00055 // Instantiate LM77
00056 LM77 lm77(&i2c, LM77_SA0); // I2C bus, XCLK Pin
00057 #endif
00058 
00059 int main() {
00060   pc.baud(D_BAUDRATE);
00061   pc.printf("\r\n\r\n");  
00062   pc.printf("Hello world\r\n");
00063 
00064 #if (PCF8593_TST == 1)
00065   char buffer[8];
00066   char status;
00067 
00068   //Init PCF8593 RTC
00069   buffer[0] = 0x00; //control reg
00070   buffer[1] = 0x00; //control reg value (32Khz osc)
00071   status = i2c.write(0xA2, buffer, 2);
00072 
00073   if (status == 0) {
00074     pc.printf("RTC status = Ok\n\r");
00075   }
00076   else {
00077     pc.printf("RTC status = Not Ok\n\r");      
00078   }  
00079 #endif
00080 
00081 #if (LM77_TST == 1)
00082   if (lm77.getStatus()) {
00083     pc.printf("LM77 status = Ok\n\r");   
00084 
00085     pc.printf("Critical Alert temperature from LM77 is %.1f [C]\r\n", lm77.getCritAlertTemp());     
00086     pc.printf("Low Alert temperature from LM77 is %.1f [C]\r\n", lm77.getLowAlertTemp());     
00087     pc.printf("High Alert temperature from LM77 is %.1f [C]\r\n", lm77.getHighAlertTemp());             
00088     pc.printf("Alert Hysteresis from LM77 is %.1f [C]\r\n", lm77.getAlertHyst());  
00089     wait(1.0);
00090 
00091 #if(0)
00092 #define CA          95.0f     
00093 #define LA           8.0f     
00094 #define HA          15.0f     
00095 #define HY           3.0f     
00096     lm77.setCritAlertTemp(CA);     
00097     lm77.setLowAlertTemp(LA);         
00098     lm77.setHighAlertTemp(HA);
00099     lm77.setAlertHyst(HY);             
00100     pc.printf("Critical Alert temperature set at %.1f [C]. Read from LM77 is %.1f [°C]\r\n", CA, lm77.getCritAlertTemp());     
00101     pc.printf("Low Alert temperature set at %.1f [C]. Read from LM77 is %.1f [°C]\r\n", LA, lm77.getLowAlertTemp());     
00102     pc.printf("High Alert temperature set at %.1f [C]. Read from LM77 is %.1f [°C]\r\n", HA, lm77.getHighAlertTemp());             
00103     pc.printf("Alert Hysteresis set at %.1f [C]. Read from LM77 is %.1f [°C]\r\n", HY, lm77.getAlertHyst());  
00104 #endif                      
00105   }
00106   else {
00107     pc.printf("LM77 status = not Ok\n\r");            
00108   } 
00109 #endif
00110 
00111 #if (HP03SA_TST == 1)
00112   //Init MCLK
00113   mclk.period_us(30);       //32768 KHz
00114   mclk.pulsewidth_us(15);   //32768 KHz
00115     
00116   if (hp03sa.getStatus()) {
00117     pc.printf("HP03S status = Ok\n\r");    
00118   }
00119   else {
00120     pc.printf("HP03S status = not Ok\n\r");            
00121   }
00122   
00123   // Take reading from sensor before setting calibration value!
00124   hp03sa.sample();
00125 
00126   // Set QNH pressure. The Altitude reading will be 0 for the set pressure.
00127 //    hp03sa.setPressure(10247);  // MSL pressure at time of testing 1024.7 hPa
00128 //                                // Results in getAltitude() = 6m because it matches local MSL pressure
00129     
00130   // Set QNH altitude. The Altitude reading will be the set value at current pressure.
00131   hp03sa.setAltitude(6);          // About right where I live..
00132 #endif
00133   
00134   while(1) {
00135 #if (HP03SA_TST == 1)    
00136     // Take reading from sensor            
00137     hp03sa.sample();
00138     
00139     // Show Pressure    
00140     pc.printf("Absolute atmospheric pressure is %.1f [hPa]\r\n", (float) hp03sa.getAbsPressure() / 10.0f);
00141     pc.printf("Sealevel atmospheric pressure is %.1f [hPa]\r\n", (float) hp03sa.getSeaPressure(6) / 10.0f); // About right where I live..
00142     
00143     // Show Temperature
00144 //    pc.printf("Ambient temperature is %.1f [°C]\r\n", (float) hp03sa.getTemperatureInt() / 10.0f);       
00145     pc.printf("Ambient temperature is %.1f [C]\r\n", hp03sa.getTemperature());       
00146     pc.printf("Ambient temperature is %.1f [F]\r\n", hp03sa.celsiusToFahrenheit(hp03sa.getTemperature()) );       
00147         
00148     // Show Altitude
00149     pc.printf("Altitude estimated at %d [m]\r\n", hp03sa.getAltitude());       
00150 
00151     // Show Altitude Ft
00152     pc.printf("Altitude estimated at %d [ft]\r\n", hp03sa.getAltitudeFt());       
00153 #endif
00154 
00155 #if (LM77_TST == 1)
00156     // Show Temperature LM77
00157 //    pc.printf("Ambient temperature from LM77 is %.1f [°C]\r\n", (float) lm77.getTemperatureInt() / 10.0f);       
00158     pc.printf("Ambient temperature from LM77 is %.1f [C]\r\n", lm77.getTemperature());     
00159 #endif
00160 
00161     wait(1.0);
00162     myled1 = 1;
00163     wait(0.2);    
00164     myled1 = 0;    
00165   }
00166 }