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: Adafruit_GFX MS5803_14BA mbed
main.cpp
00001 /* 00002 MS5803_14BA with 0.96" OLED Display 00003 */ 00004 00005 #include "mbed.h" 00006 #include "Adafruit_SSD1306.h" 00007 #include "MS5803_14BA.h" 00008 00009 //#define DISPLAY_USES_SPI 00010 //#define DISPLAY_USES_I2C 00011 00012 #ifdef TARGET_K64F 00013 #define SPI_SCK PTD1 00014 #define SPI_MOSI PTD2 00015 #define SPI_MISO PTD3 00016 #define SPI_CS PTD0 00017 #define I2C_SDA PTE25 00018 #define I2C_SCL PTE24 00019 #define displayDC PTC4 00020 #define displayRST PTC3 00021 #elif defined(TARGET_KL25Z) 00022 #define SPI_SCK PTD1 00023 #define SPI_MOSI PTD2 00024 #define SPI_MISO PTD3 00025 #define SPI_CS PTD0 00026 #define I2C_SDA PTE0 00027 #define I2C_SCL PTE1 00028 #define displayDC PTD5 00029 #define displayRST PTA13 00030 #endif 00031 00032 #define descentSL (double) 23.0 // Descent speed limit in m/s 00033 #define liftSL (double) 9.0 // Lift speed limit in m/s 00034 00035 #define LED_ON 0 00036 #define LED_OFF 1 00037 00038 #ifdef DISPLAY_USES_SPI 00039 // an SPI sub-class that provides a constructed default 00040 class SPIPreInit : public SPI 00041 { 00042 public: 00043 SPIPreInit(PinName mosi, PinName miso, PinName clk) : SPI(mosi,miso,clk) 00044 { 00045 format(8, 3); 00046 frequency(100000); 00047 }; 00048 }; 00049 00050 SPIPreInit gSpi(SPI_MOSI, NC, SPI_SCK); 00051 Adafruit_SSD1306_Spi display(gSpi, displayDC, displayRST, SPI_CS, 64, 128); 00052 #elif defined(DISPLAY_USES_I2C) 00053 #define DISPLAY_I2C_ADDRESS 0x00 00054 00055 // an I2C sub-class that provides a constructed default 00056 class I2CPreInit : public I2C 00057 { 00058 public: 00059 I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) 00060 { 00061 frequency(400000); 00062 start(); 00063 }; 00064 }; 00065 00066 I2CPreInit gI2C(I2C_SDA, I2C_SCL); 00067 Adafruit_SSD1306_I2c display(gI2C, displayRST, DISPLAY_I2C_ADDRESS, 64, 128); 00068 #endif 00069 00070 Ticker ticker; 00071 char tmpstr[50]; 00072 Serial pc(USBTX, USBRX); 00073 MS5803_14BA sensor(I2C_SDA, I2C_SCL, 0x3B, D1_OSR_4096, D2_OSR_4096); // SDA, SCL, I2C_address (0x76 or 0x77 look at MS5803.h) 00074 bool volatile tick = false; 00075 uint8_t k; 00076 double p, p0 = 0.0, pp = 0.0, h = 0.0, hmax = 0.0, t = 20.0; 00077 DigitalOut greenLED(LED_GREEN); 00078 DigitalOut redLED(LED_RED); 00079 //DigitalOut blueLED(LED_BLUE); // blueLED is not usable because it is shared with the SPI0_SCK line! 00080 00081 void ticker_ISR() { 00082 tick = true; 00083 } 00084 00085 void setup() { 00086 greenLED = LED_OFF; 00087 redLED = LED_OFF; 00088 pc.baud(115200); 00089 sprintf(tmpstr, "MS5803 demo\n"); 00090 pc.printf(tmpstr); 00091 00092 sensor.reset(); 00093 00094 #if defined(DISPLAY_USES_SPI) || defined(DISPLAY_USES_I2C) 00095 display.printf(tmpstr); 00096 #endif 00097 00098 sprintf(tmpstr, "Calibrating...\r\n\r\n"); 00099 00100 #if defined(DISPLAY_USES_SPI) || defined(DISPLAY_USES_I2C) 00101 display.printf(tmpstr); 00102 display.display(); 00103 #endif 00104 00105 // gets the reference (surface) pressure as the mean of 10 initial measures 00106 for (k = 1; k <= 10; k++) { 00107 sensor.convert(); 00108 p0 = (p0*(k-1) + sensor.pressure)/k; 00109 } 00110 hmax = 0.0; 00111 00112 #if defined(DISPLAY_USES_SPI) || defined(DISPLAY_USES_I2C) 00113 display.clearDisplay(); 00114 // display.setTextSize((uint8_t) 2); 00115 #endif 00116 ticker.attach(&ticker_ISR, 0.1); 00117 } 00118 00119 int main() 00120 { 00121 setup(); 00122 00123 while(1) { 00124 while(!tick); // wait a timer interrupt 00125 tick = false; 00126 greenLED = LED_ON; 00127 00128 sensor.convert(); 00129 p = sensor.pressure; // pressure in mBar 00130 t = sensor.temperature; // temperature in degC 00131 h = (p - p0)/100; // depth in m 00132 hmax = (h > hmax) ? h : hmax; // max depth 00133 /* 00134 if ((p - pp)/60 > descentSL) 00135 redLED = LED_ON; 00136 else 00137 redLED = LED_OFF; 00138 00139 if ((p - pp)/60 < -liftSL) 00140 redLED = LED_ON; 00141 else 00142 redLED = LED_OFF; 00143 00144 pp = p; 00145 */ 00146 sprintf(tmpstr, "%.1f mBar/%.1f m\r\n%.1f C", p, h, t); 00147 pc.printf(tmpstr);//"%.3f\n", p); 00148 pc.printf("\n"); 00149 00150 #if defined(DISPLAY_USES_SPI) || defined(DISPLAY_USES_I2C) 00151 display.clearDisplay(); 00152 display.setTextCursor(0, 0); 00153 display.printf("%s", tmpstr); 00154 display.display(); 00155 #endif 00156 greenLED = LED_OFF; 00157 } 00158 }
Generated on Mon Jul 18 2022 16:40:24 by
