Application of AVR MCU ADC
1. Basic knowledge of digital-to-analog conversion
The digital-to-analog converter (ADC) is basically a matter of ratios, that is, the digital value produced by the ADC is related to the ratio of the input analog value to the converter range. The conversion relationship is as follows:
Vin/Vfullscale=X/(2N-1) where X is the digital output, N is the number of bits of the digital output (the number of bits of the ADC), Vin is the value of the analog input, and Vfullscale is the maximum value of the analog input.
Calculation of conversion accuracy of digital-to-analog conversion:
Vresolution=Vfullscale/((2N-1)
2. Introduction to ADC of AVR microcontroller
The ADC of the AVR microcontroller has two ADC registers: the ADC control and status register ADCSR controls the operation of the ADC; the ADC multiplexer selector ADMUX controls the 8 measured analog inputs.
In order for the ADC to operate with maximum accuracy, a clock frequency between 50HZ and 200K HZ is required, and a suitable proportional division factor is selected to obtain a conversion frequency of 50HZ to 200KHZ. Since the ADC is relatively slow, if the processor is in a waiting state while the ADC is converting data, it will waste time, so the ADC usually uses interrupt mode. ADC initialization:
(1) Set the lowest three bits of ADCSR to determine the frequency division factor
(2) Set ADIE to 1 to enable interrupt mode
(3) Set ADEN to high level to enable ADC
(4) Set ADSC to start conversion immediately
3. Application
System function: When the input voltage of analog channel 3 exceeds 3V, the system lights up the red LED; when the input voltage is less than 2V, the yellow LED is lit; when the input voltage is between 2V and 3V, the green LED is lit.
Hardware connection: The three LEDs are connected to PORTD.0, PORTD.1, and PORTD.2 of the PORD port of AT908535. The ADC peripheral of the AVR microcontroller will vary depending on the specific microcontroller used. All ADCs have noise suppression requirements for the pin voltage in the VCC connection. Some have built-in noise eliminators, while others have the ability to control Vref internally. When using ADC, you need to check the manual of the relevant microcontroller.
Program code:
#i nclude<90s9535.h>
#define LEDS PORTD
#define red 0xfe
#define yellow 0xfd
#define green 0xfc
#define uchar unsigned char
interrupt[ADC_INT] void adc_isr(void)
{
uchar adc_data; //variable for ADC result
adc_data=ADCW; //read all 10bits into variable
if(adc_data>(3*1023)/5)
LEDS=red;
else(adc_data<(2*1023)/5)
LEDS=yellow;
else
LEDS=green;
ADCSR=ADCSR|0x40; //start the next conversion
}
void main()
{
DDRD=0x07; //least signifcant 3 bits for output
ADMUX=0x03; //select to read only channel 3
ADCSR=0xCE; //ADC interrupt, 64 division
#asm("sei")
while(1); //Wait for ADC interrupt to occur
}
Previous article:Main features of AVR microcontrollers
Next article:About AVR BOD
- Popular Resources
- Popular amplifiers
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- 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)
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
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- Recommend and get a gift! Courses that you can't help but watch twice or three times
- ATK-HC05 Bluetooth Serial Port Module
- LIS3DE triaxial accelerometer package and code
- Please help analyze the power circuit
- EEWORLD University Hall - Planting the latest and hottest LED driver products from TI
- [Rawpixel RVB2601 development board trial experience] External IO port chip PCF8574 test
- Share MSP430F149 and PC serial communication programming example
- CC3200 LaunchPad Out of Box Test demos-appliances
- Test category top
- Technical article: Why does Ka-band need more bandwidth?