我想采集10次AD测量结果,然后在取平均值。我现在选用的单通道多次采样,采样结束ADC12CTL0&=~ADC12SC;但是当进入采样中断后就出不来了,可以一直采样,就根本停不下来。
灰小子:
能提供下完整的程序吗?
一般单次采样时只需要每次设置ADC12CTL0 |= ADC12SC就采样一次;重复采样时,如Rep-sing,设置ADC12CTL1 = SHS_1 +CONSEQ_2就选择了Rep-sing模式,每次采样通过定时器A触发。
ying jun hao:
回复 灰小子:
#include<msp430x14x.h>#include"HR202.c"#define CPU_F ((double)8000000) #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0)) #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))#define HR202_CONTR1_OUT P1DIR |=BIT3 //湿敏电阻#define HR202_CONTR1_CLR P1OUT &=~BIT3#define HR202_CONTR1_SET P1OUT |=BIT3#define HR202_CONTR1_RD P1DIR &=~BIT3#define HR202_CONTR2_OUT P1DIR |=BIT1 //标准电阻#define HR202_CONTR2_CLR P1OUT &=~BIT1#define HR202_CONTR2_SET P1OUT |=BIT1#define HR202_CONTR2_RD P1DIR &=~BIT1 #define HR202_CONTR0_OUT P1DIR |=BIT0 //热敏电阻#define HR202_CONTR0_CLR P1OUT &=~BIT0#define HR202_CONTR0_SET P1OUT |=BIT0#define HR202_CONTR0_RD P1DIR &=~BIT0 int count=0;//int result[10];int result;int AICnt=0;float value=0.0,vout,R;int humid;int n=0,number=0;void ADC12Init(){ ADC12CTL0 &=~ENC; //一定要置0 ADC12CTL0=ADC12ON+SHT0_8+REFON+REF2_5V+MSC; // 3V3 GND REFON+REF2_5V可有可无 ADC12CTL1=ADC12SSEL_2+SHP+CONSEQ_2+CSTARTADD_1+SHS_0; ADC12MCTL1=SREF_1+INCH_1+EOS; //电路正端接入3V3,负端接GND时必须不写SREF_1 ADC12IE=0X0002; //IE中断共16位 _EINT(); }void Init_TimerA() //定时器每100us进入中断{ TACTL=TASSEL_2+MC_1; TACCTL0 = CCIE; CCR0 =50000;} #pragma vector=ADC_VECTOR__interrupt void ADC12IRQ (void){ for(AICnt=0;AICnt<10;AICnt++) { result[AICnt]=ADC12MEM1; value=value+result[AICnt]; //value=value+result[AICnt];}//value=ADC12MEM1;value=value/10; vout=(3.3/4096)*value; R=(3.3-vout)/vout*10; ADC12CTL0 &=~ENC; //完成一组数据采集 关闭ADC ADC12CTL0 &=~ADC12ON; count=1;}
void HR202_shidu(void) //100us定时器中断调用函数{ HR202_CONTR0_RD; HR202_CONTR1_OUT; // HR202_CONTR2_OUT; // HR202_CONTR2_SET;//Rf 1 HR202_CONTR1_CLR;//Rh 0 delay_us(500); HR202_CONTR2_CLR;//Rf 0 HR202_CONTR1_SET;//Rh 1 delay_us(200); ADC12CTL0 |=ENC+ADC12SC; //开启ADC delay_us(300); while(count==0); humid=shidu(28,R); count=0; }
void main(){ WDTCTL = WDTPW+WDTHOLD; ADC12Init(); Init_TimerA(); P6SEL |=0X02;
_EINT(); while(1) { _NOP(); }}void HR202_wendu(void){ HR202_CONTR1_RD; HR202_CONTR2_OUT; HR202_CONTR2_SET; HR202_CONTR0_OUT; HR202_CONTR0_CLR; ADC12CTL0 |=ENC+ADC12SC;}
#pragma vector=TIMERA0_VECTOR __interrupt void Timer_A0(){ _EINT(); number++; if(number==5) { TACCTL0 &=~CCIE; _EINT(); switch(n%2) {case 0: ADC12CTL0 |=ADC12ON; HR202_shidu();break; case 1: } n++; number=0; }}
kqian0327:
你好,
你描述的问题是程序一直在中断里面执行吗?
断点打在里面可以停住吗?
下面的程序是TI的参考代码,希望对你有帮助:
/* –COPYRIGHT–,BSD_EX * Copyright (c) 2012, Texas Instruments Incorporated * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of Texas Instruments Incorporated nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ******************************************************************************* * * MSP430 CODE EXAMPLE DISCLAIMER * * MSP430 code examples are self-contained low-level programs that typically * demonstrate a single peripheral function or device feature in a highly * concise manner. For this the code may rely on the device's power-on default * register values and settings such as the clock configuration and care must * be taken when combining code from several examples to avoid potential side * effects. Also see www.ti.com/grace for a GUI- and www.ti.com/msp430ware * for an API functional library-approach to peripheral configuration. * * –/COPYRIGHT–*///*****************************************************************************// MSP-FET430P140 Demo – ADC12, Single Channel Rpt Mode, TA1 as Sample Trigger//// Description: Sample and convert A0 using Timer_A as sample trigger in// Pulse Sample mode. Put "Num_of_Results" ADC12MEM0 values in results[]// and Light LED (P1.0) when done.//// MSP430F149// —————// | |// Vin –>|P6.0/A0 |// | |//// H. Grewal// Texas Instruments Inc.// Feb 2005// Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version: 3.21A//*****************************************************************************#include <msp430.h>
#define Num_of_Results 512int results[Num_of_Results] = {0};
void ADC_Init(void);
int main(void){ WDTCTL = WDTPW | WDTHOLD; // Disable the Watchdog. ADC_Init(); // Initialize ADC12 ADC12CTL0 |= ENC; // Start conversion __bis_SR_register(LPM0_bits); // Enter LPM0}
void ADC_Init(void){ P1DIR = 0xff; // set port to outputs P1OUT = 0; // reset port outputs P6SEL |= 0x01; // select A0 input ADC12CTL0 = ADC12ON+SHT0_1+REF2_5V+REFON; // Setup ADC12 ADC12CTL1 = SHP+CONSEQ_2+SHS_1; // Timer triggers sampling ADC12MCTL0 = INCH_0 + SREF_1; ADC12IE = 0x0001; // Enable ADC12IFG.0
TACCR0 = 1500; // Delay to allow Ref to settle TACCTL0 |= CCIE; // Compare-mode interrupt. TACTL = TASSEL_1 | MC_1; // TACLK = ACLK, Up mode. __bis_SR_register(LPM3_bits + GIE); // Wait for delay, Enable interrupts TACCTL0 &= ~CCIE; // Disable timer
P2SEL |= BIT3; // Set for Timer A1 P2DIR |= 0x08; TACCR0 = 7; // Init TACCR0 w/ sample prd=CCR0+1 TACCR1 = 4; // Trig for ADC12 sample & convert TACCTL1 = OUTMOD_3; // Set/reset TACTL = TACLR | MC_1 | TASSEL_1; // ACLK, clear TAR, up mode}
// Timer_A0 Interrupt Service Routine#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)#pragma vector=TIMERA0_VECTOR__interrupt void ta0_isr(void)#elif defined(__GNUC__)void __attribute__ ((interrupt(TIMERA0_VECTOR))) ta0_isr (void)#else#error Compiler not supported!#endif{ TACTL = 0; LPM3_EXIT; // Exit LPM3 on return}
// ADC12 Interrupt Service Routine#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)#pragma vector=ADC12_VECTOR__interrupt void ADC12ISR (void)#elif defined(__GNUC__)void __attribute__ ((interrupt(ADC12_VECTOR))) ADC12ISR (void)#else#error Compiler not supported!#endif{ static unsigned int index = 0;
results[index++] = ADC12MEM0; // Move results
if (index == 512) { ADC12CTL0 &= ~ENC; // Stop conversion index = 0; P1OUT |= 0x01; __bis_SR_register(LPM3_bits); // Enter LPM3 }}