Analog Digital Conversion using DAQ-2543


Demonstrates interfacing 8051 to Texas Instruments 12-bit Analog To Digital Converter (ADC): TLC2543

Toolkit:SDCC 8051 Development System

Location:/bipom/devtools/SDCC/examples/ADC/tlc2543/

Code Example


/*-----------DEFINES-----------------------------------------------------*/


#define ADCEOC          P1_4        /* End of Conversion for ADC */
#define ADCCLOCK        P1_2        /* Clock signal for ADC */
#define ADCDIN          P1_0        /* Data in ( to ADC ) */
#define ADCDOUT         P1_1        /* Data out ( from ADC ) */
#define ADCCS           P1_3        /* Chip Select ( to ADC ) */


unsigned short Convert( char channel );


main()
{
    unsigned int value;

    /* Set the serial port to 19200 Baud */
    serinit(CBR_19200);
startTests:
    puts( "\rPlease press a key to start" );
    /* Loop forever until user presses a key */
    getchar();
    /*************** ANALOG-DIGITAL-CONVERTER TEST ********************/

    /* Read ADC internal diags */

    /* Zero point */
    value = Convert( 0xC0 );

    if( value > 0 )
    {
    	puts( "\rError ADC zero point, press a key" );
    	 printf( "\rZero=%d", value);
	    getchar();
    }

    /* Middle point */
    value = Convert( 0xb0 );

    if( value > 2048 || value < 2047 )
    {
	    puts( "\rError ADC middle point, press a key" );
	    printf( "\rMiddle=%d", value);
	    getchar();
    }

    /* Full scale point */
    value = Convert( 0xd0 );

    if( value < 4095 )
    {
	    puts( "\rError ADC full scale, press a key" );
	    printf( "\rFull=%d", value);
	    getchar();
    }

    /* Read all ADC channels */
    puts( "\rReading ADC channels, press a key to stop..." );
    
    for( ;; )
    {
	    printf( "\rCh0=%d", Convert( 0x0 ) );
	    printf( " Ch1=%d", Convert( 0x10 ) );
	    printf( " Ch2=%d", Convert( 0x20 ) );
	    printf( " Ch3=%d", Convert( 0x30 ) );
	    printf( " Ch4=%d", Convert( 0x40 ) );
	    printf( " Ch5=%d", Convert( 0x50 ) );
	    printf( " Ch6=%d", Convert( 0x60 ) );
	    printf( " Ch7=%d", Convert( 0x70 ) );
	    printf( " Ch8=%d", Convert( 0x80 ) );
	    printf( " Ch9=%d", Convert( 0x90 ) );
	    printf( " Ch10=%d", Convert( 0xa0 ) );

	    /* User pressed a key to exit */
	    if( chkch() != 0 )  break;
    }

    goto startTests;    

}


/*********************************************************************
*                                                                   
*      SetCS - Turn off Analog To Digital Converter by setting Chip Select high.
*
*      Inputs:  none
*
*      Outputs -  to ADC
*
*      Returns -  nothing
*                                                                                       
*********************************************************************/
void setCS()
{
	P1_3 = 1;
}


/*********************************************************************
*                                                                   
*      resetCS - Turn on Analog To Digital Converter by resetting Chip Select low.
*
*      Inputs:  none
*
*      Outputs -  to ADC
*
*      Returns -  nothing
*                                                                                       
*********************************************************************/
void resetCS()
{
	P1_3 = 0;
}


/*********************************************************************
*
*      Convert - Performs analog to digital conversion on the specified channel
*
*      Inputs  -  channel number
*
*                 Possible values:
*
*                   0x0     - Channel 1
*                   0x10    - Channel 2
*                   0x20    - Channel 3
*                   0x30    - Channel 4
*                   0x40    - Channel 5
*                   0x50    - Channel 6
*                   0x60    - Channel 7
*                   0x70    - Channel 8
*                   0x80    - Channel 9
*                   0x90    - Channel 10
*                   0xA0    - Channel 11
*                   0xB0    - Internal channel for diagnostics ( mid point )
*                   0xC0    - Internal channel for diagnostics ( zero point )
*                   0xD0    - Internal channel for diagnostics ( full scale point )
*                   0xE0    - Power down
*
*
*********************************************************************/
unsigned short Convert( char channel )
{
	unsigned char i;
	unsigned short value;
	
	ADCDOUT  = 1; // Configure input	

	ADCCLOCK = 0;

	// delay a little bit

	
	resetCS();
	
	// delay a little bit


	for( i=0; i<8; i++ )
	{
		if( channel & 0x80 )
			ADCDIN = 1;
		else
			ADCDIN = 0;				
			
		ADCCLOCK = 1;
		
		// little delay 

		
		ADCCLOCK = 0;
		
		channel = channel << 1;
	}

	ADCDIN = 0;

	// Dummy 4 bits	

	for( i=0; i<4; i++ )
	{
		ADCCLOCK = 1;
		
		// little delay 

		
		ADCCLOCK = 0;
	}

	// Wait until end of conversion

	while( !ADCEOC );						

	// Now read 12 bits of value from ADC

	value = 0;
		
	for( i=0; i<12; i++ )
	{
		value <<= 1;
		value |= ADCDOUT;
	
		
		ADCCLOCK = 1;
		
		// little delay 

		
		ADCCLOCK = 0;
	}

	setCS();
	
	return value;
}