Maxim MAX44009 Industry's Lowest-Power. Ambient Light Sensor with ADC. http://www.maximintegrated.com/datasheet/index.mvp/id/7175
MAX44009.cpp@0:726b77a319d8, 2013-08-07 (annotated)
- Committer:
- gizmo69the2nd
- Date:
- Wed Aug 07 22:45:56 2013 +0000
- Revision:
- 0:726b77a319d8
Final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gizmo69the2nd | 0:726b77a319d8 | 1 | /* |
gizmo69the2nd | 0:726b77a319d8 | 2 | * MAX44009 Ambient Light Sensor with ADC library |
gizmo69the2nd | 0:726b77a319d8 | 3 | * |
gizmo69the2nd | 0:726b77a319d8 | 4 | * |
gizmo69the2nd | 0:726b77a319d8 | 5 | * Copyright (c) 2013 Davy Van Belle, MIT License |
gizmo69the2nd | 0:726b77a319d8 | 6 | * |
gizmo69the2nd | 0:726b77a319d8 | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
gizmo69the2nd | 0:726b77a319d8 | 8 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
gizmo69the2nd | 0:726b77a319d8 | 9 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
gizmo69the2nd | 0:726b77a319d8 | 10 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
gizmo69the2nd | 0:726b77a319d8 | 11 | * furnished to do so, subject to the following conditions: |
gizmo69the2nd | 0:726b77a319d8 | 12 | * |
gizmo69the2nd | 0:726b77a319d8 | 13 | * The above copyright notice and this permission notice shall be included in all copies or |
gizmo69the2nd | 0:726b77a319d8 | 14 | * substantial portions of the Software. |
gizmo69the2nd | 0:726b77a319d8 | 15 | * |
gizmo69the2nd | 0:726b77a319d8 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
gizmo69the2nd | 0:726b77a319d8 | 17 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
gizmo69the2nd | 0:726b77a319d8 | 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
gizmo69the2nd | 0:726b77a319d8 | 19 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
gizmo69the2nd | 0:726b77a319d8 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
gizmo69the2nd | 0:726b77a319d8 | 21 | */ |
gizmo69the2nd | 0:726b77a319d8 | 22 | |
gizmo69the2nd | 0:726b77a319d8 | 23 | /** @file |
gizmo69the2nd | 0:726b77a319d8 | 24 | * @brief MAX44009 I2C |
gizmo69the2nd | 0:726b77a319d8 | 25 | */ |
gizmo69the2nd | 0:726b77a319d8 | 26 | |
gizmo69the2nd | 0:726b77a319d8 | 27 | #include "mbed.h" |
gizmo69the2nd | 0:726b77a319d8 | 28 | #include "MAX44009.h" |
gizmo69the2nd | 0:726b77a319d8 | 29 | |
gizmo69the2nd | 0:726b77a319d8 | 30 | /** init MAX44009 class |
gizmo69the2nd | 0:726b77a319d8 | 31 | * @param *i2c pointer to I2C serial interface |
gizmo69the2nd | 0:726b77a319d8 | 32 | * @param addr sensor I2C address |
gizmo69the2nd | 0:726b77a319d8 | 33 | */ |
gizmo69the2nd | 0:726b77a319d8 | 34 | MAX44009::MAX44009 (I2C* i2c, char addr) |
gizmo69the2nd | 0:726b77a319d8 | 35 | { |
gizmo69the2nd | 0:726b77a319d8 | 36 | _i2c = i2c; |
gizmo69the2nd | 0:726b77a319d8 | 37 | _addr = addr; |
gizmo69the2nd | 0:726b77a319d8 | 38 | } |
gizmo69the2nd | 0:726b77a319d8 | 39 | |
gizmo69the2nd | 0:726b77a319d8 | 40 | /** Set configuration register for the device |
gizmo69the2nd | 0:726b77a319d8 | 41 | * @param config desired configuration register bits |
gizmo69the2nd | 0:726b77a319d8 | 42 | * BIT 7 - CONT: 1 = continuous mode, 0 = single measurement |
gizmo69the2nd | 0:726b77a319d8 | 43 | * BIT 6 - MANUAL: 1 = CDR, TIM[2:0] set by user, 0 = CDR, TIM[2:0] set by internal autorange |
gizmo69the2nd | 0:726b77a319d8 | 44 | * BIT [5:4] - Not Used |
gizmo69the2nd | 0:726b77a319d8 | 45 | * BIT 3 - CDR: 1 = Current divided by 8. (High-brightness), 0 = Current not divided. |
gizmo69the2nd | 0:726b77a319d8 | 46 | * BIT [2:0] - TIM: Integration Time. See datasheet. |
gizmo69the2nd | 0:726b77a319d8 | 47 | */ |
gizmo69the2nd | 0:726b77a319d8 | 48 | |
gizmo69the2nd | 0:726b77a319d8 | 49 | void MAX44009::setConfig (char config) |
gizmo69the2nd | 0:726b77a319d8 | 50 | { |
gizmo69the2nd | 0:726b77a319d8 | 51 | char cmd[2]; |
gizmo69the2nd | 0:726b77a319d8 | 52 | cmd[0] = CONFIG; |
gizmo69the2nd | 0:726b77a319d8 | 53 | cmd[1] = config; |
gizmo69the2nd | 0:726b77a319d8 | 54 | _i2c->write(_addr,cmd,2); |
gizmo69the2nd | 0:726b77a319d8 | 55 | } |
gizmo69the2nd | 0:726b77a319d8 | 56 | |
gizmo69the2nd | 0:726b77a319d8 | 57 | /** Get device INT_STATUS register |
gizmo69the2nd | 0:726b77a319d8 | 58 | * BIT 0 : 0 = No interrupt event occurred, 1 = Ambient light intensity is outside the threshold range. |
gizmo69the2nd | 0:726b77a319d8 | 59 | */ |
gizmo69the2nd | 0:726b77a319d8 | 60 | char MAX44009::getIntStatus() |
gizmo69the2nd | 0:726b77a319d8 | 61 | { |
gizmo69the2nd | 0:726b77a319d8 | 62 | char status; |
gizmo69the2nd | 0:726b77a319d8 | 63 | char cmd = INT_STATUS; |
gizmo69the2nd | 0:726b77a319d8 | 64 | _i2c->write(_addr,&cmd,1,true); |
gizmo69the2nd | 0:726b77a319d8 | 65 | _i2c->read(_addr+1,&status,1); |
gizmo69the2nd | 0:726b77a319d8 | 66 | return status; |
gizmo69the2nd | 0:726b77a319d8 | 67 | } |
gizmo69the2nd | 0:726b77a319d8 | 68 | |
gizmo69the2nd | 0:726b77a319d8 | 69 | /** Set device INT_ENABLE register |
gizmo69the2nd | 0:726b77a319d8 | 70 | * @param Enable BIT 0 : 0 = INT pin and INTS bit not effected if an interrupt event occurred, 1 = INT pin pulled low and INTS bit is set if interrupt occurred. / |
gizmo69the2nd | 0:726b77a319d8 | 71 | */ |
gizmo69the2nd | 0:726b77a319d8 | 72 | void MAX44009::setIntEnable(bool Enable) |
gizmo69the2nd | 0:726b77a319d8 | 73 | { |
gizmo69the2nd | 0:726b77a319d8 | 74 | char cmd[2]; |
gizmo69the2nd | 0:726b77a319d8 | 75 | cmd[0] = INT_ENABLE; |
gizmo69the2nd | 0:726b77a319d8 | 76 | cmd[1] = 0x00 | Enable; |
gizmo69the2nd | 0:726b77a319d8 | 77 | _i2c->write(_addr,cmd,2); |
gizmo69the2nd | 0:726b77a319d8 | 78 | } |
gizmo69the2nd | 0:726b77a319d8 | 79 | |
gizmo69the2nd | 0:726b77a319d8 | 80 | /** Get LUX reading from ADC |
gizmo69the2nd | 0:726b77a319d8 | 81 | * |
gizmo69the2nd | 0:726b77a319d8 | 82 | */ |
gizmo69the2nd | 0:726b77a319d8 | 83 | float MAX44009::getLUXReading() |
gizmo69the2nd | 0:726b77a319d8 | 84 | { |
gizmo69the2nd | 0:726b77a319d8 | 85 | char buff[2]; |
gizmo69the2nd | 0:726b77a319d8 | 86 | char cmd[2]; |
gizmo69the2nd | 0:726b77a319d8 | 87 | char expo,mant; |
gizmo69the2nd | 0:726b77a319d8 | 88 | float lux; |
gizmo69the2nd | 0:726b77a319d8 | 89 | |
gizmo69the2nd | 0:726b77a319d8 | 90 | cmd[0] = LUX_HIGH_B; |
gizmo69the2nd | 0:726b77a319d8 | 91 | cmd[1] = LUX_LOW_B; |
gizmo69the2nd | 0:726b77a319d8 | 92 | |
gizmo69the2nd | 0:726b77a319d8 | 93 | _i2c->write(_addr,&cmd[0],1,true); |
gizmo69the2nd | 0:726b77a319d8 | 94 | _i2c->read(_addr+1,&buff[0],1,true); |
gizmo69the2nd | 0:726b77a319d8 | 95 | _i2c->write(_addr,&cmd[1],1,true); |
gizmo69the2nd | 0:726b77a319d8 | 96 | _i2c->read(_addr+1,&buff[1],1); |
gizmo69the2nd | 0:726b77a319d8 | 97 | |
gizmo69the2nd | 0:726b77a319d8 | 98 | expo = (buff[0] & 0xF0) >> 4; |
gizmo69the2nd | 0:726b77a319d8 | 99 | mant = ((buff[0] & 0x0F) << 4) | (buff[1] & 0x0F) ; |
gizmo69the2nd | 0:726b77a319d8 | 100 | |
gizmo69the2nd | 0:726b77a319d8 | 101 | lux = pow((double)2,(double)expo) * mant * 0.045; |
gizmo69the2nd | 0:726b77a319d8 | 102 | |
gizmo69the2nd | 0:726b77a319d8 | 103 | return lux; |
gizmo69the2nd | 0:726b77a319d8 | 104 | } |
gizmo69the2nd | 0:726b77a319d8 | 105 | |
gizmo69the2nd | 0:726b77a319d8 | 106 | /** Set upper threshold |
gizmo69the2nd | 0:726b77a319d8 | 107 | * @param threshold set upper threshold value. Further info, see datasheet |
gizmo69the2nd | 0:726b77a319d8 | 108 | */ |
gizmo69the2nd | 0:726b77a319d8 | 109 | void MAX44009::setUpperThres(char threshold) |
gizmo69the2nd | 0:726b77a319d8 | 110 | { |
gizmo69the2nd | 0:726b77a319d8 | 111 | char cmd[2]; |
gizmo69the2nd | 0:726b77a319d8 | 112 | cmd[0] = UP_THRESH_HIGH_B; |
gizmo69the2nd | 0:726b77a319d8 | 113 | cmd[1] = threshold; |
gizmo69the2nd | 0:726b77a319d8 | 114 | _i2c->write(_addr,cmd,2); |
gizmo69the2nd | 0:726b77a319d8 | 115 | } |
gizmo69the2nd | 0:726b77a319d8 | 116 | |
gizmo69the2nd | 0:726b77a319d8 | 117 | /** Set lower threshold |
gizmo69the2nd | 0:726b77a319d8 | 118 | * @param threshold set lower threshold value. Further info, see datasheet |
gizmo69the2nd | 0:726b77a319d8 | 119 | */ |
gizmo69the2nd | 0:726b77a319d8 | 120 | void MAX44009::setLowerThres(char threshold) |
gizmo69the2nd | 0:726b77a319d8 | 121 | { |
gizmo69the2nd | 0:726b77a319d8 | 122 | char cmd[2]; |
gizmo69the2nd | 0:726b77a319d8 | 123 | cmd[0] = LOW_THRESH_HIGH_B; |
gizmo69the2nd | 0:726b77a319d8 | 124 | cmd[1] = threshold; |
gizmo69the2nd | 0:726b77a319d8 | 125 | _i2c->write(_addr,cmd,2); |
gizmo69the2nd | 0:726b77a319d8 | 126 | } |
gizmo69the2nd | 0:726b77a319d8 | 127 | |
gizmo69the2nd | 0:726b77a319d8 | 128 | /** Set Threshold time |
gizmo69the2nd | 0:726b77a319d8 | 129 | * @param time set time to trigger interrupt if value is below or above threshold value. Further info, see datasheet |
gizmo69the2nd | 0:726b77a319d8 | 130 | */ |
gizmo69the2nd | 0:726b77a319d8 | 131 | void MAX44009::setThresTimer(char time) |
gizmo69the2nd | 0:726b77a319d8 | 132 | { |
gizmo69the2nd | 0:726b77a319d8 | 133 | char cmd[2]; |
gizmo69the2nd | 0:726b77a319d8 | 134 | cmd[0] = THRESH_TIMER; |
gizmo69the2nd | 0:726b77a319d8 | 135 | cmd[1] = time; |
gizmo69the2nd | 0:726b77a319d8 | 136 | _i2c->write(_addr,cmd,2); |
gizmo69the2nd | 0:726b77a319d8 | 137 | } |
gizmo69the2nd | 0:726b77a319d8 | 138 | |
gizmo69the2nd | 0:726b77a319d8 | 139 | /* |
gizmo69the2nd | 0:726b77a319d8 | 140 | float operator= (float d) |
gizmo69the2nd | 0:726b77a319d8 | 141 | { |
gizmo69the2nd | 0:726b77a319d8 | 142 | return this.getLUXReading(); |
gizmo69the2nd | 0:726b77a319d8 | 143 | } |
gizmo69the2nd | 0:726b77a319d8 | 144 | */ |
gizmo69the2nd | 0:726b77a319d8 | 145 |