f5529 呼吸灯(三角波)
我写的程序有问题:可以做出呼吸灯(用PWM),但是总会莫名地闪。另外请问有没有三角波和正弦波的程序?我想参考一下。以下是我的程序
#include <msp430.h>
void inittime(void)//12MHz
{
volatile unsigned int i;
UCSCTL3 |= SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
UCSCTL1 = DCORSEL_5; // Select DCO range 24MHz operation
UCSCTL2 = FLLD_1 + 374; // Set DCO Multiplier for 12MHz
// (N + 1) * FLLRef = Fdco
// (374 + 1) * 32768 = 12MHz
// Set FLL Div = fDCOCLK/2
__bic_SR_register(SCG0); // Enable the FLL control loop
// Worst-case settling time for the DCO when the DCO range bits have been
// changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
// UG for optimization.
// 32 x 32 x 12 MHz / 32,768 Hz = 375000 = MCLK cycles for DCO to settle
__delay_cycles(375000);
// Loop until XT1,XT2 & DCO fault flag is cleared
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
// Clear XT2,XT1,DCO fault flags
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
}
void delay1s(int a)//12MHz
{
int i,j;
for(i = a*1000; i >=1; i--)
for(j = 2996; j >= 1; j--);
}
void delay1ms(int a)//12MHz
{
int j;
for(a = a; a >=1; a--)
for(j = 2996; j >= 1; j--);
}
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
inittime();
int one;
P1DIR |= BIT2+BIT3; // P1.2 and P1.3 output
P1SEL |= BIT2+BIT3; // P1.2 and P1.3 options select
TA0CCR0 = 512-1; // PWM Period
TA0CCTL1 = OUTMOD_7; // CCR1 reset/set
TA0CCR1 = 384; // CCR1 PWM duty cycle
TA0CCTL2 = OUTMOD_7; // CCR2 reset/set
TA0CCR2 = 20; // CCR2 PWM duty cycle
TA0CTL = TASSEL_1 + MC_1 + TACLR; // ACLK, up mode, clear TAR
while(1)
{
for(one = 400; one >= 0; one -= 5)
{
TA0CCR1 = one; // CCR1 PWM duty cycle
delay1ms(10);
}
for(one = 0; one <= 400; one += 5)
{
TA0CCR1 = one; // CCR1 PWM duty cycle
delay1ms(10);//while(1);
}
}
}
亲;啥情况下会闪?你的电路是啥样的?