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.
Fork of SPI_HelloWorld_Mbed by
Revision 3:3da5bf0df3ab, committed 2017-06-20
- Comitter:
- ODEM
- Date:
- Tue Jun 20 07:46:04 2017 +0000
- Parent:
- 2:29e536aabd5b
- Commit message:
- I2C MPL3115A2
Changed in this revision
MPL3115A2.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPL3115A2.lib Tue Jun 20 07:46:04 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/mmotor/code/MPL3115A2/#c0ee6755d57f
--- a/main.cpp Wed Jun 14 13:49:06 2017 +0000 +++ b/main.cpp Tue Jun 20 07:46:04 2017 +0000 @@ -1,91 +1,54 @@ -// Read from I2C slave at address 0x62 - #include "mbed.h" - -I2C i2c(p9, p10); -int SlaveAddress = 0xc0; -//set to Altimeter -int SetA1 = 0x26; -int SetA2 = 0xB8; -//enable data flag -int flag1 = 0x13; -int flag2 = 0x07; -//Set Active -int active1 = 0x26; -int active2 = 0xB9; -//read status register -int status1 = 0x00; - - -// -int OUT_P_MSB=0; -char data_OUT_P_MSB[1]; -char STA; - - - -/******************************main**********************************/ -int main() -{ - char data_OUT_P_MSB[1]; - data_OUT_P_MSB[0] = 0x01; - - //cmd = 0x60; - - char data[2]; - data[0]=SetA1; - data[1]=SetA2; - - char flags[2]; - flags[0]=flag1; - flags[1]=flag2; - - char active[2]; - active[0]=active1; - active[1]=active2; +#include "MPL3115A2.h" + +I2C i2c(p9, p10); // sda, scl +Serial pc(USBTX, USBRX); // tx, rx +MPL3115A2 sensor(&i2c, &pc); + +DigitalOut myled(LED1); +DigitalOut powerPin(p21); - char status [1]; - status[0]=status1; - char cmd[2]; - const int addr = 0xc1; - - // wait(0.1); - //WRITE - //set to Altimeter - i2c.write(SlaveAddress, data, 2); - //enable data flag - i2c.write(SlaveAddress, flags, 2); - //Set Active - i2c.write(SlaveAddress, active, 2); - - while(1) { - - /*//READ - //read status register - STA=i2c.read(SlaveAddress, status, 2,false); - - - if (STA&&0x08==1) { - OUT_P_MSB=i2c.read(SlaveAddress, data_OUT_P_MSB, 1); - } - - - printf("data= %04X\n", OUT_P_MSB); - printf("data= %d\n", STA);*/ +int main() { + + powerPin = 1; + wait_ms(300); + + pc.printf("** MPL3115A2 SENSOR **\r\n"); + + sensor.init(); + + pc.printf("Who Am I: 0x%X\r\n", sensor.whoAmI()); + + Altitude a; + Temperature t; + Pressure p; + + // Offsets for Dacula, GA + sensor.setOffsetAltitude(83); + sensor.setOffsetTemperature(20); + sensor.setOffsetPressure(-32); + + while(1) + { + sensor.readAltitude(&a); + sensor.readTemperature(&t); - cmd[0] = 0x01; - cmd[1] = 0x00; - i2c.write(addr, cmd, 2); + sensor.setModeStandby(); + sensor.setModeBarometer(); + sensor.setModeActive(); + sensor.readPressure(&p); + + pc.printf("Altitude: %sm, Temp: %s°C, Pressure: %sPa\r\n", a.print(), t.print(), p.print()); + pc.printf("OFF_H: 0x%X, OFF_T: 0x%X, OFF_P: 0x%X\r\n", sensor.offsetAltitude(), sensor.offsetTemperature(), sensor.offsetPressure()); + + myled = 1; + wait(5); + myled = 0; + wait(5); - wait(0.5); - cmd[0] = 0x00; - i2c.write(addr, cmd, 1); - i2c.read(addr, cmd, 2); - printf("Temp = %d\n", cmd); - float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0); - printf("Temp = %.2f\n", tmp); + sensor.setModeStandby(); + sensor.setModeAltimeter(); + sensor.setModeActive(); + } -} - - - +} \ No newline at end of file