All pastes #833466 Raw Edit

Unnamed

public text v1 · immutable
#833466 ·published 2007-12-27 07:47 UTC
rendered paste body
	a2d_init();
           .....

	// sample a2d channel repeatldly
	while(1) {
		led_on(1);
		i = a2d_read0();
		led_off(1); led_on(2);
		printf("%d\r\n",i);
		led_off(2);
		delay_ms(50);
	}

void a2d_init(void) {
	// Vref = AVCC; ADC  right adjusted 
	ADMUX = _BV(REFS0); 
	// enable ADC, Fosc/64
	ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1);
}

uint16_t a2d_read0(void) {
	uint16_t out;
	ADMUX =_BV(REFS0) ;
	ADCSRA |= _BV(ADSC); // start conversion
	while (ADCSRA & _BV(ADSC)); // wait for completion of conversion
	out = (ADCH<<8|ADCL);
	return out;

}