ISR ADC采样ATMEGA2560始终显示0

问题描述 投票:1回答:1

我的采样始终固定为0。我不知道为什么?!我尝试删除ISR,并将代码放入while循环中,但仍然保持不变。问题是什么,我不了解。我想在ATMEGA2560上采样TMP36。如果需要进一步澄清,请问我。我的代码是:

ISR(ADC_vect)
{
        sample = ADCH;
        display(sample,digit);// displays on seven segment
        digit++;
       if(digit>=4) digit=0;
    }


  void init_analog()
{
    sei();
    DDRK &= ~(1<<PK6);
    ADMUX |= (1<<REFS0) | (1<<MUX1) | (1<<MUX2);
    ADCSRB |= (1<<MUX5);
    ADCSRB |= (1<<ADEN) | (1<<ADATE) |(1<<ADIE) | (1<<ADPS0) | (1<<ADPS1)  | (1<<ADPS2);



}

    void convert()
{
    ADCSRA|=(1<<ADSC);    //start conversion
}




void display(uint16_t number, int digit)
{    int code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};// array of representations
    int thousands = number/1000;// stores thousands
    int hundreds= (number%1000)/100;// stores hundreds
    int tens = (number%100) /10;// stores tens
    int hey = (number%10);// stores ones

    if(digit==0)// if fragment number = 0 it shows the rightmost digit.
    {
        PORTF = ~0x08;
        spi_data(code[hey]);
    }
    else if(digit==1)// next diggit
    {
        PORTF = ~0x04; // changes the fragment to be shown
        spi_data(code[tens]); // sends the value to the spi
    }
    else if(digit==2)// next digit
    {
        PORTF = ~0x02;
        spi_data(code[hundreds]);
    }
    else if(digit==3)// leftmost ddigit
    {
        PORTF = ~0x01;
        spi_data(code[thousands]);
    } //|0x80
}

void spi_data(uint8_t n) // passes the number to the SPI.
{

    SPDR = n;
    while( !(SPSR & (1<<SPIF)) );
    L_ON;
    L_OFF;
}
c embedded avr atmega adc
1个回答
0
投票

首先,在代码中的任何地方都没有声明sample变量。

确保已声明,并且为volatile

您的代码示例中也没有main

TMP36输出750 mV @ 25°C。您已将REFS1:0中的ADMUX位设置为01,因此将AVCC(即5V电源)用作参考。

[ATmega2560具有10位ADC,这意味着当未设置ADCH中的ADLAR位时,高字节(ADMUX)只能包含值0、1、2或3。

对于0 ... 1250 mV范围内的电压,ADCH值将始终为零。

获得非零值:

  • 两者都读取ADCH:ADCL(通过简单读取C中的ADC)。您不能只读取ADCL,因为它会冻结ADC寄存器,直到读取ADCH
  • 或设置ADLAR中的ADMUX位使结果左移6位,然后您可以单独使用ADCH
© www.soinside.com 2019 - 2024. All rights reserved.