1.捕获模式
1 #include <msp430x14x.h> 2 unsigned int last0=0,last1=0; 3 unsigned int k=0,j=0; 4 void mian(void) 5 { 6 WDTCTL = WDTPW + WDTHOLD; 7 TACTL = TACLR + TASSEL_2 + ID_3 + MC_3; 8 9 TACCTL0 = CM_3 + CCIS_0 + SCS + CAP + CCIE; 10 TACCTL1 = CM_3 + CCIS_1 + SCS + CAP + CCIE; 11 12 P1DIR = 0X00; 13 P1SEL = 0XFF; 14 15 P3DIR = 0XFF; 16 P3OUT = 0XFF; 17 _EINT(); 18 for(;;) 19 { 20 for(int i=0;i<10000;i++) 21 _NOP(); 22 P3OUT ^= BIT0; //产生捕获信号源 23 } 24 } 25 #pragma vector=TIMERA1_VECTOR 26 __interrupt void Timer_A(void) 27 { 28 switch(TAIV) 29 { 30 case 2: 31 P1OUT ^= BIT3; 32 j=CCR1 = last1; 33 last1 = CCR1; 34 break; 35 case 4:break; 36 case 10:P1OUT ^= BIT0;break; 37 } 38 } 39 40 #pragma vector=TIMERA1_VECTOR 41 __interrupt void Timer_cc(void) 42 { 43 if(SCCI0 == 0) 44 { 45 k = CCR0 - last0; 46 last0 = CCR0; 47 } 48 }
2.比较模式
1 /*************************************** 2 功能:ACLK=32768 Hz,MCLK=SMCLK=DCOCLK=80kHz 3 由捕获/比较模块0产生10Hz的方波,P6.0输出; 4 由捕获/比较模块1产生20Hz的方波,P6.7输出。 5 ***************************************/ 6 #include <msp430x14x.h> 7 void main(void) 8 { 9 WDTCTL = WDTPW + WDTHOLD; 10 TACTL |= TASSEL_2 + ID_3 + MC_2 + TACLR + TAIE; 11 12 TACCTL0= CCIE; 13 TACCTL1= CCIE; 14 TACCR0 = 5000; 15 TACCR1 = 10000; 16 17 P6DIR = 0XFF; 18 P6OUT = 0Xff; 19 _EINT(); 20 } 21 22 #pragma vector=TIMERA0_VECTOR 23 __interrupt void Timer_A(void) 24 { 25 P6OUT ^= BIT0; 26 TACCR0 = TACCR0 + 5000; 27 } 28 #pragma vector=TIMERA1_VECTOR 29 __interrupt void Timer_A1(void) 30 { 31 switch(TAIV) 32 { 33 case 2: 34 P6OUT ^= BIT7; 35 TACCR1 = TACCR1+10000; 36 break; 37 case 4:break; 38 case 10:P6OUT ^= BIT1;break; 39 } 40 }