Vin[V] = (ADCH*256+ADCL) * Vref[V]/1024
Vin[V] = ADCH * Vref[V]/256
void init_adc(void) { /* Měříme na portu ADC0, připravíme si jen ten. */ PORTC &= ~_BV(PC0); DDRC &= ~_BV(PC0); /** Choose clock and start ADC */ ADCSRA |= _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0) | _BV(ADSC); /* wait for the value */ loop_until_bit_is_clear(ADCSRA, ADSC); } voltage_t measure_voltage() { voltage_t result; /* ADC0/PC0 přepneme na vstupní, pro jistotu. */ PORTC &= ~_BV(PC0); DDRC &= ~_BV(PC0); ADMUX = 0b11000000; /* 2.56V Rref, noADLAR, select ADC0 */ ADCSRA |= _BV(ADEN) | _BV(ADSC); /* Start Conversion */ loop_until_bit_is_clear(ADCSRA, ADSC); result = (voltage_t) ADCL; result |= (((voltage_t) ADCH) <<8); ADCSRA &= ~_BV(ADEN); /* Turn of ADC */ return result; }