|
#include "msp430g2231.h"
#define PWM BIT6
int main( void )
{
unsigned int i;
unsigned char flag;
flag = 1;
i = 0;
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
TACTL = TASSEL_2 + TACLR;//Timer时钟源选择 清零计时器
CCR0 = 1500;
TACCTL1 = OUTMOD_7;//输出模式选择:PWM复位/置位
CCR1 = 0;
P1DIR |= BIT6;//P1.6设置为输出
P1SEL |= PWM;//P1.6设置为PWM输出口功能
TACTL |= MC_1;//工作模式为增加模式
while(1)
{
if(TACTL & BIT0)
{
TACTL &= ~BIT0;
if(flag)i++;
else i--;
if(i > 1500) flag = 0;
if(i == 0) flag = 1;
CCR1 = i;//改变占空比
}
}
}
实验效果:学习板上P1.6口连接的LED灯会由暗变亮,再由亮变暗,如此循环往复
|
|