Detailed explanation of MSP430F5529 timer usage program[Copy link]
#include
void main(void) { WDTCTL=WDTPW+WDTHOLD; P1DIR|=(BIT1+BIT2+BIT3+BIT4+BIT5);//P1.1-P1.5 is the output direction P1OUT=0x00; //Pull all low, initialize all LEDs to turn off TA0CCTL1=CCIE; //Capture comparator 1 to enable CCIFG bit interrupt TA0CCR1=13107; //Put in the value to be compared 0xff/5=13107 TA0CCTL2=CCIE; //Capture comparator 2 to enable interrupt TA0CCR2=26214; //13107*2=26214 TA0CCTL3=CCIE; //Capture comparator 3 to enable interrupt TA0CCR3=39321; //13107*3=39321 TA0CCTL4=CCIE; //Capture comparator 4 to enable interrupt TA0CCR4=52428; //13107*4=52428 TA0CTL|=TACLR+TAIE; //Enable interrupt and clear TA0CTL|=TASSEL_1+MC_2+TAIE; //Select SCLK32.768KHZ as the clock, select continuous mode, and enable interrupt/*In this case, the time for 5 lights to flash once is 0xffff/32768=2S*/ __enable_interrupt(); //Enable total interruptwhile(1); } /*TIMER0_A0_VECTOR is the interrupt register of CCR0 of timer 0, TIMER0_A1_VECTOR is the register of CCR1-CCR4 and TA of timer 0*/ /*Similarly, timer TA1 is also divided into two TIMER1_A0_VECTOR and TIMER1_A1_VECTOR*/ #pragma vector=TIMER0_A1_VECTOR __interrupt void TimerA(void) { switch(__even_in_range(TA0IV,14)) /* This sentence means: the statements in the switch function will only be executed when the value of TA0IV is an even number between 0 and 14. Its function is to improve the efficiency of the switch statement*/ { case 2:P1OUT=BIT1;break; //TACCR1 CCIFG is set, indicating that the count value is equal to the set 13107, which means that 0.4S has been counted case 4:P1OUT=BIT2;break; //TACCR2 CCIFG is set, indicating that 0.8S has been counted case 6:P1OUT=BIT3;break; //TACCR3 CCIFG is set, indicating that 1.2S has been counted case 8:P1OUT=BIT4;break; //TACCR4 CCIFG is set, indicating that 1.6S has been counted case 14:P1OUT=BIT5;break; //TAIFG is set, indicating that 2S has been counted default:break; } }