代码一:
/************************** 功能:LED每隔1秒闪烁一次 ****************************/ #include <msp430x14x.h> void main() { P6DIR = 0XFF; P6OUT = 0X00; TACCR0 = 4095; TACCTL0 = CCIE; TACTL = TASSEL_1 + ID_3 + MC_1; _EINT(); LPM3; } #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A(void) { P6OUT ^= 0XFF; }
代码二:
1 #include <msp430x14x.h> 2 3 int flag=0,time=0; 4 void int_clk(); //初始化时钟 5 void int_timer(); //设置定时器A 6 void main() 7 { 8 WDTCTL = WDTPW +WDTHOLD; 9 P6DIR = 0XFF; 10 P6OUT = 0XFF; 11 int_clk(); 12 int_timer(); 13 _EINT(); 14 15 for(;;) 16 { 17 while(flag) 18 { 19 flag=0; 20 time++; 21 if(time==100) 22 { 23 P6OUT ^= 0XFF; 24 time = 0; 25 } 26 } 27 } 28 } 29 30 void int_clk() 31 { 32 BCSCTL1 &= ~XT2OFF; 33 BCSCTL2 |= SELM_1 + SELS; 34 do 35 { 36 IFG1 &= ~OFIFG; 37 for(int i=100;i>0;i--) 38 _NOP(); 39 }while((IFG1 & OFIFG)!=0); 40 IFG1 &= ~OFIFG; 41 } 42 43 void int_timer() 44 { 45 TACTL |= TASSEL_2 + ID_3 + MC_1; 46 TACCTL0 = CCIE; 47 TACCR0 = 9999; //10ms 48 } 49 #pragma vector = TIMERA0_VECTOR 50 __interrupt void Timer_A(void) 51 { 52 flag = 1; 53 }