Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: Atmega Analog to Digital

2005-12-11 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "nzrh2003" <roy.h@i...> wrote:
> Can anyone help with an example of seting up an A/D conversion in 
> assembler using the 5v supply as the reference.

The basics of setting up an ADC conversion are described in the AVR 
datasheet including some C and assembly language code.  Below is 
some (incomplete) code that might also be helpful, written for the 
GCC assembler.

// the channel number (0-7) should be in r24, return
// the result in r25:r24
getADC:
#define chan r24
	// check to see if the ADC is busy
	sbic	IO(ADCSRA), ADEN
	rjmp	_no_adc

	// check for a valid pin
	cpi		chan, ADC_CHAN_HI + 1
	brlo	_do_adc

_no_adc:
	// ADC busy or pin invalid, do something appropriate
        ldi     r24, 0
        ldi     r25, 0
        ret

_do_adc:
	// select AVcc and the channel
	in		r23, IO(SFIOR)
	andi	r23, ~(_BV(ADTS2) | _BV(ADTS1) | _BV(ADTS0))
	out		IO(SFIOR), r23
	ori		chan, _BV(REFS0)
	out		IO(ADMUX), chan

	// enable and select the 128 prescaler, clear the completion 
flag
	ldi		r23, _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07
	out		IO(ADCSRA), r23

	// check for a completed conversion
wait_adc:
	sbis	IO(ADCSRA), ADIF
	rjmp	wait_adc

	// conversion complete, read the result and clear the flag
	in		r24, IO(ADCL)
	in		r25, IO(ADCH)
	sbi		IO(ADCSRA), ADIF
	cbi		IO(ADCSRA), ADEN
        ret

Don
ZBasic Microcontrollers
http://www.zbasic.net

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.