View previous topic :: View next topic |
Author |
Message |
Guest
|
AD7998 driver |
Posted: Wed Oct 19, 2005 2:13 pm |
|
|
Hi All,
Did someone wrote a driver for the I2c adc ad7998 ?
Where I can find it ??
Thanks, |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Wed Oct 19, 2005 4:19 pm |
|
|
Here's the code I used for reading the AD7998.
Code: |
// I2C constants
#define I2C_TIMEOUT 100 // # of tries to start the i2c transaction
#define I2CWRITE 0x00
#define I2CREAD 0x01
// AD7998 constants (external ADC)
#define AD7998_VIN1 0x80
#define AD7998_VIN2 0x90
#define AD7998_VIN3 0xA0
#define AD7998_VIN4 0xB0
#define AD7998_VIN5 0xC0
#define AD7998_VIN6 0xD0
#define AD7998_VIN7 0xE0
#define AD7998_VIN8 0xF0
void i2c_init(int8 iAddr)
// PURPOSE: Initiate the I2C transaction
// PRECONDITIONS: I2C bus is free.
// POSTCONDITIONS: I2C transaction is initiated. The target chip is addressed.
// The code, which has called i2c_init(), is responsible for ending the i2c transaction w/ i2c_stop().
// If i2c transaction didn't initialize, the error flag is set.
// The code, which has called i2c_init(), has to check the error flags at some point.
{
int8 i; // loop counter
i = 0;
do
{
i2c_start();
++i;
delay_ms(1);
}
while (i < I2C_TIMEOUT && i2c_write(iAddr) == 1); // 0 returned for ACK
if (i >= I2C_TIMEOUT)
{
g_sSystemValues.Errors |= E_I2C;
}
} // i2c_init()
int16 read_adc_AD7998(
int8 iAddr, // [in] I2C address of the ADC to be read
int8 iChannel) // [in] address of the channel to be read.
// AD7998 has 8 channels. See also ADC constants.
// PURPOSE: Read one channel on AD7998 ADC over the I2C bus
// PRECONDITIONS: I2C bus is free
// POSTCONDITIONS: The 12-bit value is returned as a 16bit integer.
// I2C bus is freed.
// NOTE: Unused channels are grounded on the board.
{
int8 iRet[2];
i2c_init(iAddr | I2CWRITE); // initialize AD7998 for writing
i2c_write(iChannel); // command/address byte, start the conversion
i2c_init(iAddr | I2CREAD); // initialize AD7998 for reading
iRet[1] = i2c_read(); // read the 1st data byte MSBs
iRet[0] = i2c_read(0); // read the 2nd data byte LSBs, NACK
i2c_stop(); // stop the I2C transaction
return ( make16(iRet[1], iRet[0]) & 0x0FFF ); // mask out the 4 MSBs
} // read_adc_AD79998()
|
|
|
|
Guest
|
|
Posted: Thu Oct 20, 2005 3:30 pm |
|
|
I wrote this code like te example but still it dosent work ...
can you tell me why ???
I'm using the ad7998 -0 AS is GND
union int16_2char
{
int16 int16_;
struct int8_
{
int8 H;
int8 L;
};
} ;
int16 read_adc7998(int8 reg){
union int16_2char ic;
ic.int16_ = 0;
i2c_start();
i2c_write(0x42|I2CREAD);
if(reg == AD7998_ALERT_STATUS || reg == AD7998_CYCLE_TIMER)
{
// 8bit read
ic.int8_.L = i2c_read();
}
else
{
// 16bit read
ic.int8_.H = i2c_read();
ic.int8_.L = i2c_read(0);
}
i2c_stop();
return ic.int16_;
}
[/code] |
|
|
Guest
|
|
Posted: Thu Oct 20, 2005 3:30 pm |
|
|
I wrote this code like te example but still it dosent work ...
can you tell me why ???
I'm using the ad7998 -0 AS is GND
union int16_2char
{
int16 int16_;
struct int8_
{
int8 H;
int8 L;
};
} ;
int16 read_adc7998(int8 reg){
union int16_2char ic;
ic.int16_ = 0;
i2c_start();
i2c_write(0x42|I2CREAD);
if(reg == AD7998_ALERT_STATUS || reg == AD7998_CYCLE_TIMER)
{
// 8bit read
ic.int8_.L = i2c_read();
}
else
{
// 16bit read
ic.int8_.H = i2c_read();
ic.int8_.L = i2c_read(0);
}
i2c_stop();
return ic.int16_;
}
[/code] |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Thu Oct 20, 2005 3:38 pm |
|
|
You don't specify a channel to read. You should do it imediately after you sdress the AD7998.
And by the way, how exactly it doesn't work?
What's your expected result?
What's your actual result? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 20, 2005 3:49 pm |
|
|
Also he's written it so in his 8-bit mode he's not doing a NACK on
the i2c read. |
|
|
Guest
|
|
Posted: Fri Oct 21, 2005 3:23 am |
|
|
Hi,
I corrected the 8 bit nak -> thanks.
There is a write function:
void write_adc7998(int8 reg,int16 data){
union int16_2char ic;
ic.int16_ = 0;
i2c_start();
i2c_write(0x42|I2CWRITE);
if(reg == AD7998_POINTER)
{
// pointer
ic.int16_ = data;
i2c_write(ic.int8_.L);
}
else if(reg == AD7998_ALERT_STATUS || reg == AD7998_CYCLE_TIMER)
{
// 8bit write
i2c_write(reg);
i2c_write((int8)(data));
}
else
{
// 16bit write
ic.int16_ = data;
i2c_write(ic.int8_.H);
i2c_write(ic.int8_.L);
}
i2c_stop();
}
the test looks like this:
write_adc7998(AD7998_HYSTERESIS_CH4, 0x55aa);
adc_val = read_adc7998(AD7998_HYSTERESIS_CH4);
I shold read adc_val as 0x55aa.
now whats wrong ... |
|
|
|