Tuesday, June 2, 2020

Introduction to analog to digital converter of ATMega32

Introduction

ATMega32 uses a successive approximation ADC type a device analog inputs. It's 10-bit wise and has up to 8 input channels. In addition, it can handle up to 16 differential voltage input combinations.

Introduction to analog to digital converter of ATMega32
Eight analog input channel of ATMega32

Eight analog channels are multiplexed with PortA digital pins. By default PortA is digital I/O. AVCC is supply voltage for ADC module.  AREF pin is a positive analog voltage reference. But the voltage reference could be programmed to internally connected to a 2.56 V reference voltage.

The ADC clock source is driven from the MCU clock with the division factor from 2 to 128. For the 10-bit ADC reading result, the clock frequency work 50 kHz to 200 kHz. But only an 8-bit result is needed the clock frequency could take higher than 200 kHz.

Basically, to use ADC we have to deal with some registers.
  • ADMUX
Introduction to analog to digital converter of ATMega32

Reference selection register REFS[1:0], used for selecting the reference voltage. By default it's AREF pin. For more reference voltage selections, check the device datasheet.

ADC left adjust result ADLR, used for adjusting the 10-bit ADC result between right and left. By default it's right justified ('0').

Analog channel and gain selection bits MUX[3:0], used for selecting between any single ended input, differential input and gain setting. By default, it's single ended ADC0 input channel.
  • ADCSRA
Introduction to analog to digital converter of ATMega32

ADC enable ADEN, turn on the ADC module when it's set to '1'.

ADC start conversion ADSC, start the ADC conversion when it's set to '1'. This bit is also  used for testing the progress of the conversion. When it's read '0', the conversion is completed.

ADC auto trigger enable ADATE, make the ADC conversion automatically start by any options (see the datasheet).

ADC interrupt flag ADIF, is set to '1' when the ADC conversion is completed.

ADC interrupt enable ADIE, when set to '1' the interrupt for ADC is enabled.

ADC prescaler select bits ADPS[2:0], used for select the clock source for ADC. it's driven from MCU clock with the scale factor between 2 to 128. By default it has a division factor of 2.
  • ADCL and ADCH
Introduction to analog to digital converter of ATMega32

The data register is a 10-bit pair of two 8-bit registers ADCL and ADCH. It's readable and writable. By default ADLAR = '0', the result is right-justified. 
Introduction to analog to digital converter of ATMega32
Introduction to analog to digital converter of ATMega32

Using the ADC

Basically, to program the ADC we must do the following steps:
  1. set the preferred pin to input
  2. in the ADMUX register, set the reference voltage, set up the result arrangement and select the analog input channel.
  3. in the ADCSRA register, turn on the ADC by setting ADEN to '1'. Selecting the ADC clock prescaler by setting any value to ADPS[1:0]
  4. start the conversion by setting the ADSC of the ADCSRA to '1'.
  5. wait for completion by checking the ADSC bit become low, or waiting for ADIF is activated.
  6. get the result from ADCL and ADCH.
In this simple introduction of using the ADC, I use ADC0 an analog input channel with single end mode. The ADC clock is driven from the MCU clock with the division of 2. PortC displays the ADCH while PortD displays the ADCL result.

Introduction to analog to digital converter of ATMega32
Schematic diagram. ATMega32 run at 4 MHz reading the analog input from ADC0.

Source code:

#include <avr/io.h>
int main(void)
{
    //PortC as output
 DDRC=0xFF;
 //PortD as output
 DDRD=0xFF;
 //PA0 or ADC0 as an analog input
 DDRA=0;
 //Turn on the ADC module
 ADCSRA|=(1<<ADEN);
    while (1) 
    {
  //Start the conversion
  ADCSRA|=(1<<ADSC);
  //Wait for the completion
  while((ADCSRA&(1<<ADSC))==1);
  //Read the result
  PORTD=ADCL;
  PORTC=ADCH;
    }
}

Introduction to analog to digital converter of ATMega32
A screen shot simulation

Click here to download its source file. Back to main tutorial page ATMega32 tutorials in C with Atmel Studio 7.



No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)