发这个帖子是为了帮助
童鞋理解PWM控制LED,给出几个示例程序。
简单PWM的生成其实很简单,CCR0和时钟控制周期,CCR1控制占空比,如图:
附代码1:用按键来控制占空比
#include "io430.h"
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P1SEL |= BIT6;
P1DIR |= BIT6; // P1.6 as TA0.1 output
P1IES |= BIT3; //P1.3 high to low interrupt
P1IFG = 0;
P1IE |= BIT3; //Enable P1.3 interrupt
CCTL1 = OUTMOD_3; /* PWM output mode: 3 - PWM set/reset */
CCR0 = 1000;
CCR1 = 800;
TACTL = TASSEL_2 + TACLR + MC_1; // SMCLK, UP mode, enable interrupt.
__low_power_mode_0();
}
#pragma vector = PORT1_VECTOR
__interrupt void port1_isr(void)
{
if (P1IFG & BIT3)
{
for(int i=0; i<10; i++);
CCR1 += 50;
if (CCR1 > 1000)
CCR1 = 800;
P1IFG &= ~BIT3;
}
}
复制代码