Topic 13 - ATMega32 ADC in C (ISMAIL - SKE - UTM 2021)
Topic 13 - ATMega32 ADC in C (ISMAIL - SKE - UTM 2021)
Topic 13 - ATMega32 ADC in C (ISMAIL - SKE - UTM 2021)
ISMAIL ARIFFIN
SKE FK UTM SKUDAI JOHOR
(2017,2021)
Content
• Analogue to digital converter
• ADC in AVR, ADC Registers
• AVR’s ADC programming
• ADC Application using LM35
• ADC Problem Exercise
ADC
o Analog-to-digital converters (ADC) are among the most widely used
devices for data acquisition.
o Digital computer use binary (discrete), but physical world everything is
analog (continuous).
o Thus a device called transducer (sensor) is needed to convert physical
quantities (i.e. temperature, pressure, etc) to electrical (voltage/current)
signal.
3. Vref
• Vref is an input voltage used for reference voltage.
• The voltage connected to this pin, along with ADC resolution, will dictate
the step size.
o To select conversion time, we can select any of Fosc/2, Fosc/4, Fosc/8, Fosc/16,
Fosc/32, Fosc/64 or Fosc/128 for ADC clock.
o Fosc is the speed of the crystal frequency connected to AVR.
o For the AVR, ADC requires an input clock frequency less than 200 KHz for the
maximum accuracy.
ADCSRA Register
ADC Programming procedure using Polling
1. Make the selected ADC channel as an input pin.
2. Enable ADC since its disable upon power-on-reset to save power.
3. Select conversion speed (ADPS2:0)
4. Select voltage reference (REFS1:0) and ADC input channel (MUX4:0) in
ADMUX register.
5. Activate start conversion bit – write ‘1’ to ADSC of ADCSRA register.
6. Wait for the conversion to be completed by polling the ADIF bit of
ADCSRA register.
7. After ADIF = HIGH ‘1’, read ADCH:ADCL to get digital data output. * read
ADCL before ADCH.
8. If want to read the selected channel again, go back to step 5.
9. If want to select another Vref source or input channel, go back to step 4.
Example: Programming the ADC using Polling
Initialise ADC
The following adc_init() function initializes the ADC. Assume
CPU frequency = 16Mhz.
void adc_init()
{
ADMUX = (1<<REFS0); // Select Vref, AREF = AVcc
// ADC Enable and prescaler of 128
// Conversion speed, 16000000/128 = 125000
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}
Read ADC Value
The following adc_read() function reads the value of the ADC.
(ii) Write all the required definition and declaration header files for the
development of the C program.
(iv) Write a C code in function named as blinking() , to blink all LEDs once at the reset
time.
(v) Write a C code in function named as adc_init() to configure and enable ADC with
a prescale of clk/128 and internal 2.56 Vref.
(vi) Write the main function to continuously sample the temperature through the
LM35 sensor and display the sensor’s digital value on the LEDs.
SUMMARY
• Know general features of Analogue-to-
Digital Converter and features of ATmega32
ADC