ADC application of avr microcontroller

Publisher:未来感知Latest update time:2016-10-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Basic knowledge of digital-to-analog conversion  
      The digital-to-analog converter (ADC) is basically a matter of ratio, that is, the digital value generated 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.
ADEN
ADFR
ADIF
CHICKEN
ADPS2
ADPS1
ADPS0
                                                        Table 2-1 ADCSR bit definition
   Bit
                        illustrate
ADEN
   Setting the ADC support bit to 1 enables the ADC
ADSC
   ADC starts conversion bit. Set to 1 to start the first rotation.
ADFR
  ADC free mode selection bit, set to 1 to enable free mode
ADIF
  ADC interrupt flag
CHICKEN
  ADC interrupt mask bit, set to 1 to allow an interrupt at the end of each conversion
ADPS2
  ADC scale factor selection bits
ADPS1
  ADC scale factor selection bits
ADPS0
  ADC scale factor selection bits
  
  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.
  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
}
Reference address:ADC application of avr microcontroller

Previous article:avr keyboard program
Next article:AVR microcontroller eeprom byte read and write and block read and write program examples

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号