This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430输出可调占空比的PWM波失败

Other Parts Discussed in Thread: MSP430G2553

手中板子为launchpad 芯片为MSP430G2553
现想用定时器输出PWM占空比可调的波形,尝试多种办法,一直失败.

一.这个实现的思路是:使用定时器的UP_MODE,配置CCIE的中断,在中断中对TA1CCR1进行更改,从面实现PWM波占空比的改变
//系统时钟配置为8Mhz,中断开启
        P2DIR |= 0x02;                            // P2.1  output
        P2SEL |= 0x02;                            // P2.1  配为定时器输出

        TA1CTL = TASSEL_2  + ID_3 + MC_1; //+ TAIE//  开启定时器   SMCLK, 8分频(1M) up mode
        TA1CCTL1 =  OUTMOD_7 + CCIE; //           // 工作模式7,使能CCR0中断,即最高优先级中断
        TA1CCR0 = 50;                             // PWM Period
        TA1CCR1 = 5;                              // 高电平时间
        while(1);

//中断代码:在中断中实现TA1CCR1在30和10之间的切换
#pragma vector=TIMER1_A1_VECTOR
__interrupt void Timer_A(void)
{
switch( TA1IV )
{
   case  2:
invert_flag_1 ^= 1;
   if(invert_flag_1)
   {        
                TA1CCR1 = 30;                              // 高电平时间
   }
   else
   {
                TA1CCR1 = 10;                              // 高电平时间
   }
                      break;                          // CCR1 not used
   case  4: break;                          // CCR2 not used
   case 10:break;
}
}

二.P2.1 的输出波形: