Como usar o ADC embutido do microcontrolador AVR (ATmega16) – (Parte 26/46)

How to use the built-in ADC of the AVR microcontroller (ATmega16) – (Part 26/46)

The microcontroller only understands digital language. However, the inputs available from the environment to the microcontroller are mostly analog in nature, that is, they vary continuously over time. To understand the inputs of the digital processor, a device called an Analog to Digital Converter (ADC) is used. As the name suggests, this peripheral gathers the analog information provided by the environment and converts it into the controller's understandable digital format, the microcontroller then processes the information and provides the desired result at the output.

((wysiwyg_imageupload:451:))

ATmega16 has an integrated 10-bit, 8-channel ADC system. Some of the basic features of the Armega16 ADC are:
· 8 channels.
· 10-bit resolution.
· Input voltage range from 0 to Vcc.
· 2.56V selectable internal reference voltage source.
· AREF pin for external reference voltage.
· Complete interruption of ADC conversion.
The ADC channels on the Atmega16 are PORTA multiplexed and use the common pins (pin33 to pin40) with PORTA. The Atmega16 microcontroller ADC system consists of the following pins:
I. ADC0-ADC7: 8 channels from pin 40 to pin 33 of the Atmega16 ADC peripheral.
ii. AREF: Pin32 of Atmega16 microcontroller, the voltage at AREF pin acts as reference voltage for ADC conversion, the reference voltage is always less than or equal to the supply voltage i.e. Vcc.
iii. AVCC: Pin30, this pin is the supply voltage pin for using PORTA and ADC; The AVCC pin must be connected to Vcc (microcontroller supply voltage) to use PORTA and ADC.
Note: External reference voltage source can be used on the AREF pin. However, the Atmega16 also has internal reference voltage options of 2.56V and Vref = Vcc.
The figure below shows the pin configuration of the Atmega16 microcontroller ADC system.
Números de pinos do ADC no microcontrolador ATmega16 AVR
Fig. 2: ADC pin numbers on ATmega16 AVR microcontroller
ADC Records
To use the Atmega16 ADC peripheral, certain registers need to be configured.
I. ADMUX (ADC Multiplexer and Select Register)
Valores de bits do multiplexador ADC e registro de seleção para configurar o periférico ADC no AVR
Fig. 3: ADC multiplexer bit values ​​and select register for configuring the ADC peripheral in the AVR
REF(0:1) bits determine the reference voltage source, whether internal or external connected to the AREF pin. The MUX (4:0) bits are used to select between channels that will provide data to the ADC for conversion. The ADLAR bit when set to 1 gives the left-adjusted result in the ADCH and ADCL data registers.
ii. ADCSRA (ADC Control and Status Record)
Configuração de bits do controle ADC e registro de status no microcontrolador AVR
Fig. 4: ADC control bit configuration and status register in the AVR microcontroller
ÁDEN: ADC enable bit, this bit must be set to 1 to turn on the ADC.
ADSC: ADC Initial Conversion Bit, this bit is set to 1 to start the ADC conversion, once the conversion is complete this bit is set back to 0 by the hardware.
A FIND: ADC Auto Trigger Enable, this bit is set to 1 to enable automatic triggering of the ADC conversion.
ADIF: ADC interrupt flag, this bit is set to 1 when ADC conversion is completed.
ADIE: ADC Interrupt Enable, this bit is set to 1 if we want to enable complete interrupt of ADC conversion.
ADPS(0:2): ADC Prescaler Bits, these bits are used to set the clock frequency of the ADC, the setting of these bits determines the division factor by which the microcontroller clock frequency is divided to get the frequency of the ADC clock. The figure above shows the prescaler bit values ​​for the respective division factor.
Equação da frequência do relógio ADC
Fig. 5: ADC clock frequency equation
The ADC clock frequency must be between 50 KHz and 200 KHz.
iii. ADCH and ADCL (ADC Data Records)
When the ADC conversion is complete, the data is stored in these two registers. The data configuration depends on the value of the ADLAR bit in the ADMUX register. If ADLAR=0, the data is right-fitted and if ADLAR=1, the data is left-fitted. Always read the ADCL first and then the ADCH. In cases where 8-bit precision is sufficient, set the ADLAR bit to 1 to left-shift the data and read only the ADCH data register.
When ADLAR = 0,
Configuração de bits do registro de dados ADC em ADLAR = 0 no microcontrolador ATmega16 AVR
Fig. 6: ADC data register bit setting at ADLAR = 0 on ATmega16 AVR microcontroller
When ADLAR = 1,
Configuração de bits do registro de dados ADC em ADLAR = 1 no microcontrolador ATmega16 AVR
Fig. 7: ADC data register bit setting at ADLAR = 1 on ATmega16 AVR microcontroller
Circuit Description
Connect the circuit as shown in the circuit diagram. A ceramic capacitor 104 is connected between AVcc (pin 30) and Aref (pin 32). AVcc (pin 30) is connected to +5V external power.
Code Explanation
To interface the analog device with the AVR microcontroller, follow the steps below to program it.

Step 1: To initialize the ADC
I. Set the value in the ADMUX register according to the ADC channel and reference voltage.
ii. Set the Prescaler bits appropriately in the ADCSRA register.
iii. Set the ADEN bit to enable the ADC.


 empty ADC_init(empty) // ADC initialization

{ ADMUX=(1< // AVcc with external capacitor in AREF ADCSRA=(1< // Enables the ADC and sets the Prescaler division factor to 128 }
Step 2: To read the analog value
I. Enter the channel value in ADMUX
ii. Start the conversion by setting the ADSC bit.
iii. Monitor the ADIF bit for conversion completion.
4. Clear the ADIF conversion bit. When writing 1.
v. The digitally converted result is now available in the ADCH and ADCL registers.


 unsigned int ADC_read(unsigned char ch)
 {
 ch= ch&0b00000111; // the channel must be black and white from 0 to 7
 ADMUX = channel; //selecting channel
 
ADCSRA =(1< //start conversion
 while(!(ADCSRA & (1< // waiting for ADIF, conversion complete
 ADCSRA =(1< // ADIF cleaning, is done by writing 1 to it
 
return (ADC);
 }

Project source code

###


 //Program for the ADC to read from channel 0 and show 8-bit o/p in PORTB
#include #include void ADC_init(void); unsigned int ADC_read (unsigned char); // ------------------------------------------------ int main(empty) { unsigned internal value; DDRB=0xFF; DDR=0x03; ADC_init ; // ADC initialization //ch=0; while(1) { value=ADC_read(0); PORTB=value; _delay_ms(500); } } //------------------------------------------------ empty ADC_init(empty) // ADC initialization { ADMUX=(1< // AVcc with external capacitor in AREF ADCSRA=(1< // Enables the ADC and sets the Prescaler division factor to 128 } unsigned int ADC_read(unsigned char ch) { ch= ch&0b00000111; // the channel must be black and white from 0 to 7 ADMUX = channel; //selecting channel ADCSRA =(1< //start conversion while(!(ADCSRA & (1< // waiting for ADIF, conversion complete ADCSRA =(1< // ADIF cleaning, is done by writing 1 to it return (ADC); }

###

Project Components

  • ATmega16
  • LED

Project video

Conteúdo Relacionado

What is the role of automotive sensors in modern vehicles?
A network of sensors is embedded in every vehicle,...
How to choose an e-bike controller
The motor controller is one of the most important...
How to Troubleshoot Common ESP32-CAM Problems
ESP32-CAM is a compact camera module that combines the...
A guide to USB standards from 1.0 to USB4
A evolução dos padrões USB foi fundamental para moldar...
Schurter aprimora série de seletores de tensão com revestimento prateado
A SCHURTER anuncia um aprimoramento para sua conhecida série...
A interface serial PCI fornece conectividade confiável em ambientes extremos
A Sealevel Systems anuncia o lançamento da Interface Serial...
STMicroelectronics expande portfólio de conversão de energia com diodos Trench Schottky de 100 V
A STMicroelectronics introduziu Diodos retificadores Schottky de trincheira de...
O que são Sistemas Globais de Navegação por Satélite (GNSS) e como são usados?
Determinar uma localização precisa é necessário em várias indústrias...
O conversor GaN de 50 W da STMicroelectronics permite projetos de energia de alta eficiência
O novo VIPerGaN50 da STMicroelectronics simplifica a construção de...
Samsung e Red Hat farão parceria em software de memória de próxima geração
A Samsung Electronics, fornecedora de tecnologia de memória avançada,...
Primeiro MPU single-core com interface de câmera MIPI CSI-2 e áudio
O mercado embarcado tem uma necessidade de soluções de...
Fluency decoding in induction motor for better performance
You have probably come across the term ' drag...
Understanding Support Insulators in Electrical Systems
You probably have a support insulator if you've noticed...
Explore Stay Insulator – safety, installation and maintenance
You've probably seen stand an insulator sit on power...
More safety and efficiency with the shackle isolator
You've probably seen shackle insulators enthroned on electricity poles,...
Discover the power of latching relays for energy efficiency and more
You have probably experienced situations where controlling a circuit...
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.