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.
Dependents: LED_Demo LED_Demo2 LED_Demo
Fork of BMP180 by
MAX44000.cpp
00001 /******************************************************************************* 00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00020 * OTHER DEALINGS IN THE SOFTWARE. 00021 * 00022 * Except as contained in this notice, the name of Maxim Integrated 00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated 00024 * Products, Inc. Branding Policy. 00025 * 00026 * The mere transfer of this software does not imply any licenses 00027 * of trade secrets, proprietary technology, copyrights, patents, 00028 * trademarks, maskwork rights, or any other form of intellectual 00029 * property whatsoever. Maxim Integrated Products, Inc. retains all 00030 * ownership rights. 00031 ******************************************************************************* 00032 */ 00033 00034 #include "MAX44000.h" 00035 00036 /***** Definitions *****/ 00037 #define I2C_ADDR (0x94) // 1001_010x 00038 00039 //****************************************************************************** 00040 MAX44000::MAX44000(PinName sda, PinName scl) 00041 { 00042 i2c_ = new I2C(sda, scl); 00043 i2c_owner = true; 00044 00045 i2c_->frequency(400000); 00046 } 00047 00048 //****************************************************************************** 00049 MAX44000::MAX44000(I2C *i2c) : 00050 i2c_(i2c) 00051 { 00052 i2c_owner = false; 00053 } 00054 00055 //****************************************************************************** 00056 MAX44000::~MAX44000() 00057 { 00058 if(i2c_owner) { 00059 delete i2c_; 00060 } 00061 } 00062 00063 //****************************************************************************** 00064 int MAX44000::init(MAX44000::modes_t mode, MAX44000::alstim_t alstim, MAX44000::alspga_t alspga, MAX44000::drive_t drive) 00065 { 00066 char data[2]; 00067 00068 data[0] = REG_RX_CONFIG; 00069 data[1] = 0xF0 | ((alstim & 0x03)<<2) | (alspga & 0x03); 00070 if (i2c_->write(I2C_ADDR, data, 2) != 0) { 00071 return -1; 00072 } 00073 00074 data[0] = REG_TX_CONFIG; 00075 data[1] = (drive & 0x0F); 00076 if (i2c_->write(I2C_ADDR, data, 2) != 0) { 00077 return -1; 00078 } 00079 00080 data[0] = REG_MAIN_CONFIG; 00081 data[1] = ((mode & 0x07)<<2) | (1<<5); // Set bit 5 to use factory trim data 00082 if (i2c_->write(I2C_ADDR, data, 2) != 0) { 00083 return -1; 00084 } 00085 00086 return 0; 00087 } 00088 00089 //****************************************************************************** 00090 int MAX44000::writeReg(MAX44000::registers_t reg_addr, char reg_data) 00091 { 00092 char data[2]; 00093 00094 data[0] = reg_addr; 00095 data[1] = reg_data; 00096 if (i2c_->write(I2C_ADDR, data, 2) != 0) { 00097 return -1; 00098 } 00099 00100 return 0; 00101 } 00102 00103 //****************************************************************************** 00104 int MAX44000::readReg(MAX44000::registers_t reg_addr) 00105 { 00106 char data; 00107 00108 data = reg_addr; 00109 if (i2c_->write(I2C_ADDR, &data, 1, true) != 0) { 00110 return -1; 00111 } 00112 00113 if (i2c_->read(I2C_ADDR, &data, 1) != 0) { 00114 return -1; 00115 } 00116 00117 return (0x0 + data); 00118 } 00119 00120 //****************************************************************************** 00121 int MAX44000::readALS(void) 00122 { 00123 char data; 00124 int alsData; 00125 00126 data = REG_ALS_DATA_HIGH; 00127 if (i2c_->write(I2C_ADDR, &data, 1, true) != 0) { 00128 return -1; 00129 } 00130 00131 if (i2c_->read(I2C_ADDR, &data, 1, true) != 0) { // must use repeated start to protect low byte data 00132 return -1; 00133 } 00134 00135 if (data & 0x40) { // if the overflow bit is set 00136 return -1; 00137 } 00138 00139 alsData = (data << 8); 00140 data = REG_ALS_DATA_LOW; 00141 if (i2c_->write(I2C_ADDR, &data, 1, true) != 0) { 00142 return -1; 00143 } 00144 00145 if (i2c_->read(I2C_ADDR, &data, 1) != 0) { 00146 return -1; 00147 } 00148 00149 alsData += data; 00150 return alsData; 00151 }
Generated on Thu Jul 21 2022 14:13:30 by
1.7.2

Ambient Light and Proximity Sensor MAX44000