程序如下。。。。
#include <msp430g2553.h>
void key_init();
void main(void)
{
static int i;
//XTS(1);
//XT1OFF(0);
//XT1BYPASS(0);
//XCAP(0);
_EINT(); //打开全局中断
WDTCTL = WDTPW + WDTHOLD;
// Stop WDT禁止看门狗
P1DIR |= BIT2; // P1.2 and P1.3 TA1/2 options 1为选引脚第二功能,0为引脚第一功能1
P1SEL |= BIT2;
CCR0 = 512-1; // PWM Period
CCTL1 = OUTMOD_7;// CCR1 reset/set
i=168;
CCR1 =i; // CCR1 PWM duty cycle
TACTL = TASSEL_1 + MC_1; // ACLK时钟, 上升沿捕获
_BIS_SR(CPUOFF); // Enter LPM0
}
void key_init()
{
P1DIR&=~BIT3; //P1.3为输入
P1REN|=BIT3; //设置上拉
P1OUT|=BIT3;
P1IES|=BIT3; //高电平到低电平有效,即下降沿
P1IFG&=~BIT3; //清除中断标志,带上这句最好
P1IE|=BIT3;
}
#pragma vector=PORT1_VECTOR //中断
__interrupt void port1(void)
{
if(P1IFG&BIT3)
{
static int i;
if(i<=511)
i += 168;
else i = 0; // 调整占空比
P1IFG&=~BIT3; //这句清除中断标志
}
else
P1IFG=0X00;
} |