12-Bit, 8-Channel, ADC System Monitor w/ Temp Sensor, Internal/External Reference, & I2C Interface
Dependents: ADC128D818_HelloWorld
Diff: ADC128D818.cpp
- Revision:
- 0:9cc68ef524da
- Child:
- 1:5f9dbbbc34c5
--- /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