ADS1015 library

Dependents:   s-rov-firmware

Committer:
YJ_Kim
Date:
Thu Jan 26 18:02:32 2017 +0000
Revision:
2:2407708483f4
Parent:
0:b3c2afab9578
fix bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YJ_Kim 0:b3c2afab9578 1 #include "ads1015.h"
YJ_Kim 0:b3c2afab9578 2
YJ_Kim 0:b3c2afab9578 3 ADS1015::ADS1015(uint8_t i2c_addr, I2C* i2c_object, uint16_t adc_gain):
YJ_Kim 0:b3c2afab9578 4 ADS1015_ADDR(i2c_addr << 1),
YJ_Kim 0:b3c2afab9578 5 i2c(i2c_object),
YJ_Kim 0:b3c2afab9578 6 m_gain(adc_gain)
YJ_Kim 0:b3c2afab9578 7 {}
YJ_Kim 0:b3c2afab9578 8
YJ_Kim 0:b3c2afab9578 9 uint16_t ADS1015::read_single_channel(int ch){
YJ_Kim 0:b3c2afab9578 10 if(ch > 3)
YJ_Kim 0:b3c2afab9578 11 return 0;
YJ_Kim 0:b3c2afab9578 12
YJ_Kim 0:b3c2afab9578 13 uint16_t config = ADS1015_CONFIG_COMP_QUE_NONE | // disable comparator
YJ_Kim 0:b3c2afab9578 14 ADS1015_CONFIG_COMP_LAT_NONE | // nonlatching comparator
YJ_Kim 0:b3c2afab9578 15 ADS1015_CONFIG_COMP_POL_ACTVLOW | // Alert/Rdy active low
YJ_Kim 2:2407708483f4 16 ADS1015_CONFIG_COMP_MODE_TRAD | // Traditional comparator
YJ_Kim 0:b3c2afab9578 17 ADS1015_CONFIG_DR_1600SPS | // Data rate : 1600sps
YJ_Kim 0:b3c2afab9578 18 ADS1015_CONFIG_MODE_SINGLE | // Single shot mode
YJ_Kim 0:b3c2afab9578 19 m_gain; // Set gain
YJ_Kim 0:b3c2afab9578 20
YJ_Kim 0:b3c2afab9578 21 switch(ch){
YJ_Kim 0:b3c2afab9578 22 case 0 :
YJ_Kim 0:b3c2afab9578 23 config |= ADS1015_CONFIG_MUX_SINGLE_0;
YJ_Kim 0:b3c2afab9578 24 break;
YJ_Kim 0:b3c2afab9578 25 case 1 :
YJ_Kim 0:b3c2afab9578 26 config |= ADS1015_CONFIG_MUX_SINGLE_1;
YJ_Kim 0:b3c2afab9578 27 break;
YJ_Kim 0:b3c2afab9578 28 case 2 :
YJ_Kim 0:b3c2afab9578 29 config |= ADS1015_CONFIG_MUX_SINGLE_2;
YJ_Kim 0:b3c2afab9578 30 break;
YJ_Kim 0:b3c2afab9578 31 case 3 :
YJ_Kim 0:b3c2afab9578 32 config |= ADS1015_CONFIG_MUX_SINGLE_3;
YJ_Kim 0:b3c2afab9578 33 break;
YJ_Kim 0:b3c2afab9578 34 }
YJ_Kim 0:b3c2afab9578 35
YJ_Kim 0:b3c2afab9578 36 config |= ADS1015_CONFIG_OS_START_CONV;
YJ_Kim 0:b3c2afab9578 37
YJ_Kim 0:b3c2afab9578 38 write_16(ADS1015_REG_CONFIG, config);
YJ_Kim 0:b3c2afab9578 39 wait_ms(ADS1015_CONVERSIONDELAY);
YJ_Kim 0:b3c2afab9578 40 return read_16(ADS1015_REG_CONVERSION) >> 4;
YJ_Kim 0:b3c2afab9578 41 }
YJ_Kim 0:b3c2afab9578 42
YJ_Kim 0:b3c2afab9578 43 void ADS1015::write_16(uint8_t reg, uint16_t msg){
YJ_Kim 0:b3c2afab9578 44 char cmd[3];
YJ_Kim 0:b3c2afab9578 45 cmd[0] = reg;
YJ_Kim 0:b3c2afab9578 46 cmd[1] = (char)(msg>>8);
YJ_Kim 0:b3c2afab9578 47 cmd[2] = (char)(msg & 0xff);
YJ_Kim 0:b3c2afab9578 48
YJ_Kim 0:b3c2afab9578 49 i2c->write(ADS1015_ADDR, cmd, 3);
YJ_Kim 0:b3c2afab9578 50 }
YJ_Kim 0:b3c2afab9578 51
YJ_Kim 0:b3c2afab9578 52 uint16_t ADS1015::read_16(uint8_t reg){
YJ_Kim 0:b3c2afab9578 53 char cmd[2];
YJ_Kim 0:b3c2afab9578 54 cmd[0] = reg;
YJ_Kim 0:b3c2afab9578 55 i2c->write(ADS1015_ADDR, cmd, 1);
YJ_Kim 0:b3c2afab9578 56 i2c->read(ADS1015_ADDR, cmd, 2);
YJ_Kim 2:2407708483f4 57 return (cmd[0] << 8 | cmd[1]);
YJ_Kim 0:b3c2afab9578 58 }