rt:我想用定时器去控制AD数据采集,但是没有达到理想结果,我首先用TB定时10ms,然后再TB的中断服务程序里面启动TA,给TA赋初值300us,TA的终端里面开启AD并给TA重新装入100us的值,到100us后关掉AD,程序编译没有问题,但是我运行的时候tbcounter和f1的数值比j的值要大4倍多,原则上应该几个值是一样的,在线等
#include <msp430x14x.h>
void InitSys(void) //初始化系统时钟 { unsigned int iq0; _DINT(); //关中断 BCSCTL1 &=~XT2OFF; do { IFG1 &= ~OFIFG; // 清除振荡器失效标志 for (iq0 = 0xFF; iq0 > 0; iq0--); // 延时,等待XT2起振 } while ((IFG1 & OFIFG) != 0); // 判断XT2是否起振 BCSCTL2 =SELM1+SELS; // MCLK,SMCLK时钟为XT2,8M /* DCOCTL = 0XFF; BCSCTL1 = RSEL2 + RSEL1 + RSEL0;*/ //BCSCTL2 = SELM0; }
void delay(void) { int i; for(i=0;i<1000;i++); }
void Init_ad(void) //初始化AD { P6SEL |= 0x08;//A3 ADC12CTL0 = ADC12ON +SHT0_0 + REFON ; //+ MSC;//内核打开,采样保持定时定器, ADC12CTL1 = SHP+ ADC12SSEL_2 + CSTARTADD_3+ CONSEQ_0 ; //MCLK,单通道多次转换,采样来自采样定时器,转换存储地址为mem3 ADC12MCTL3 = SREF_1 + INCH_3; //Vr+=AREF+,VR-=AVSS; ADC12CTL0 |= ENC; //使能转换 ADC12IE = 0X08; //允许a3中断 delay(); }
void Init_ta(void) //初始化定时器A { CCTL0 = CCIE; //允许CCR0中断 CCR0 = 0;//300us //CCTL1 = CCIE; //CCR1 = 5000; TACTL = TASSEL_2 + TACLR + ID_1 ; //选择系统主时钟8M,2分频,定时器清零,增记数模式 }
void Init_tb(void) //初始化定时器B { TBCCTL0 = CCIE; TBCCR0 = 40000; //10ms TBCTL = TBSSEL_2 + TBCLR + MC_1 + ID_1; //MCLK8M/2,clear, }
unsigned int result=0; int ReadArray[400]={0}; int Aver[100]={0}; double sum=0; unsigned int m,n;
int tbcounter=0; int ta1=0;
int j=0; int i=0; int f1=0;
int k=0; int counter=0; long int ADcounter=0; int flag; int tempkey; void main(void) { WDTCTL = WDTPW + WDTHOLD; P5DIR = BIT4 + BIT5 + BIT7; P5SEL = BIT4 + BIT5; InitSys(); Init_ad(); Init_ta(); Init_tb(); P5OUT =0X00;//open ad627 //ADC12CTL0 = ADC12ON;//open ad _EINT(); //开总中断 flag = 0; while(1) { if(flag==1) { // P5OUT = 0X80; ADC12CTL0 |= ADC12SC; } if(ADcounter==11) { ADC12CTL0 &= ~ADC12SC; Aver[j++]=sum/11; ADcounter=0; sum=0; } } //m=ADcounter&0XFFFF; // n=(ADcounter>>16)&0XFFFF; // n=m*1500/4096; }
#pragma vector = ADC_VECTOR __interrupt void ADC12ISR(void) { sum+=ADC12MEM3; ADcounter++; }
#pragma vector = TIMERA0_VECTOR __interrupt void TIMER_A(void) { counter++; if(counter==2) { //P5OUT = 0X80; // ADC12CTL0 |= ADC12SC; flag = 1; CCR0 = 400; f1++; } if(counter==3) { flag=0; P5OUT = 0X00; //TBCTL ^= MC_1; TACTL ^=MC_1;
counter=1; } }
#pragma vector = TIMERB0_VECTOR __interrupt void Timer_B(void) { P5OUT = 0X80; CCR0=1200; tbcounter++; TACTL += MC_1; }
|