我試圖讀取ATMega128開發板上兩個模擬紅外傳感器的值,然後通過PWM輸出縮放表示。當我將兩個PWM波連接到示波器時,它們保持相同,不應該如此。我的PWM代碼很好,所以我認爲問題在於我的代碼從ADC的通道0和1讀取。從ATMEGA128上的多個ADC通道讀取
int sens1,sens2,sens3;
volatile const uint8_t adc1 = (1<<ADLAR) | (0<<MUX0)|(0<<MUX1);
volatile const uint8_t adc2 = (1<<ADLAR) | (1<<MUX0)|(0<<MUX1);
void main(void)
{
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading
ADCSRA |= (1 << ADFR); // Set ADC to Free-Running Mode
ADCSRA |= (1 << ADEN); // Enable ADC
ADCSRA |= (1 << ADIE); // Enable ADC Interrupt
sei(); // Enable Global Interrupts
ADCSRA |= (1 << ADSC); // Start A2D Conversions
while (1)
{
}
}
ISR(ADC_vect)
{
static uint8_t firstTime = 1;
static uint8_t val;
val = ADCH*100;
if (firstTime == 1){
firstTime = 0;
}
else if (ADMUX == adc1)
{
ADMUX = adc2;
OCR3A = val;
}
else if (ADMUX == adc2)
{
ADMUX = adc1;
OCR1C = val;
}
}
代碼看起來很好看。我將驗證每個ADC通道是否給出了預期的結果,而無需在通道之間進行切換以縮小範圍。 – 2014-09-22 11:18:17
每個通道都會給出預期結果 – user27365 2014-09-22 12:24:12