Analog to Digital Conversion

A Dr. Taylor Tutorial

I/O Registers Needed

       sbi   ADCSRA, ADIF  ; clears the AD interrupt flag
ADTS2 ADTS1 ADTS0 Mode of operation
0 0 0 Free running
0 0 1 Analog comparator
0 1 0 External Interrupt Request 0
0 1 1 Timer/Counter0 Compare Match
1 0 0 Timer/Counter0 Overflow
1 0 1 Timer/Counter Compare Match B
1 1 0 Timer/Counter1 Overflow
1 1 1 Timer/Counter1 Capture Event

Sample Code

The following function uses the ADC in polling mode (does one conversion and stores the result when the conversion has completed).

; Initialize ADC for single ended conversion on ADC channel 0 (PA0)
init_adc0:
      push  r16
      ; Set ADC reference voltage, put in 8 bit mode and select channel 0
      ldi   r16, 0b01100000  ; REFS0 = 1, ADLAR=1 (left adjust result), select ADC0
      out   ADMUX, r16

      ; Enable ADC and divide clock speed by 128
      ldi   r16, 0b10000111  ; ADEN = 1, Prescale by factor of 128
      out   ADCSRA, r16

      pop   r16
      ret

; Perform a single ADC
; Assume ADC has been initialized for single ended input on the relevant channel
;  with left adjust result set
; Returns result in r24
single_adc:
      sbi   ADCSRA, ADSC   ; Start ADC
sadcPoll:
      sbic  ADCSRA, ADSC   ; Wait until conversion is complete
      rjmp  sadcPoll
      in    r24, ADCH      ; Put 8 MSBs of ADC in 
      ret

Tutorials for Other Courses

Additional Links

Site Mirror

This is a mirror to be used when http://myweb.msoe.edu/taylor/, (EDU instead of US) is not available.