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: SX1276GenericLib USBDevice
Fork of NonPingPong_PICO_LoRa by
MAX44009_lib.cpp
00001 00002 #include "mbed.h" 00003 #include "MAX44009_lib.h" 00004 00005 // ***************************************************************************** 00006 // MAX44009_write_register(char, char, char) writes single byte to MAX44009 00007 // char I2C address 00008 // char MAX44009 register address 00009 // char data byte to be writen 00010 // returns 0 on success ACK, 1 on NACK 00011 // ***************************************************************************** 00012 00013 int MAX44009_write_register(I2C *i2c, char I2C_add, char reg_add, char byte){ 00014 char data[2]; // char type ranges from 0 to 255 (8 bytes) 00015 int error; 00016 data[0] = reg_add; 00017 data[1] = byte; 00018 error = i2c->write(I2C_add,data,2); // why send 2 bytes? 00019 //if(DEBUG)db.printf("wr[%02X %02X %d]\r\n", data[0], data[1], error); 00020 return error; 00021 00022 } 00023 00024 /// **************************************************************************** 00025 // MAX44009_write_register(char, char, char *, int) writes multiple bytes to MAX44009 00026 // char I2C address 00027 // char MAX44009 register address 00028 // char * data vector of bytes to be written 00029 // int number of bytes to write 00030 // returns 0 on success ACK, 1 on NACK 00031 // ***************************************************************************** 00032 00033 int MAX44009_write_register(I2C *i2c, char I2C_add, char reg_add, char *bytes, int n){ 00034 int i; 00035 //set start address 00036 char data[16]; 00037 int error; 00038 data[0] = reg_add; 00039 for(i=1;i<=n;i++){ 00040 data[i] = bytes[i-1]; 00041 } 00042 error = i2c->write(I2C_add,data,n+1); // send n bytes of data 00043 00044 return error; 00045 } 00046 00047 // ***************************************************************************** 00048 // MAX44009_read_register(char, char, char *) reads single byte from MAX44009 00049 // char I2C address 00050 // char MAX44009 register address 00051 // char * data vector for read bytes to be stored in 00052 // returns 0 on success, 1 on fail 00053 // ***************************************************************************** 00054 00055 int MAX44009_read_register(I2C *i2c, char I2C_add, char reg_add, char *bytes){ 00056 int error; 00057 error = i2c->write(I2C_add,®_add,1,1); 00058 if(error)return error; 00059 error = i2c->read(I2C_add,bytes,1); 00060 //if(DEBUG)db.printf("rr e[%d]\r\n",error); 00061 return error; 00062 } 00063 00064 00065 // ***************************************************************************** 00066 // MAX44009_read_lux_register(char, char, char *, int) reads lux value bytes from MAX44009 00067 // char I2C address 00068 // char MAX44009 register address 00069 // char * data vector for read bytes to be stored in 00070 // returns 0 on success, 1 on fail 00071 // ***************************************************************************** 00072 00073 int MAX44009_read_lux_register(I2C *i2c, char I2C_add, char reg_add, char *bytes){ 00074 int error; 00075 00076 error = i2c->write(I2C_add, ®_add, 1, 1); 00077 if(error)return error; 00078 error = i2c->read(I2C_add, bytes, 1, true); 00079 if(error)return error; 00080 00081 reg_add += 1; // increment register address 00082 error = i2c->write(I2C_add, ®_add, 1, 1); //? 00083 if(error)return error; 00084 error = i2c->read(I2C_add, bytes + 1, 1); // bytes + 1; &bytes[1] 00085 00086 //if(DEBUG)db.printf("rr e[%d]\r\n",error); 00087 return error; 00088 } 00089 00090 00091 // ***************************************************************************** 00092 // convert_temperature(char) sends convert command to MAX44009 device 00093 // char I2C address 00094 // ***************************************************************************** 00095 00096 //void convert_temperature(I2C *i2c, char I2C_add){ // set convert bit to start conversion 00097 // 00098 // char data[2]; 00099 // 00100 // //read ADC_SETUP register 0x14 00101 // MAX44009_read_register(i2c, I2C_add, MAX44009_ADC_SETUP,data,1); 00102 // 00103 // //mask convert register value with 0x01 and write back register 0x14 00104 // MAX44009_write_register(i2c, I2C_add, MAX44009_ADC_SETUP, data[0]|0x01); 00105 //} 00106 00107 //****************************************************************************** 00108 // get_luxvalue(char) read lux value from MAX44009 device register 00109 // char I2C address 00110 // returns LuxResponse luxValue = light intensity in lux 00111 // status = register read result 00112 //****************************************************************************** 00113 00114 LuxResponse get_luxvalue(I2C *i2c, char I2C_add){ 00115 char data[2]; // 2 bytes of raw Lux Register 00116 double lux; 00117 // int count; 00118 00119 // Read lux value, 2 bytes 00120 int error = MAX44009_read_lux_register(i2c, I2C_add, MAX44009_LUX_HI, data); 00121 00122 //calculate lux from data 00123 // count = (int)(data[0]*256 + data[1]); 00124 // if (count >= 32768)count = count - 65536; // 2s comp 00125 // T = (double)count*0.005; 00126 00127 int exponent; 00128 int mantissa; 00129 exponent = int(data[0] >> 4); 00130 mantissa = int(data[0] << 4) + int(data[1]); 00131 lux = 0.045 * mantissa * pow((double)2, exponent); 00132 00133 00134 LuxResponse resp; 00135 resp.luxValue = lux; 00136 resp.status = error; // 1 for nack/error. 0 for ack/success 00137 return resp; 00138 } 00139 00140 double calc_lux(char *data) { 00141 00142 // data is an array of size 2 00143 00144 int exponent = int(data[0] >> 4); 00145 int mantissa = (int)((data[0] << 4) & 0xF0) + (int)(data[1]); 00146 double lux = 0.045 * mantissa * pow((double) 2, exponent); 00147 00148 return lux; 00149 00150 }
Generated on Mon Jul 18 2022 15:25:00 by
1.7.2
