用的是MSP430g2553.
/*实验板MSP-EXP430G2
* 利用定时器编写PWM电路,驱动LED,并可以通过按键调节亮度
* MCLK=SMCLK=DCOCLK=32×ACLK=1.048576MHz,
*/
#include <msp430g2553.h>
void main(void)
{
P1DIR |= BIT6+BIT0;
P1DIR &=~BIT3; //P1.3作为按键输入
P1SEL |= BIT6; //P1.6作为PWM输出
P1IE |= BIT3; // P1.3 interrupt enabled
P1IES |= BIT3; // P1.3 Hi/lo edge
P1REN |= BIT3; // P1.3 pullup
P1OUT|=BIT0; //P1.0作为一个指示灯输出
_EINT();
CCR0 = 9;// PWM 周期 =(CCRO+1)/1048576,频率=1.048576MHz/(CCRO+1))
CCTL1 = OUTMOD_7;// CCR1 reset/set
CCR1 = 1;// CCR1 PWM 占空比% = (CCR1+1)/(CCRO+1)
TACTL = TASSEL_2 + MC_1+TACLR;// MCLK, up mode,适用于高频率
for (;;)
{
_BIS_SR(GIE); // 进入 LPM3
_NOP();
}
} #pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
P1OUT ^= BIT0; //每读一次中断,指示灯状态取反
TACCR1++;
if(TACCR1==CCR0)
TACCR1=1;
P1IFG &= ~BIT3; // P1.3 IFG cleared
}