12-Bit, 8-Channel, ADC System Monitor w/ Temp Sensor, Internal/External Reference, & I2C Interface

Dependents:   ADC128D818_HelloWorld

Files at this revision

API Documentation at this revision

Comitter:
fblanc
Date:
Tue Aug 27 11:38:38 2013 +0000
Child:
1:5f9dbbbc34c5
Commit message:
OK;

Changed in this revision

ADC128D818.cpp Show annotated file Show diff for this revision Revisions of this file
ADC128D818.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADC128D818.cpp	Tue Aug 27 11:38:38 2013 +0000
@@ -0,0 +1,162 @@
+
+#include "ADC128D818.h"
+
+//Create instance
+ADC128D818::ADC128D818(PinName sda, PinName scl, PinName adc_int) : _i2c(sda, scl), _Adc_Int (adc_int)
+{
+}
+
+//destroy instance
+ADC128D818::~ADC128D818()
+{
+}
+
+int ADC128D818::init(char address, char mode, char vref, char rate, char mask_channel, char mask_int)
+{
+
+char data;
+char cmd_data[2];
+
+    _address=address << 1; //bug ?
+
+    //2 test Busy_Status_Register
+    cmd_data[0]= ADC_REG_Busy_Status_Register;
+
+    if(_i2c.write(_address, cmd_data, 1))
+        return  -1; //NO DEVICE
+    _i2c.read(_address,&data,1); //read a byte
+
+    if ((data & Busy_Status_Register_Not_Ready) == 1)
+        return  -2; //ADC is BUSY
+        
+    ADC128D818::stop();
+    //3 Program the Advanced Configuration Register
+    data=0;
+    switch (vref)
+    {
+        case ADC_VREF_INT:
+            data&=~Advanced_Configuration_Register_External_Reference_Enable; //0
+        break;
+        case ADC_VREF_EXT:
+            data|=Advanced_Configuration_Register_External_Reference_Enable; //1
+        break;
+    }
+    switch (mode)
+    {
+        case ADC_MODE_0:
+            data&=~Advanced_Configuration_Register_Mode_Select_0; //0
+            data&=~Advanced_Configuration_Register_Mode_Select_1; //0
+        break;
+        case ADC_MODE_1:
+            data&=~Advanced_Configuration_Register_Mode_Select_0; //0
+            data|=Advanced_Configuration_Register_Mode_Select_1; //1
+        break;
+        case ADC_MODE_2:
+            data|=~Advanced_Configuration_Register_Mode_Select_0; //1
+            data&=Advanced_Configuration_Register_Mode_Select_1; //0
+        break;
+        case ADC_MODE_3:
+            data|=~Advanced_Configuration_Register_Mode_Select_0; //1
+            data|=Advanced_Configuration_Register_Mode_Select_1; //1
+        break;
+    }
+   
+    cmd_data[0]=ADC_REG_Advanced_Configuration_Register;
+    cmd_data[1]=data;
+
+   _i2c.write(_address, cmd_data, 2); //send a byte & wait acknowledged
+
+    //4 Program the Conversion Rate Register
+    data=0;
+    switch (rate)
+    {
+        case ADC_RATE_LOW_POWER:
+            data&=~Advanced_Configuration_Register_External_Reference_Enable; //0
+        break;
+        case ADC_RATE_CONTINUOUS:
+            data|=Advanced_Configuration_Register_External_Reference_Enable; //1
+        break;
+    }
+    
+    cmd_data[0]=ADC_REG_Conversion_Rate_Register;
+    cmd_data[1]=data;
+
+    _i2c.write(_address, cmd_data, 2); //send a byte & wait acknowledged
+
+    //5 Choose to enable or disable the channels using the Channel Disable Register
+
+    cmd_data[0]=ADC_REG_Channel_Disable_Register;
+    cmd_data[1]=mask_channel;
+
+    _i2c.write(_address, cmd_data, 2); //send a byte & wait acknowledged
+
+    //6 Using the Interrupt Mask Register
+
+    cmd_data[0]=ADC_REG_Interrupt_Mask_Register;
+    cmd_data[1]=mask_int;
+
+    _i2c.write(_address, cmd_data, 2); //send a byte & wait acknowledged
+
+return  0;
+}
+
+
+int ADC128D818::init_limit(char channel, int limit, char high_low)
+{
+    char cmd_data[3];
+    char *ptr;
+    cmd_data[0]=ADC_REG_Limit_Registers + channel + high_low;
+    ptr=(char *) & limit;
+    cmd_data[1]=*ptr;
+    cmd_data[2]=*++ptr;
+
+    _i2c.write(_address, cmd_data, 3); //send a byte & wait acknowledged
+
+   return  0;
+}
+
+int ADC128D818::read_channel(char channel)
+{
+
+    char data[2];
+//    char *ptr;
+    char cmd[1] ;
+    cmd[0] = ADC_REG_Channel_Readings_Registers + channel;
+    _i2c.write(_address, cmd, 1); //send a byte & wait acknowledged
+    //ptr=(char *) & data;
+    _i2c.read(_address,data,2); //read a byte
+    
+   return  (int) data[0]*256+data[1];
+}
+
+
+
+char ADC128D818::read_register(char Register)
+{
+    char cmd ;
+    cmd = Register;
+    
+    _i2c.write(_address, &cmd, 1); //send a byte 
+    _i2c.read(_address,&cmd,1); //read a byte
+    
+   return  cmd;
+}
+
+void ADC128D818::start()
+{
+    char cmd_data[2];
+    cmd_data[0]= ADC_REG_Configuration_Register;
+    cmd_data[1]= Configuration_Register_Start | Configuration_Register_INT_Enable  ;
+
+   _i2c.write(_address, cmd_data, 2); //send a 2 byte 
+
+}
+void ADC128D818::stop()
+{
+    char cmd_data[2];
+    cmd_data[0]= ADC_REG_Configuration_Register;
+    cmd_data[1]= 0 ;
+
+   _i2c.write(_address, cmd_data, 2); //send a byte 
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADC128D818.h	Tue Aug 27 11:38:38 2013 +0000
@@ -0,0 +1,115 @@
+
+
+#ifndef ADC128D818_H
+
+#define ADC128D818_H
+
+#include "mbed.h"
+
+
+//Library for the ADC128D818 12 BIT ADC.
+enum ADC_MODE {
+        ADC_MODE_0 = 0x00,
+        ADC_MODE_1 = 0x01,
+        ADC_MODE_2 = 0x02,
+        ADC_MODE_3 = 0x03
+    };
+    enum ADC_ADDRESS {
+        ADC_ADDRESS_LOW_LOW = 0x1D,
+        ADC_ADDRESS_LOW_MID = 0x1E,
+        ADC_ADDRESS_LOW_HIGH = 0x1F,
+        ADC_ADDRESS_MID_LOW = 0x2D,
+        ADC_ADDRESS_MID_MID = 0x2E,
+        ADC_ADDRESS_MID_HIGH = 0x2F,
+        ADC_ADDRESS_HIGH_LOW = 0x35,
+        ADC_ADDRESS_HIGH_MID = 0x36,
+        ADC_ADDRESS_HIGH_HIGH = 0x37
+    };
+    enum ADC_VREF {
+        ADC_VREF_INT = 0x00,
+        ADC_VREF_EXT = 0x01
+    };
+    enum ADC_RATE {
+        ADC_RATE_LOW_POWER = 0x00,
+        ADC_RATE_CONTINUOUS = 0x01
+    };
+    enum ADC_LIMIT {
+        ADC_LIMIT_HIGH = 0x00,
+        ADC_LIMIT_LOW = 0x01
+    };
+    enum ADC_CHANNEL {
+        ADC_CHANNEL_IN0 = 0x00,
+        ADC_CHANNEL_IN1 = 0x01,
+        ADC_CHANNEL_IN2 = 0x02,
+        ADC_CHANNEL_IN3 = 0x03,
+        ADC_CHANNEL_IN4 = 0x04,
+        ADC_CHANNEL_IN5 = 0x05,
+        ADC_CHANNEL_IN6 = 0x06,
+        ADC_CHANNEL_IN7 = 0x07,
+        ADC_CHANNEL_TEMP = 0x07
+    };
+    enum ADC_REG {
+        ADC_REG_Configuration_Register  = 0x00,
+        ADC_REG_Interrupt_Status_Register = 0x01,
+        ADC_REG_Interrupt_Mask_Register = 0x03,
+        ADC_REG_Conversion_Rate_Register = 0x07,
+        ADC_REG_Channel_Disable_Register = 0x08,
+        ADC_REG_One_Shot_Register = 0x09,
+        ADC_REG_Deep_Shutdown_Register = 0x0A,
+        ADC_REG_Advanced_Configuration_Register = 0x0B,
+        ADC_REG_Busy_Status_Register = 0x0C,
+        ADC_REG_Channel_Readings_Registers = 0x20,
+        ADC_REG_Limit_Registers = 0x2A,
+        ADC_REG_Manufacturer_ID_Register = 0x3E,
+        ADC_REG_Revision_ID_Register = 0x3F
+    };
+class ADC128D818
+{
+ protected:
+     
+    
+   enum Configuration_Register {
+        Configuration_Register_Start = 1<<0,
+        Configuration_Register_INT_Enable = 1<<1,
+        Configuration_Register_INT_Clear = 1<<3,
+        Configuration_Register_Initialization = 1<<7
+    };
+    
+    enum Busy_Status_Register {
+        Busy_Status_Register_Busy = 1<<0,
+        Busy_Status_Register_Not_Ready = 1<<1
+    };
+    
+    enum Advanced_Configuration_Register {
+        Advanced_Configuration_Register_External_Reference_Enable = 1<<0,
+        Advanced_Configuration_Register_Mode_Select_0 = 1<<1,
+        Advanced_Configuration_Register_Mode_Select_1 = 1<<2
+    };
+    
+    enum Conversion_Rate_Register {
+        Conversion_Rate_Register_Rate_Register = 1<<0
+    };
+    
+ 
+private:
+    I2C _i2c;
+    char _data[2];
+    char _address;
+    char _mode;
+    
+public:
+    
+    ADC128D818(PinName sda, PinName scl, PinName adc_int );
+    ~ADC128D818();
+
+    InterruptIn _Adc_Int;
+    int read_channel(char channel);
+    char read_register(char Register);
+    int init(char address, char mode, char vref, char rate, char mask_channel, char mask_int);
+    int init_limit(char channel, int limit, char high_low);
+    void start();
+    void stop();
+};
+
+#endif
+