AKM AK9752 Ultra-small IR Sensor IC with I2C I/F
Embed:
(wiki syntax)
Show/hide line numbers
AK9752.cpp
00001 #include "mbed.h" 00002 #include "AK9752.h" 00003 00004 #define REG_WIA1 0x00 00005 #define REG_WIA2 0x01 00006 #define REG_INFO1 0x02 00007 #define REG_INFO2 0x03 00008 #define REG_ST1 0x04 00009 #define REG_INTCAUSE 0x05 00010 #define REG_IR_LSB 0x06 00011 #define REG_IR_MSB 0x07 00012 #define REG_TMP_LSB 0x08 00013 #define REG_TMP_MSB 0x09 00014 #define REG_ST2 0x0A 00015 #define REG_THIRH_LSB 0x0B 00016 #define REG_THIRH_MSB 0x0C 00017 #define REG_THIRL_LSB 0x0D 00018 #define REG_THIRL_MSB 0x0E 00019 #define REG_THTMPH_LSB 0x0F 00020 #define REG_THTMPH_MSB 0x10 00021 #define REG_THTMPL_LSB 0x11 00022 #define REG_THTMPL_MSB 0x12 00023 #define REG_INTEN 0x13 00024 #define REG_CNTL1 0x14 00025 #define REG_CNTL2 0x15 00026 #define REG_CNTL3 0x16 00027 00028 00029 AK9752::AK9752(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) { 00030 // activate the peripheral 00031 } 00032 00033 AK9752::~AK9752() { } 00034 00035 int AK9752::readRegs(int addr, uint8_t * data, int len) { 00036 char t[1] = {addr}; 00037 int result ; 00038 result = m_i2c.write(m_addr, t, 1, true); 00039 if (result == 0) { 00040 result = m_i2c.read(m_addr, (char *)data, len); 00041 } 00042 return( result ) ; 00043 } 00044 00045 int AK9752::writeRegs(uint8_t * data, int len) { 00046 int result ; 00047 result = m_i2c.write(m_addr, (char *)data, len); 00048 return( result ) ; 00049 } 00050 00051 /** 00052 * getCompanyCode 00053 * the code is expected to be 0x48 00054 */ 00055 uint8_t AK9752::getCompanyCode(void) 00056 { 00057 uint8_t data ; 00058 readRegs(REG_WIA1, &data, 1) ; 00059 return(data) ; 00060 } 00061 00062 /** 00063 * getDeviceID 00064 * the ID is expected to be 0x14 00065 */ 00066 uint8_t AK9752::getDeviceID(void) 00067 { 00068 uint8_t data ; 00069 readRegs(REG_WIA2, &data, 1) ; 00070 return(data) ; 00071 } 00072 00073 bool AK9752::dataReady(void) /* returns ST1[0], read ST2 to clear */ 00074 { 00075 uint8_t data ; 00076 readRegs(REG_ST1, &data, 1) ; 00077 return((data & 0x01) == 0x01) ; 00078 } 00079 00080 uint8_t AK9752::getIntcause(void) /* get REG_INTCAUSE (0x05) */ 00081 { 00082 uint8_t data ; 00083 readRegs(REG_INTCAUSE, &data, 1) ; 00084 return( data ) ; 00085 } 00086 00087 int16_t AK9752::getRawIR(void) /* raw data, must be mulitplied by 0.4578 for pA */ 00088 { 00089 uint16_t temp ; 00090 uint8_t data[2] ; 00091 readRegs(REG_IR_LSB, data, 2) ; 00092 temp = (data[1] << 8) | data[0] ; 00093 return( temp ) ; 00094 } 00095 00096 float AK9752::getIR(void) 00097 { 00098 float fIR ; 00099 uint16_t iIR ; 00100 iIR = getRawIR() ; 00101 fIR = iIR * 0.4578 ; 00102 return( fIR ) ; 00103 } 00104 00105 int16_t AK9752::getRawTMP(void) /* raw data, must be multiplied by 0.0019837 and + 25 */ 00106 { 00107 uint16_t temp ; 00108 uint8_t data[2] ; 00109 readRegs(REG_TMP_LSB, data, 2) ; 00110 temp = (data[1] << 8) | data[0] ; 00111 return( temp ) ; 00112 } 00113 00114 float AK9752::getTMP(void) 00115 { 00116 float ftemp ; 00117 uint16_t itemp ; 00118 itemp = getRawTMP() ; 00119 ftemp = itemp * 0.0019837 + 25.0 ; 00120 return( ftemp ) ; 00121 } 00122 00123 bool AK9752::dataOverRun(void) /* check data over run and clear data ready */ 00124 { 00125 uint8_t data ; 00126 readRegs(REG_ST2, &data, 1) ; 00127 return((data & 0x01) == 0x01) ; 00128 } 00129 00130 void AK9752::getTHIR(int16_t *high, int16_t *low) 00131 { 00132 uint8_t data[4] ; 00133 readRegs(REG_THIRH_LSB, data, 4) ; 00134 *high = (data[1] << 8) | data[0] ; 00135 *low = (data[3] << 8) | data[2] ; 00136 } 00137 00138 void AK9752::setTHIR(int16_t high, int16_t low) 00139 { 00140 uint8_t data[5] ; 00141 data[0] = REG_THIRH_LSB ; 00142 data[1] = high & 0xFF ; 00143 data[2] = (high >> 8) & 0xFF ; 00144 data[3] = low & 0xFF ; 00145 data[4] = (low >> 8) & 0xFF ; 00146 writeRegs(data, 5) ; 00147 } 00148 00149 void AK9752::getTHTMP(int16_t *high, int16_t *low) 00150 { 00151 uint8_t data[4] ; 00152 readRegs(REG_THTMPH_LSB, data, 4) ; 00153 *high = (data[1] << 8) | data[0] ; 00154 *low = (data[3] << 8) | data[2] ; 00155 } 00156 00157 void AK9752::setTHTMP(int16_t high, int16_t low) 00158 { 00159 uint8_t data[5] ; 00160 data[0] = REG_THTMPH_LSB ; 00161 data[1] = high & 0xFF ; 00162 data[2] = (high >> 8) & 0xFF ; 00163 data[3] = low & 0xFF ; 00164 data[4] = (low >> 8) & 0xFF ; 00165 writeRegs(data, 5) ; 00166 } 00167 00168 /** 00169 * ITEN[4] = IRHI : IR upper threshold interrupt enable 00170 * ITEN[3] = IRLI : IR lower threshold interrupt enable 00171 * ITEN[2] = TMPHI : Temp upper threshold interrupt enable 00172 * ITEN[1] = TMPLI : Temp lower threshold interrupt enable 00173 * ITEN[0] = DRI : Data ready interrupt enable 00174 */ 00175 uint8_t AK9752::getINTEN(void) 00176 { 00177 uint8_t value ; 00178 readRegs(REG_INTEN, &value, 1) ; 00179 return(value) ; 00180 } 00181 00182 void AK9752::setINTEN(uint8_t value) 00183 { 00184 uint8_t t[2] ; 00185 t[0] = REG_INTEN ; 00186 t[1] = value ; 00187 writeRegs(t, 2) ; 00188 } 00189 00190 /** 00191 * CNTL1 00192 * CNTL1[4:2] = FCTMP[2:0] 00193 * FCTMP = 000 : no digital filter 00194 * FCTMP = 001 : Fc = 2.5Hz 00195 * FCTMP = 010 : Fc = 0.9Hz 00196 * FCTMP = 011 : Fc = 0.45Hz 00197 * FCTMP = 100 : Fc = 0.22Hz 00198 * FCTMP = other : (reserved) 00199 * CNTL1[1:0] = FCIR[1:0] 00200 * FCIR = 00 : no digital filter 00201 * FCIR = 01 : Fc = 2.5Hz 00202 * FCIR = 10 : Fc = 0.9Hz 00203 * FCIR = 11 : Fc = 0.45Hz 00204 */ 00205 uint8_t AK9752::getCNTL1(void) 00206 { 00207 uint8_t value ; 00208 readRegs(REG_CNTL1, &value, 1) ; 00209 return(value) ; 00210 } 00211 00212 void AK9752::setCNTL1(uint8_t value) 00213 { 00214 uint8_t t[2] ; 00215 t[0] = REG_CNTL1 ; 00216 t[1] = value ; 00217 writeRegs(t, 2) ; 00218 } 00219 00220 /** 00221 * CNTL2 00222 * CNTL2[1:0] = MODE[1:0] 00223 * MODE = 00 : standby 00224 * MODE = 01 : continuous measure 00225 * MODE = 10 : single shot 00226 * MODE = 11 : (reserved) 00227 */ 00228 uint8_t AK9752::getCNTL2(void) 00229 { 00230 uint8_t value ; 00231 readRegs(REG_CNTL2, &value, 1) ; 00232 return(value) ; 00233 } 00234 00235 void AK9752::setCNTL2(uint8_t value) 00236 { 00237 uint8_t t[2] ; 00238 t[0] = REG_CNTL2 ; 00239 t[1] = value ; 00240 writeRegs(t, 2) ; 00241 } 00242 00243 void AK9752::software_reset(void) 00244 { 00245 uint8_t t[2] = { REG_CNTL3, 0xFF } ; 00246 writeRegs(t, 2) ; 00247 }
Generated on Tue Jul 12 2022 22:52:49 by 1.7.2