定时使用32Khz的时钟,捕获使用8M的SMCLK,但是每次结果都是32K???
时钟设置
float count = 0.0;
void ClkInit() { char i; BCSCTL1 &= ~XT2OFF; //打开XT2振荡器 IFG1&=~OFIFG; //清除振荡错误标志 while((IFG1&OFIFG)!=0) { for(i=0;i<0xff;i++); IFG1&=~OFIFG; //清除振荡错误标志 } BCSCTL2 |= SELM_2+SELS+DIVS_0; //MCLK为8MHz,SMCLK为8MHz }
捕获模式设置 void Init_cap(void) { P1SEL |=0x04; //选择P1.2作为捕获的输入端子 TACCTL1|=CM0+SCS+CCIS_0+CAP+CCIE;//上升沿触发,同步模式,使能中断 TACTL |=TASSEL1+ID_0; //选择8M-SMCLK时钟 }
定时设置
void Init_timer_b(void) { TBCCTL0 = CCIE; // CCR0 interrupt enabled TBCCR0 = 64000; //定时2秒 TBCTL = TBSSEL_1 + MC_1; // ACLK, upmode } int main( void ) { //Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; ClkInit(); //初始化时钟MCLK 8MHZ,SMCLK 8MHZ P5DIR |= 0X60; P5SEL |= 0X60; LcdInit(); LcdWriteString(0x80,"欢迎"); Init_cap(); Init_timer_b(); _EINT(); while(1); }
#pragma vector=TIMERA1_VECTOR __interrupt void Timer_A(void) { switch(TAIV) { case 2: { count++; break; }; case 4: break; case 10: break; } } #pragma vector=TIMERB0_VECTOR __interrupt void Timer_B (void) { TACCTL1 &= ~(CCIE);//关闭捕获中断 TBCCTL0 &= ~(CCIE); //关闭定时中断 P2DIR |= 0XFF; P2OUT ^= BIT0; count = (count/2000.0); fenli(count); LcdWriteString(0x90,"频率:"); LcdWriteData(baiwei); LcdWriteData(shiwei); LcdWriteData(gewei); LcdWriteData(0x2e); //此处显示一个. LcdWriteData(yiwei); LcdWriteData(lianwei); LcdWriteString(LcdReadAddr()+0x01,"KHz"); }
|