Analog To Digital Converter AD7998


This examples demonstrates reading the analog inputs of Analog Devices AD7998 Analog to Digital Converter using the I2C (2-wire) interface.

Toolkit:ARM Development System

Location:/bipom/devtools/GCC/LPC2000/examples/adc/AD7998

Code Example


int main (void)
{
	UBYTE channel;
	UINT value;
	
	I2C_ERRCODE ec = I2C_SUCCESS;
	I2C_DATA dataTemp;	
	
	/* Initialize the system */
	Initialize();
	
	/* Send messages */
	uart0Puts("\n\rBIPOM MINI-MAX/ARM");
	uart0Puts("\n\rAD7998 EXAMPLE");

	/* Initialize 100 KHz I2C bus */
	I2C_Config(100000);

	/* Repair I2C bus */
	ec = I2C_Repair();

	if( ec != I2C_SUCCESS )
	{
		tiprintf ("\n\rERROR: Can't repair I2C bus, ec = %d",ec);
		for (;;);
	}
    
    /*************** ANALOG-DIGITAL-CONVERTER TEST ********************/
	tiprintf( "\n\r\n\r*** AD7998 TEST ***\n\r");

    for(;;) 
	{
		for( channel = 0; channel <8; channel++)
		{
			if( Get_AD7998(channel, &value)==SUCCESS )
			{
				tiprintf (" Ch%02d=%04d",channel&7,value);
			}
			else
			{
				tiprintf (" Ch%02d= BAD",channel&7);
			}

			delayMs(10);
		} 

		tiprintf ("\n\r");

		delayMs(1000);
	}
}

I2C_ERRCODE Get_AD7998( UBYTE channel, UINT* pVal )
{
   I2C_ERRCODE ec;
   I2C_DATA tempByte;
    	
	// Start a conversion 

	// I2C start

    ec = Start();
    
    if( ec ) 
    {
		tiprintf ("\nStart Error");
		goto func_end;
    }
    
    // Slave Address

   	ec = TransmitSlaveAddressW( AD7998_1_SLAVE_ADDRESS );
 	
    if( ec ) 
    {
		tiprintf ("\nTransmit Error");
		goto func_end;
    }
 	
   	ec = TransmitData(0x80|((channel&7)<<4));
    
    if( ec ) 
    {
		tiprintf ("\nTransmit2 Error");
		goto func_end;
    }
    
    // I2C stop

  	Stop();
    
    // Read a conversion  

	ec = Start();
	
	if( ec ) goto func_end;
    
    ec = TransmitSlaveAddressR( AD7998_1_SLAVE_ADDRESS );
    
    if( ec ) goto func_end;
    
    ec = ReceiveData(&tempByte,I2C_ACK);
    
    if( ec ) goto func_end;
    
    *pVal = (tempByte&0x0F);
    *pVal <<= 8;
 	ec = ReceiveData(&tempByte,I2C_NO_ACK);
    
    if( ec ) goto func_end;
    
    *pVal |= tempByte;

func_end:

    Stop();
	
	return ec;
}