Hello,
I cannot get multiple ADC channel reads to work for me. If I only read one channel, the individual channel reads everything properly, but if I read multiple things go off the rails.
Here is how I initialize the ADC:
SIM_SCGC |= SIM_SCGC_ADC_MASK;
ADC_SC1 = 0;
ADC_SC1|= ADC_SC1_ADCO_MASK; /* Continuous mode operation */
ADC_SC2 |=ADC_SC2_REFSEL(0b01); /* Select VDD and VSS as voltage reference source*/
ADC_SC3 |= ADC_SC3_MODE(TWELVE_BIT) | 0x30; /* 8,10,12 bit mode operation */
ADC_APCTL1 |= 0x3C; /* Channel selection */
This is the subroutine to read the ADC channel:
UINT16 ADC_Read(UINT8 channel)
{
if (channel == 2){
ADC_SC1 |= 0b00000010;
}
else if (channel == 3){
ADC_SC1 |= 0b00000011;
}
else if (channel == 4){
ADC_SC1 |= 0b00000100;
}
else if (channel == 5){
ADC_SC1 |= 0b00000101;
}
while(!(ADC_SC1 & ADC_SC1_COCO_MASK)); /* Wait conversion to complete */
return ADC_R & 0x0FFF; /* Return adc value */
}
Then I go ahead and read each channel using above subroutine:
adc_val2 = ADC_Read(2);
adc_val3 = ADC_Read(3);
adc_val4 = ADC_Read(4);
adc_val5 = ADC_Read(5);
I can comment out all but any one desired channel, and I can transmit the correct ADC value via UART, but if I use more than one channel, the read fails. It appears it is showing only the last read channel or not... Weird things happen!
Any advice?
Solved! Go to Solution.
Hi,
Can you change the code as Following? This is my guess, anyway, pls have a try.
BR
Xiangjun Rong
ADC_SC1&=~(0x1F); //clear the ADCH bits
ADC_SC1&=~(0x1F); //clear the ADCH bits
ADC_SC1&=~(0x1F); //clear the ADCH bits
ADC_SC1&=~(0x1F); //clear the ADCH bits
ADC_SC1 |= 0b00000101; } while(!(ADC_SC1 & ADC_SC1_COCO_MASK)); /* Wait conversion to complete */ return ADC_R & 0x0FFF; /* Return adc value */ }
Hi,
Can you change the code as Following? This is my guess, anyway, pls have a try.
BR
Xiangjun Rong
ADC_SC1&=~(0x1F); //clear the ADCH bits
ADC_SC1&=~(0x1F); //clear the ADCH bits
ADC_SC1&=~(0x1F); //clear the ADCH bits
ADC_SC1&=~(0x1F); //clear the ADCH bits
ADC_SC1 |= 0b00000101; } while(!(ADC_SC1 & ADC_SC1_COCO_MASK)); /* Wait conversion to complete */ return ADC_R & 0x0FFF; /* Return adc value */ }
Magic! It works. Thanks for your help.