[En-Nut-Discussion] A/D converter

Rich Wellner ethernut at objenv.com
Thu Jan 15 18:59:46 CET 2004


Sigurd Kleppan <sigurkl at frisurf.no> writes:

> Where do I find the Ethernut internal analog converter on the board? Which pins??
> Do someone have a simple example how I sample analog values?

Here's a snippet someone else posted that I found useful to start (for the
pins see the manual): 


----------------------------------------------
Hello,
you can try this for a starter if you use GCC
simple, non interrupt driven.

// Adcdrv.c

#include <io.h>

void ADCSetup(void)
{
// for ATMEGA128
/*
	REFS1 = 1; // Use internal 2.56V REF
	REFS0 = 1;
	ADLAR = 0;
	ADEN = 1;	// AD converter on
*/
	ADCSRA = 0x97;
	ADMUX = 0xC0;
}

int ADConvert(short chan)
{
	int tmp;
	int ADresult;
	tmp = ADMUX & ~0x07;
	tmp |= chan & 0x7;
	ADMUX = tmp;						// Set
channel
	// maybe delay a little here to settle the MUX
	sbi(ADCSRA,ADSC);
// Start ADC	
	loop_until_bit_is_set(ADCSRA, ADIF);	// Wait for ADIF, will
happen soon
	ADresult = ADCL;				// Read LSB
first
	ADresult |= ((int)ADCH) << 8;	// then MSB
	sbi(ADCSRA,ADIF);
// Clear AD Interrupt Flag

	return ADresult;
}



More information about the En-Nut-Discussion mailing list