AVR series ADC conversion

Publisher:DreamySunsetLatest update time:2021-06-18 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Let's sort out the ADC conversion function of AVR. The digital-to-analog converter in AVR is a 10-bit successive approximation ADC. It provides 8 single-ended input channels. The single-ended voltage is based on 0V. It also has 7 differential input channels. These seven differential channels share a common negative terminal (ADC). The input voltage range is 0~VCC. Its conversion time is 65~260uS. The phenomenon of the following program is that the serial port sends a command to start the ADC and sends the result to the PC through the serial port. Any character is turned on


#include

#include  < macros.h >

#define uchar unsigned char

 

uchar UcAdcData; //ADC conversion result  

uchar AdcConCom = 0; //ADC conversion completion flag  

uchar admuxx = 0x67; //External reference source data storage left alignment

 

//AD conversion initialization

void AdcInit(void)
{
     ADCSR = 0x00; //ADC invalid
     ADMUX = admuxx; //Select ADC channel 7
     /*--REFS1---REFS0---ADLAR---0--------------Reference power supply selection------Alignment--------*/
     /*--0000-- 0001-- 0010-- 0011-- 0100-- 0101-- 0110-- 0111-- 1110--- --1111---*/
     /*--Channel 0--Channel 1--Channel 2--Channel 3--Channel 4--Channel 5--Channel 6-Channel 7--1.23V(VBG)-0V(GND)*/
     ACSR = 0x80; //Analog comparator control and status register ACSR
                                                             //ACD is set to 1 to disable the analog comparator
    /*--ACD--ACBG--ACO--ACI---ACIE---ACIC---ACIS1---ACIS0------------------------*/
   /*-Analog comparator disable--Analog comparator energy gap reference source--Analog comparator output--Analog comparator interrupt flag*/
   /*-Analog comparator interrupt enable--Analog comparator input capture enable--Analog comparator interrupt mode selection-------- */
    ADCSR = 0x8B; //Analog comparator control and status register
    /*-----------ADEN---ADSC---ADFR---ADIF---ADIE---ADPS2---ADPS1---ADPS0--------*/
    //Enable analog-to-digital conversion ADC starts conversion ADC continuous conversion ADC interrupt enable 128 division
}

 

//AD conversion interrupt processing function

#pragma interrupt_handler adc_isr : 15
void adc_isr ( void )                 

{
     UcAdcData = ADCH; //Read ADC conversion result
     AdcConCom = 1; //Set ADC conversion completion flag
}

 

//Serial port initialization

void Uart0Init(void)
{
     UCSRB = 0x00; //Close the serial port
     UCSRA = 0x02; //Speed ​​mode M16 Chinese P151
     UCSRC = BIT(URSEL) | 0x06; //Write UCSRC register, set 8 bits
     UBRR = 0x000c; //Set the serial port baud rate to 9600
     UCSRB = 0x98; //Open the serial port
}

 

//Serial port interrupt processing function

#pragma interrupt_handler UartRecvData:12
void UartRecvData(void) //Serial port interrupt processing function, that is, PC sends a start signal to AVR
{
      uchar temp;
      temp = UDR;
      ADMUX |= 0x07;
      ADCSR |= 0x40; //Start an ADC conversion
}

 

//Serial port sends data

void UartTransmitData(void)
{
    while(!(UCSRA&0x20)); //Judge whether the data register is empty
    UDR = UcAdcData;
}

 

// Initialize various information

void InitDevices(void)
{
    CLI ( ); //Disable all interrupts
    Uart0Init ( ) ;
    AdcInit ( ) ;

    MCUCR = 0x00;
    SEI ( ) ; // Enable all interrupts
}


//Main function

void main ( void )

{

       InitDevices ( ) ; //Serial port initialization
       while ( 1 )
      {
           if ( AdcConCom ) //AD completion flag starts processing data
           {
               AdcConCom = 0; //Clear ADC conversion completion flag
               UartTransmitData ( ) ; //Send conversion results
           }
      }

}


Reference address:AVR series ADC conversion

Previous article:AVR series PWM test
Next article:Summer vacation study summary - AVR series (I)

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号