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 ************************************************************/
Previous article:ATmega16 Development Board Tutorial (9) - KeyPad
Next article:ATmega16 Development Board Tutorial (7) - LCD1602
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- PCB panelization
- RPG game ported using RP2040 and MicroPython
- For large project files containing nios, timing constraints, and layout and routing, modify the internal module names and some variable names.
- What is output impedance? What is impedance matching?
- High-frequency transformer sandwich winding method: primary wrapped in secondary? Or secondary wrapped in primary?
- Detailed description of I2C bus protocol and timing, getting started is no longer difficult
- LM1875 Op Amp
- About the Minimum System
- Magic online translation
- Quick Start on RISC-V Architecture and Embedded Development