Understanding of TIMERA timing interrupt of MSP430F149[Copy link]
The code is as follows: //This platform is msp430f149 #include#include volatile unsigned char count = 0,flag = 0; void select_xt2(void){ unsigned char i; /*------Select the system main clock as 8MHz-------*/ BCSCTL1 &= ~XT2OFF; do{ IFG1 &= ~OFIFG; //Clear crystal oscillator failure flag for (i = 0xFF; i > 0; i--); //Wait for the 8MHz crystal to start oscillating }while ((IFG1 & OFIFG)); BCSCTL2 |= SELM_2 + SELS ; //MCLK and SMCLK select high frequency crystal oscillator } void main(){ WDTCTL = WDTPW + WDTHOLD; // Dog off select_xt2(); //8MHz crystal oscillator P2DIR = 0xFF; P2OUT = 0x00; CCTL0 = 0X0010; // CCR0 interrupt enable, or write CCTL0 = CCIE; CCR0 = 10000; TACTL = 0X02E0; // SMCLK = 1MHz, continuous counting mode _EINT(); // Enable global interrupt while(1){ if(count == 50){ count = 0; P2OUT = ~P2OUT ; } } } // Timer A0 interrupt service function #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void){count ++;CCR0 += 10000; // Load the next interrupt time into CCR0}I prefer to configure registers directly rather than use library functions for AND or OR, so when setting TACTL, according to needs:1. Use 8 division to change 8MHz to 1MHz2. Use SMCLK subsystem clock. In setting xtal2, SMCLK and MCLK are both external crystal oscillators of 8MHZ3. Use continuous counting mode. This mode needs to be manually reloaded. The rest of the bits are 0 So the TACTL control word is: 0000, 0010, 1110, 0000 02E0 CCTL0 register configuration:, Because we are using compare mode and enabling interrupts, only the CCIEx bit is 1, and the rest are all 0 The control word is: 0X0010. At this point, you can also directly write CCTL0 = CCIE0; but I still like to write it in the configuration register. Load the initial count value 10000 into CCR0. When the count reaches 10000, count increases by 1 and reloads CCR0. When count equals 50, count = 0