⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adc.c

📁 AVR DRAGON的仿真版配套源程序,包括定时器,LCD驱动,按键处理,AD转换等等.对AVR初学者甚为有用
💻 C
字号:
//***************************************************************************
//  File........: ADC.c
//  Author(s)...: ATMEL Norway
//  Target(s)...: ATmega169
//  Compiler....: AVR GCC 20060421
//  Description.: AVR Butterfly ADC routines
//  Revisions...: 1.1
//  YYYYMMDD - VER. - COMMENT                                       - SIGN.
//  20030116 - 1.0  - Created                                       - LHM
//	20060829 - 1.1  - Ported to AVR GCC                             - TMF, OL
//***************************************************************************

#include <avr/io.h>
#include "main.h"
#include "ADC.h"

/*****************************************************************************
*   Function name : ADC_init
*   Returns :       None
*   Parameters :    char input
*   Purpose :       Initialize the ADC with the selected ADC-channel
*****************************************************************************/
void ADC_init(char input)
{

    ADMUX = input;    // external AREF and ADCx
    ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);    // set ADC prescaler to , 1MHz / 8 = 125kHz

    input = ADC_read();        // dummy convertion (first convertion takes a few cycles extra
}


/*****************************************************************************
*   Function name : ADC_read
*   Returns :       int ADC
*   Parameters :    None
*   Purpose :       Do a Analog to Digital Conversion
*****************************************************************************/
int ADC_read(void)
{
    char i;
    int ADC_temp;
    int adc = 0;

    // To save power, the voltage over the LDR and the NTC is turned off when not used
    // This is done by controlling the voltage from a I/O-pin (PORTF3)

    sbi(PORTF, PORTF3);     // Enable the VCP (VC-peripheral)
    sbi(DDRF, PORTF3);

    sbi(ADCSRA, ADEN);     // Enable the ADC

    //do a dummy readout first
    ADCSRA |= (1<<ADSC);        // do single conversion
    while(!(ADCSRA & 0x10));    // wait for conversion done, ADIF flag active

    for(i=0;i<32;i++)            // do the ADC conversion 8 times for better accuracy
    {
        ADCSRA |= (1<<ADSC);        // do single conversion
        while(!(ADCSRA & 0x10));    // wait for conversion done, ADIF flag active

        ADC_temp = ADCL;            // read out ADCL register
        ADC_temp += (ADCH << 8);    // read out ADCH register

        adc += ADC_temp;      // accumulate result (32 samples) for later averaging
    }

    adc = adc >> 5;     // average the 8 samples

    cbi(PORTF, PORTF3);     // disable the VCP
    cbi(DDRF, PORTF3);

    cbi(ADCSRA, ADEN);      // disable the ADC

    return adc;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -