Using MAXIM 12-bit DAC


This program generates varying voltages using a MAXIM 12-bit Digital to Analog Converter (DAC)

Toolkit:SDCC 8051 Development System

Location:/bipom/devtools/SDCC/examples/dac/

Code Example


/* 8051 port pins allocated to DAC; change these as necessary 
 * when uing other port pins.
 */
#define DAC_CS			P3_3
#define DAC_CLK			P2_2
#define DAC_DIN			P2_0
                


void WriteDAC( unsigned value );


void main()
{
	unsigned i;
	
	/* Generate a continuous sawtooth ( ramp ) signal by varying 
	 * DAC output from 0 Volts to 2.5 Volts
	 */
	for( i=0; i<4096; i++ )
	{
		WriteDAC(i);
	}
}


/***************************************************************************
; Function: WriteDAC
;
; Description: Generates a voltage at DAC's output based on the given integer value
;
; Inputs:    unsigned - 12-bit value ( from 0 to 4095 )
;
; Returns:   nothing
;**************************************************************************/
void WriteDAC( unsigned value )
{
	unsigned char i;
	
	/* Set DAC Chip Select high to disable DAC */	
	DAC_CS = 1;
	
	/* Now enable DAC; DAC is ready to use */	
	DAC_CS = 1;
	
	/* Reset DAC clock to start */
	DAC_CLK = 1;

	/* Program the voltage value as a 16-bit integer into DAC. This value
	 * is sent serially, one bit at a time, to DAC. DAC uses the lower 
	 * 12 bits of the value.
	 */
	for( i=0; i<16; i++ )
	{
		DAC_CLK = 0;

		/* Most significant bit of the 16-bit value is the current data bit */
		if( value & 0x8000 )
		{
			/* Set data input of DAC if current data bit is set */
			DAC_DIN = 1;
		}
		else
		{
			/* Reset data input of DAC if current data bit is reset */
			DAC_DIN = 0;			
		}
		
		/* On this rising edge of the clock, current data bit is accepted by DAC */
		DAC_CLK = 1;
		
		/* Shift the value to retrieve the next data bit */
		value = value<<1;
	}

	/* Reset DAC clock because we are finished */	
	DAC_CLK = 1;
	
	/* Disable the DAC before exiting */
	DAC_CS = 1;			
}