Hi all:
我在使用MSP430FR2033时需要使用ADC测量一个电压,打算多次测量取平均值,发现采集的数据数量少于预期的数量,但单步调试是正常的,这有可能是什么问题呢?
代码如下:
1、调用:
unsigned int read_angle_value(void) { unsigned char i; unsigned char num = 10; unsigned int angle_temp = 0; //num = angle_num; // Configure ADC A9 pin SYSCFG2 |= ADCPCTL9; adc_repeat_single_channel_vcc(ADCINCH_9, 10, angle_value); for (i = 0; i < num; i++) { angle_temp = angle_temp + angle_value[i]; } angle_temp = angle_temp / num; return angle_temp; }
2、ADC初始化程序:
unsigned int *ADC_Result; unsigned char ADC_Count; unsigned int read_angle_value(void) { unsigned char i; unsigned char num = 10; unsigned int angle_temp = 0; //num = angle_num; // Configure ADC A9 pin SYSCFG2 |= ADCPCTL9; adc_repeat_single_channel_vcc(ADCINCH_9, 10, angle_value); for (i = 0; i < num; i++) { angle_temp = angle_temp + angle_value[i]; } angle_temp = angle_temp / num; return angle_temp; }
3、中断处理程序
// ADC interrupt service routine #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector=ADC_VECTOR __interrupt void ADC_ISR(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(ADC_VECTOR))) ADC_ISR (void) #else #error Compiler not supported! #endif { switch(__even_in_range(ADCIV,ADCIV_ADCIFG)) { case ADCIV_NONE: break; case ADCIV_ADCOVIFG: break; case ADCIV_ADCTOVIFG: break; case ADCIV_ADCHIIFG: ADCIFG &= ~ADCHIIFG; // Clear interrupt flag P4OUT |= BIT3 | BIT4 |BIT5; P4OUT &= ~BIT3; break; case ADCIV_ADCLOIFG: ADCIFG &= ~ADCLOIFG; // Clear interrupt flag P4OUT |= BIT3 | BIT4 |BIT5; P4OUT &= ~BIT4; break; case ADCIV_ADCINIFG: ADCIFG &= ~ADCINIFG; // Clear interrupt flag *ADC_Result = ADCMEM0; adc_window_isr(); P4OUT |= BIT3 | BIT4 |BIT5; P4OUT &= ~BIT5; //ADCCTL0 &= ~ADCENC; //__bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from LPM0 break; case ADCIV_ADCIFG: if (ADC_Count > 1) { *ADC_Result++ = ADCMEM0; ADC_Count--; break; } else { *ADC_Result++ = ADCMEM0; ADCCTL0 &= ~ADCENC; __bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from LPM0 break; } default: break; } }