ATmega328p 模数转换无响应

问题描述

以下代码不会更新初始 adcValue。 LED 在不同的程序下正常工作,并且在给定初始 adcValue 的情况下它们也正常工作。他们不响应电位器调整。延迟只是为了让它变慢,没有它它也不起作用。

#define F_CPU   1000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

double adcValue = 700;

void startConversion()
{
    ADCSRA |= (1 << ADSC);
}        
void setupADC()
{
    // AVcc with external capacitor as reference,ADC5 as input
    ADMUX = (1 << REFS0) | (1 << MUX0) | (1 << MUX2);
    // Enabling ADC,acticating interrupts,setting prescaler to 8
    ADCSRA = (1 << ADEN) | (1 << ADIE) | (1 << ADPS0) | (1 << ADPS1) ;
    //disabling digital input buffer
    DIDR0 = (1 << ADC5D);       
    startConversion();
}            
ISR(ADC_vect)
{       
    adcValue = ADC;     
}    
int main(void)
{
    DDRB = 0b00111111;          
    setupADC();     
    sei();          
    while(1)
    {
        _delay_ms(500);
        if( adcValue < 400)
        {
            PORTB = 0b00000000;
        } 
        else if ( adcValue < 800)
        {
            PORTB = 0b00000011;
        } 
        else 
        {
            PORTB = 0b00000111;
        }

    startConversion();
}

}

解决方案:

现在可以了

volatile double adcValue = 700;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)