msp430f2618使用pwm褪色LED

时间:2016-12-05 20:57:42

标签: msp430 pwm

我正在尝试用C编写代码,使用PWM和MSP430f2618淡化非板外LED。我可以让LED打开,但它保持完全强度。我试图读取一系列频率值,并根据频率值淡化LED。

int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;   // Stop watchdog timer
    int array_size = 0, i = 0, delay = 0;
    double frequency[50] = {0.0};

    array_size = sizeof(frequency);

    frequency [0] = 60.0;

    for (i = 1; i < array_size; i++)
    {
        if (frequency[i - 1] < 61)
        {
            frequency[i] = frequency[i-1] + 0.1;
        }
        else
        {
            frequency[i] = 60.0;
        }
    }

    P4OUT &= 0;
    P4DIR |= (BIT1 + BIT2); //P4.1 and P4.2 output
    P4SEL &= ~(BIT1 + BIT2);    //P4.1 and P4.2 TBx options, timer select
    TBCCR0 = 512-1;

    TBCCTL1 = OUTMOD_7;
    TBCCTL2 = OUTMOD_7;

    for (i = 0; i < array_size; i++)
    {
        P4OUT &= 0;
        if ((frequency[i] < 60.2) && (frequency[i] >=60.0))
        {
            //TBCCR1 = 3200;
            TBCCR1 = 384;
        }

        else if ((frequency[i] < 60.4) && (frequency[i] >=60.2))
        {
            //TBCCR1 = 2560;
            TBCCR1 = 256;
        }

        else if ((frequency[i] < 60.6) && (frequency[i] >=60.4))
        {
            //TBCCR1 = 1920;
            TBCCR1 = 128;
        }

        else if ((frequency[i] < 60.8) && (frequency[i] >=60.6))
        {
            //TBCCR1 = 1280;
            TBCCR1 = 64;
        }

        else if ((frequency[i] < 61) && (frequency[i] >=60.8))
        {
            //TBCCR1 = 640;
            TBCCR1 = 32;
        }

        else
        {
            TBCCR2 = 512;
        }

        P4OUT ^= BIT1;
        for (delay = 0; delay < 32000; delay++);

    }
    TBCTL = TBSSEL_2 + MC_1;                  // ACLK, up mode



    __bis_SR_register(LPM0_bits);             // Enter LPM3
    return 0;
}

1 个答案:

答案 0 :(得分:0)

在您通过设置MC字段启动计时器之前,计时器未运行。初始化必须在开始时完成。