Usually written as A/D Conversion or ADC.
Analog – Infinite number of possible values.
Digital – Finite number of possible values represented by a sequence of zeros and ones.
A transducer is a device that converts from one form of energy to another.
PORTA is the analog input port on the ATmega32.
The ADC converts the voltage on an input channel into a digital representation.
Input channels on the ATmega32 can handle voltages between 0 and 5 volts.
The resolution of an ADC is the range of input voltage divided by 2n where n is the number of bits in the digital result.
For the ATmega32, n = 10 and the voltage range is 5 volts, so the resolution is 5v/1024 = 0.00488 volts.
This means that we cannot tell the difference between voltage variations that are less than 0.00488 volts.
We can operate the ADC in 8 bit mode where our resolution is now 5v/28 = 19.4 mV.
We can calculate the digital value produced by the ADC as follows: (Digital Output)10 = (Analog Input)/resolution.
If we know the result of the ADC, we can determine the voltage on the input as follows: (Analog Input) = (Digital Output)10 * resolution.
For the ATmega32, we have 10 bits and a range of 0 to 5 volts. Each bit represents a specific contribution to the total voltage measured by the ADC:
ADCH – High byte of ADC result.
ADCL – Low byte of ADC result.
ADMUX – Analog to Digital MUltipleXer.
ADCSRA – Analog to Digital Control/Status Register for portA.
-
ADEN – Analog to Digital ENable (active high)
ADSC – Analog to Digital Start Conversion (active high)
When set, conversion begins.
When conversion is complete, the bit gets cleared.
We can poll this bit in order to determine when we can look at ADCH and ADCL.
ADATE – Analog to Digital Automatic Trigger Enable (active high)
ADIF – Analog to Digital Interrupt Flag
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 |
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)
initADC0:
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
singleADC:
.def retval = r24
sbi ADCSRA, ADSC ; Start ADC
sadcPoll:
sbic ADCSRA, ADSC ; Wait until conversion is complete
rjmp sadcPoll
in retval, ADCH ; Put 8 MSBs of ADC in r20
ret