kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
AD5258 digital pot driver |
Posted: Sun Mar 09, 2008 5:01 pm |
|
|
A small driver for the AD5258 digital potentiometer with I2C interface.
Code: |
void i2c_init(
int8 iAddr) // [in] address of the target I2C device with R/W# bit
// 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()
void set_pot_AD5258(
int8 iAddr, // [in] I2C slave address of the AD5258 digital pot
int8 iPotVal) // [in] potentiometer position (AD5258 has 64 positions)
// PURPOSE: Set the value of the AD5258 digital pot
{
i2c_init(iAddr | I2CWRITE);
i2c_write(0x00); // instruction byte
i2c_write(iPotVal); // wiper position
i2c_stop();
}
|
_________________ Read the label, before opening a can of worms. |
|