StuartH
Joined: 19 Aug 2005 Posts: 14 Location: W. Midlands, UK
|
MAX521 Driver |
Posted: Tue Jul 11, 2006 4:22 pm |
|
|
This is a simple driver for the Maxim MAX521 8 channel digital to analogue converter (DAC). It assumes that I2C has been set up elsewhere in your program.
Code: |
///////////////////////////////////////////////////////////////////////////////
///
/// MAX521.C
///
/// CCS C Driver for the Maxim MAX521 Octal DAC with 2-wire serial interface
/// (I2C compatible).
///
/// Stuart Hunter 28/04/2006
///
/// Comments:
///
/// Functions:
///
/// max521_init()
/// Call this function before using the device, and whenever you need to reset
/// the chip AND driver.
///
/// max521_reset()
/// Call this function to reset the DAC chip only.
///
/// max521_power_mode( int1 mode )
/// Call this to set normal or low power/standby operation. In standby mode, all
/// DAC outputs are floating and current consumption is reduced to around 4uA.
/// mode == MAX521_NORMAL or MAX521_STANDBY
/// Note: this setting will persist until the function is called again (or the
/// driver is reset with a max521_init() call ). The device and driver initialises
/// in the MAX521_NORMAL state.
///
/// max521_write_single_dac( int8 dac, int8 value )
/// Call this to immediately update a single specified DAC (0-7) with the supplied
/// value.
/// dac == DAC number, 0 to 7
/// value == DAC output value, 0 to 255
///
/// max521_load_dac( int8 dac, int8 value )
/// Call this to preload a specified DAC with the supplied value. This function
/// stores the value locally in the driver and does not pass it to the DAC
/// immediately. This is useful if you want all DAC outputs to change
/// simultaneously.
///
/// max521_write_dac()
/// Writes preloaded data to all DACs in the device. Only individual DACs which have
/// been preloaded since the last call to this function are updated. All affected
/// DAC outputs change simultaneously.
///
/// None of these functions returns anyhing; they are all of type void.
///
/******************************************************************************
****** T O D O : Set MAX521_DEVICE_ADDRESS to the actual address of the *****
****** MAX521 in your system. It is an 8 bit number in the *****
****** format 01010xx0, where xx refers to the setting of the *****
****** AD1 and AD0 pins on the device. *****
****** *****
****** I have not implemented any support for multiple MAX521 *****
****** devices (you can have up to four). It shouldn't be too *****
****** difficult to modify this driver though, if you need *****
****** that functionality. *****
******************************************************************************/
#define MAX521_DEVICE_ADDRESS 0x50 //AD0 and AD1 both pulled LOW
/*****************************************************************************/
#define MAX521_NORMAL 0
#define MAX521_STANDBY 1
int8 max521_command;
int8 max521_dac[8];
int8 max521_bitfield;
///////////////////////////////////////////////////////////////////////////////
// Send a single command to the DAC. Internal use only.
///////////////////////////////////////////////////////////////////////////////
void max521_send_command( void )
{
i2c_start();
i2c_write( MAX521_DEVICE_ADDRESS );
i2c_write( max521_command );
i2c_stop();
}
///////////////////////////////////////////////////////////////////////////////
// Send a string of commands and data. Internal use only.
///////////////////////////////////////////////////////////////////////////////
void max521_send_data( int8 dac_bitfield )
{
int8 n;
i2c_start();
i2c_write( MAX521_DEVICE_ADDRESS );
for( n=0; n<8; n++ )
{
if( dac_bitfield & (1<<n) )
{
max521_command |= n;
i2c_write( max521_command );
i2c_write( max521_dac[n] );
max521_command &= 0x18; //reset 3 LSBs
}
}
i2c_stop();
}
///////////////////////////////////////////////////////////////////////////////
// Set the power mode.
// Mode == MAX521_NORMAL or STANDBY
// Device supply current is reduced to 4uA in standby, but the outputs are
// nonfunctional (floating).
///////////////////////////////////////////////////////////////////////////////
void max521_power_mode( int1 mode )
{
if( mode )
max521_command |= 8;
else
max521_command &= 0xF7;
max521_send_command();
}
///////////////////////////////////////////////////////////////////////////////
// Reset the DAC
///////////////////////////////////////////////////////////////////////////////
void max521_reset( void )
{
int8 tmp;
tmp = max521_command;
max521_command = 0x10;
max521_send_command();
max521_command = tmp;
}
///////////////////////////////////////////////////////////////////////////////
// Call this routine to initialise the driver.
///////////////////////////////////////////////////////////////////////////////
void max521_init( void )
{
int8 n;
max521_command = 0;
max521_bitfield = 0;
for( n=0;n<8;n++ )
max521_dac[n] = 0;
max521_reset();
}
///////////////////////////////////////////////////////////////////////////////
// Call this function to update an individual DAC with the value supplied.
// dac == 0-7
// value == 0-255
///////////////////////////////////////////////////////////////////////////////
void max521_write_single_dac( int8 dac, int8 value )
{
int8 bf;
max521_dac[dac] = value;
bf = 1<<dac;
max521_send_data( bf );
}
///////////////////////////////////////////////////////////////////////////////
// Call this function to preload the DAC with data. Same arguments as
// max521_write_single_dac()
///////////////////////////////////////////////////////////////////////////////
void max521_load_dac( int8 dac, int8 value )
{
int8 bf;
max521_dac[dac] = value;
bf = 1<<dac;
max521_bitfield |= bf;
//Note: this function does NOT
//write anything to the DACs!!!
}
///////////////////////////////////////////////////////////////////////////////
// Call this function to write the preloaded data to the DACs. All outputs will
// change simultaneously.
///////////////////////////////////////////////////////////////////////////////
void max521_write_dac( void )
{
max521_send_data( max521_bitfield );
max521_bitfield = 0;
}
|
That's it. |
|