ADS1100 I2C 16bit ADC driver

Files at this revision

API Documentation at this revision

Comitter:
mederic
Date:
Mon Jul 23 12:58:05 2018 +0000
Commit message:
1st released

Changed in this revision

ADS1100.cpp Show annotated file Show diff for this revision Revisions of this file
ADS1100.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADS1100.cpp	Mon Jul 23 12:58:05 2018 +0000
@@ -0,0 +1,70 @@
+#include "ADS1100.h"
+
+#define ADS1100_DR_BIT  2
+#define ADS1100_SC_BIT  4
+#define ADS1100_ST_BIT  7
+#define ADS1100_BSY_BIT 7
+
+ADS1100::ADS1100(I2C* i2c, char ad):_i2c(i2c){
+    _addr = I2C_ADDR | (ad<<1);
+    config(false, 8, 1);
+}
+
+float ADS1100::read(float vdd){
+    int max = -_minCode * _gain;
+    return double(read_s16()) * vdd / double(max);
+}
+
+short ADS1100::read_s16(void){
+    return readReg(&_config);
+}
+
+short ADS1100::readReg(char* config){
+    char buf[3];
+    _i2c->read(_addr,buf,3);
+    *config = buf[2];
+    return (buf[0]<<8) | (buf[1]&0xff);
+}
+
+void ADS1100::config(bool single, char rate, char gain){
+    for(_config=0; _config<4; _config++){
+        if(1==(gain>>_config)){
+            break;
+        }
+    }
+    switch (rate){
+        case 128:
+            _config |= 0 << ADS1100_DR_BIT;
+            _minCode = -2048;
+            break;
+        case 32:
+            _config |= 1 << ADS1100_DR_BIT;
+            _minCode = -8192;
+            break;
+        case 16:
+            _config |= 2 << ADS1100_DR_BIT;
+            _minCode = -16384;
+            break;
+        case 8:
+            _config |= 3 << ADS1100_DR_BIT;
+            _minCode = -32768;
+            break;
+        default:
+            _config |= 3 << ADS1100_DR_BIT;
+            _minCode = -32768;
+            break;
+    };
+    _config |= single << ADS1100_SC_BIT;
+    _i2c->write(_addr, &_config, 1);
+    _gain = gain;
+}
+
+void ADS1100::startConvert(void){
+    _config |= 1<<ADS1100_BSY_BIT;
+    _i2c->write(_addr, &_config, 1);
+}
+
+bool ADS1100::busy(void){
+    readReg(&_config);
+    return _config & (1<<ADS1100_BSY_BIT);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADS1100.h	Mon Jul 23 12:58:05 2018 +0000
@@ -0,0 +1,61 @@
+#ifndef ADS1100_H
+#define ADS1100_H
+
+#include "mbed.h"
+
+class ADS1100{
+    
+    public:
+        enum state{
+            I2C_ADDR = 0x90,    //I2C adress 
+        };
+        
+        
+        /** Create ADC1100 instance
+        * @param *i2c initialized I2C bus to use
+        * @param ad   <0,7> marking code number for addr
+        */
+        ADS1100(I2C* i2c, char ad);
+        
+        /** Read outputCode 
+        * @return signed 16bit output code
+        */
+        short read_s16(void);
+        
+        /** Read output
+        * @param vdd proportional factor defualt=1.0
+        * @return vdd*code/codemax*gain
+        */
+        float read(float vdd=1.0);
+        
+        /** Configure ADS1100
+        * @param single true for single converion mode false for continuous
+        * @param rate can be 8,16,32,128 sample per sencods
+        * @param gain can be 1,2,4,8
+        */
+        void config(bool single, char rate, char gain);
+        
+        /** Read registers
+        * @param output cofig reg
+        * @return s16 code
+        */
+        short readReg(char* config);
+        
+        /** Initiate a conversion (single mode)
+        */
+        void startConvert(void);
+        
+        /** Check if converstion finished (continuous mode)
+        * @return bool true if busy
+        */
+        bool busy(void);
+        
+    private:
+        I2C*    _i2c;
+        char    _addr;
+        char    _config;
+        short   _minCode;
+        char    _gain;
+};
+
+#endif