|
需要用两个ADC通道采样不同的电压,使用了16倍过采样来得到12位的数据。现在出的状况是不管先采样哪个通道,开始的通道正确,再采样另一个通道的数据总是错的,值会受到前一个通道的影响。
用AVR比较少,对比数据手册和示例代码没找到原因。代码如下求指教:
- typedef enum {
- ADC_CH_TEMP,
- ADC_CH_LIGHT
- } ADC_Channel_t;
- // Read ADC with oversampling to get 12bits result
- static uint16_t ADC_Read(ADC_Channel_t ch)
- {
- uint8_t i;
- uint8_t ADC_tempL;
- uint16_t ADC_var;
- if (ch == ADC_CH_TEMP) {
- // ADC2, VCC as reference
- ADMUX = (0 << REFS0) | (1 << MUX1) | (0 << MUX0);
- } else {
- // ADC3, Internal reference (1.1V)
- ADMUX = (1 << REFS0) | (1 << MUX1) | (1 << MUX0);
- }
- // Enable the ADC, 1.2MHz / 8 = 150kHz (Conversion time: 86uS)
- ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0);
- ADCSRB = 0x00;
- // do a dummy readout first
- ADCSRA |= (1 << ADSC); // do single conversion
- while (!(ADCSRA & (1 << ADIF))); // wait for conversion done, ADIF flag active
- ADCSRA &= ~(1 << ADIF); // clear ADIF flag
- ADC_var = 2;
- // do the ADC conversion 16 times for oversampling
- for (i = 16; i != 0; i--) {
- ADCSRA |= (1 << ADSC); // do single conversion
- while (!(ADCSRA & (1 << ADIF))); // wait for conversion done, ADIF flag active
- ADCSRA &= ~(1 << ADIF); // clear ADIF flag
- ADC_tempL = ADCL; // read out ADCL register
- ADC_var += (ADCH << 8) | ADC_tempL; // accumulate result
- }
- ADC_var >>= 2; // shift right to get extra 2 bits
- ADCSRA &= ~(1 << ADEN); // disable the ADC
- return ADC_var;
- }
复制代码 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|