ATmega16 Development Board Tutorial (8) - ADC

Publisher:TapirLatest update time:2022-01-05 Source: eefocusKeywords:ATmega16 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

9. ADC

Before we talk about ADC, let's talk about its cause and effect. Since computers widely use digital electronic technology in automatic control, detection and other fields, and the signals of nature are all analog signals, how to save natural signals through computers? Then we need ADC (Analog-Digital Converter) to convert analog signals into digital signals so that they can be stored in computers.


So how does ADC convert?


First, sample the analog signal at a selected moment;


Then convert the value into a digital quantity;


lFinally, convert according to a certain encoding format.

—>


An analog signal cannot be sampled at one point, but is composed of many points. When the rectangle is narrow and long and almost close to the vertical line, the previous waveform can be restored well.


In official terms: Sampling Theorem - if the maximum frequency of the original image is fmax, then the sampling frequency must be at least twice the maximum frequency of the original image, so that the original waveform can be restored accurately.

As shown in the figure, 0-1V is a continuous voltage quantity, and 0-0.125V is regarded as 0△, so there will be a minimum error △; then it is converted through a certain encoding method, such as binary, octal, hexadecimal, etc.


As for the ADC conversion process, you can learn more about it through the introduction on the Internet. Now let's take a look at the characteristics of AVR's ADC and how to operate it.


The ATmega16 microcontroller has its own ADC, so no external ADC chip is needed for conversion. Its main features are:


l10-bit successive approximation ADC, ±2LSB absolute accuracy


l65-260us conversion time


l Sampling rate up to 15kSPS at highest resolution


8 multiplexed single-ended input channels


ADC input voltage range of l0-AVcc, including internal 2.56V reference voltage


lSupport continuous conversion or word conversion mode


lCan trigger ADC interrupt


Let’s take a look at the ADC timing diagram - continuous conversion

It can be found that the first 13 clock cycles are not collected, mainly to initialize the analog circuit. The conversion result of ADC is - ADC = Vin*1024/Vref, Vin is the input voltage of the ADC channel, and Vref is the reference voltage.


Let's start with the main registers of ADC:


ADMUX (multiplex selection register):

Bits 6-7: Used to set whether the reference voltage is internal, external, or both, as shown in the following figure

Bit 5: ADC conversion result is left-aligned, which mainly affects the storage format of ADC value in register;


Bits 0-4: used to select ADC channel

This example uses an external AVcc reference voltage and channel 0, right-aligned, so ADMUX = 0x00.


ADCSRA (Control and Status Register):

Bit 7: ADC enable bit;


Bit 6: ADC starts converting bit;


5 bit: ADC automatic triggering is enabled, automatic triggering is started, and the rising edge of the trigger signal drives the conversion;


Bit 4: ADC interrupt flag. After the ADC conversion is completed, ADIF is set and needs to be cleared by software.


Bits 0-2: ADC prescaler selection.

Finally, there is an old friend - SFIOR register (sets whether IO is pulled up)

Bits 5-7: The settings of these 3 bits are valid only when the ADATE bit in ADCSRA is set to 1. The selected interrupt triggers the ADC conversion on the rising edge (this function is not performed in this case example, please check it out).

Now let's start the code explanation:

Initialize ADC, select channel, divide by 128, and enable.

Collect the ADC value, start the conversion, wait for the conversion to be completed, clear the flag, and get the ADC value.


Finally, the collected value can be converted into voltage through the formula.


Simulation video


/**

******************************************************************************

* @file main.c

* @author Alex——Xiaobai

* @version V1.0

* @date 2021.1.20

* @brief header file call, type redefinition

* @store Proteus simulation store——Official account

* @Link https://shop484534014.taobao.com/

* @Email 844545015@qq.com

******************************************************************************

* @attention All Rights Reserved  

**/

#include

/*

The step value of Touch Pad is 0.5V, i.e. 10 levels.

Here the voltage is amplified 100 times to facilitate data processing

*/

void main(void)

{  

    uint16_t adcValue = 0;

    uint16_t vol = 0;

    uint8_t cnt = 0;

    DDRB_DDB3 = 1;    

    AD.ADC_Init(0); //ADC channel 0

    LCD.LCD1602_Init();    

    LCD.LCD1602_WriteStr(0,2,"ADC:");  

    LCD.LCD1602_WriteStr(1,2,"Vol:");

    Delay.Delay_ms(1000);

    while(1)

    { 

        adcValue = AD.ADC_GetValue(); //ADC data acquisition

        vol = (uint16_t)(adcValue * 5.0 * 100 / 1024); //Magnify 100 times        

        LCD.LCD1602_WriteChar(0,8,adcValue/1000 + '0');

        LCD.LCD1602_WriteChar(0,9,adcValue%1000/100 + '0');

        LCD.LCD1602_WriteChar(0,10,adcValue%1000%100/10 + '0');

        LCD.LCD1602_WriteChar(0,11,adcValue%1000%100%10 + '0');

        LCD.LCD1602_WriteChar(1,8,vol/100 + '0');

        LCD.LCD1602_WriteChar(1,9,'.');

        LCD.LCD1602_WriteChar(1,10,vol%100/10 + '0');

        LCD.LCD1602_WriteChar(1,11,vol%100%10 + '0'); 

        

        if(vol > 250) //When the actual voltage value > 2.5V, the buzzer will sound an alarm

        {

            for(cnt = 0;cnt < 80;cnt++)

            {

                PORTB_PORTB3 = 0;

                Delay.Delay_us(2);

                PORTB_PORTB3 = 1;

                Delay.Delay_us(2);

            }

        }

    }

}

/********************************************************

  End Of File

************************************************************/

Keywords:ATmega16 Reference address:ATmega16 Development Board Tutorial (8) - ADC

Previous article:ATmega16 Development Board Tutorial (9) - KeyPad
Next article:ATmega16 Development Board Tutorial (7) - LCD1602

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号