2016-12-05 74 views
0

我想在C中編寫代碼以淡入使用PWM和MSP430f2618的板外LED。我可以讓LED打開,但它保持全部亮度。我正在嘗試讀取頻率值數組,並根據頻率值淡入LED。msp430f2618使用pwm的衰落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; 
} 

回答

0

定時器只有在通過設置MC字段啓動之後纔會運行。初始化必須在開始時完成。

+0

我將TBCTL = TBSSEL_2 + MC_1代碼行移到了看門狗定時器線的下方,但它仍然不起作用? – user3277932

+0

我移動了代碼行,並在確定了最終的工作週期並且工作後更改了我的for語句以驅動pwm。謝謝你的幫助 – user3277932