代码:
1 #include <msp430f5529.h> //函数库 2 int sum=0; //全局变量 3 int main(void) 4 { 5 volatile unsigned int i; //局部变量 6 //设置timerA相关参数 7 WDTCTL = WDTPW+WDTHOLD; // 关闭看门狗 8 P1DIR |= BIT2; // 将P1.2设置为输出端口,验证状态 9 P2DIR |= BIT0; //P2.0输出PWM波 10 TA0CCTL1 = OUTMOD_7; //将输出模式选择为模式7 11 TA0CTL = TASSEL_1 + MC_1 + TACLR ; //选择ACLK时钟源,timerA计数模式选择up模式,计数到TA0CCR0,重置timerA计数器 12 TA1CCTL1 = OUTMOD_7; 13 TA1CTL = TASSEL_1 + MC_1 +TACLR ; 14 15 P1REN |= BIT7; // 使能端口1.7 16 P1OUT |= BIT7; // 选择下拉模式 17 P1IES |= BIT7; // 1.7端口中断触发沿选择下降沿,高到低触发 18 P1IFG &= ~BIT7; // 1.7端口中断寄存器清零 19 P1IE |= BIT7; // 1.7端口中断使能 20 21 __bis_SR_register(GIE); //将GIE置1,打开全局中断使能 22 23 while(1) // 采用switch,每一个case值对应一个状态 24 { 25 switch (sum) 26 { 27 case 0: 28 P1OUT &= ~BIT2; //初始状态为低功耗状态 29 TA0CCR1 = 0; 30 __bis_SR_register(LPM3_bits); //将LPM3置1,使系统进入低功耗 31 break; //跳出switch函数 32 case 1: //选系统的默认时钟,频率为1MHZ。 33 P1OUT &= ~BIT2; //状态1要求:转到—60°,900微秒,设置的为1000微秒,时钟频率为32768Hz 34 TA0CCR0 = 20000; //验证进入状态1 35 TA0CCR1 = 10000; 36 TA1CCR0 = 100; //输出PWM波周期 37 TA1CCR1 = 34; 占空比 38 P1DIR |=BIT2; 39 P1SEL |=BIT2; 40 P2DIR |=BIT0; 41 P2SEL |=BIT0; 42 break; 43 case 2: 44 P1OUT &= ~BIT2; //状态2要求:转到0°。1520微秒 45 TA0CCR0 = 20000; 46 TA0CCR1 = 5000; 47 TA1CCR0 = 100; 48 TA1CCR1 = 50; 49 P1DIR |=BIT2; 50 P1SEL |=BIT2; 51 P2DIR |=BIT0; 52 P2SEL |=BIT0; 53 break; 54 case 3: //时钟为辅助时钟ACLK,频率为32768HZ 55 //P1OUT &= ~BIT2; //状态3要求:转到60° 2100微秒 56 TA0CCR0 = 20000; 57 TA0CCR1 = 800; 58 TA1CCR0 = 100; 59 TA1CCR1 = 66; 60 P1DIR |=BIT2; 61 P1SEL |=BIT2; //选择1.2端口的复用功能,使之能够实现亮和灭 62 P2DIR |=BIT0; 63 P2SEL |=BIT0; 64 break; 65 66 } 67 if(sum>3) //当sum的值超过3时,要让sum减3,重新进入循环 68 { 69 sum=sum-3; 70 } 71 } 72 } 73 74 //采用S1中断按键实现 75 #pragma vector=PORT1_VECTOR //端口p1的中断函数 76 __interrupt void Port_1(void) 77 { 78 unsigned int push_key =0; 79 push_key == P1IFG & (P1IN); 80 __delay_cycles(20000); //精确时间延迟,乘以MCLK时钟周期,防止抖动 81 if( push_key == (P1IN & BIT7)) 82 { 83 __low_power_mode_off_on_exit(); //SR寄存器置0,退出低功耗 84 sum=sum+1; // 按下p1.7,sum加1,周期变化一次 85 } 86 P1IFG &= ~BIT7; // p1.7中断重置 87 }