调一个 死值的占空比 没事 ,我将占空比 100% 慢慢降到 0% 快到 0%时
出现一次 占空比100%的一个脉冲 LED灯闪一下,调光中间不闪.
现在还不知道是我程序的BUG还是这IC本来就有这鸟问题 不能用于调光.
这要命 ,,,
我已经在 PWM 那路中断 中 去更新占空比了,
前面我还试了 在MAIN中更新 和 另一组定时器中更新 结果就是中间不断有闪烁感 有时无.
我用的是MSP430G2553
//===========================================================================
TA1CCR0 = Lum100-1; // PWM Period 1000us/(1/8MHZ) 这里不减1 高电平还不能到100%
TA1CCR1 = 0;//PWM1_duty_cycle_Val; // CCR1 PWM duty cycle 2.1
TA1CCR2 = 0;//PWM1_duty_cycle_Val; // CCR2 PWM duty cycle 2.4
TA1CCTL0 = CCIE; // CCR0 interrupt enabled 就用这个中断
TA1CCTL1 = OUTMOD_7; // CCR1 reset/set
TA1CCTL2 = OUTMOD_7; // CCR2 reset/set
TA1CTL = TASSEL_2 + TACLR + MC_1; // SMCLK, up mode
然后 定时中断 更新
// Timer1 A0 interrupt service routine
#pragma vector=TIMER1_A0_VECTOR //TA1 CCR0
__interrupt void Timer1_A0 (void) //1khz
{
TA1CCR1 = PWM1_duty_cycle;
TA1CCR2 = PWM1_duty_cycle;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
typedef signed char s8;
typedef signed int s16;
typedef signed long s32;
#define LED1() (P2OUT ^= BIT0)
#define PWM1_duty_cycle_Val 8000
u16 PWM1_duty_cycle = 0;
u16 PWM1_duty_cycle_CCR = 0;
//=============================================================================
void InitSystemClock(void)
{
BCSCTL1 = CALBC1_8MHZ;
DCOCTL = CALDCO_8MHZ; // DCO 8MHZ
FCTL2 = FWKEY + FSSEL0 + 0x18; // MCLK/3 for Flash Timing 8M / 25 320K //FLASH IN 257-476KHZ
}
//=============================================================================
void Init_IO(void)
{
P2DIR |=BIT0; //P2.0 Output LED
//P2OUT &= ~BIT1; //LOW
//P2OUT |= BIT1; //HIGH
P2DIR |= BIT1; // P2.1 pwm output 详见硬件手册
P2SEL |= BIT1; // P2.1 Pin9 Multiplexing
//P2DIR |= BIT2; // P2.1 pwm output 2.1 2.2只能输出相同波形
//P2SEL |= BIT2; // P2.1 Pin9
//P2OUT &= ~BIT4; //LOW
P2DIR |= BIT4; // P2.4 pwm output
P2SEL |= BIT4; // P2.4 Pin12 Multiplexing
//P2DIR |= BIT5; // P2.4 pwm output 2.4 2.5只能输出相同波形
//P2SEL |= BIT5; // P2.4 Pin12
}
//=============================================================================
void InitPWM(void)
{
/*
//===========================================================================
TA0CCR0 = 2550; // PWM Period
TA0CCR1 = 1000; // CCR1 PWM duty cycle
//TA0CCR2 = 800; // CCR2 PWM duty cycle 28Pins
TA0CCTL0 = CCIE; // CCR0 interrupt enabled
//TA0CCTL1 = OUTMOD_7; // CCR1 reset/set
//TA0CCTL2 = OUTMOD_7; // CCR2 reset/set 28Pins
TA0CTL = TASSEL_2 + TACLR + MC_1; // SMCLK, up mode 定时器_A 控制寄存器
*/
//===========================================================================
TA1CCR0 = PWM1_duty_cycle_Val-1; // PWM Period 1000us/(1/8MHZ)
TA1CCR1 = 0;//PWM1_duty_cycle_Val; // CCR1 PWM duty cycle 2.1
TA1CCR2 = 0;//PWM1_duty_cycle_Val; // CCR2 PWM duty cycle 2.4
TA1CCTL0 = CCIE; // CCR0 interrupt enabled
TA1CCTL1 = OUTMOD_7; // CCR1 reset/set
TA1CCTL2 = OUTMOD_7; // CCR2 reset/set
TA1CTL = TASSEL_2 + TACLR + MC_1; // SMCLK, up mode
}
// Timer1 A0 interrupt service routine
#pragma vector=TIMER1_A0_VECTOR //TA1 CCR0
__interrupt void Timer1_A0 (void) //1khz
{
if (PWM1_duty_cycle_CCR < PWM1_duty_cycle) //Smooth
{
PWM1_duty_cycle_CCR++;
}
else if (PWM1_duty_cycle_CCR > PWM1_duty_cycle)
{
if (PWM1_duty_cycle_CCR)
PWM1_duty_cycle_CCR--;
}
TA1CCR1 = PWM1_duty_cycle_CCR;
TA1CCR2 = PWM1_duty_cycle_CCR;
}
//=============================================================================
void DelaymS(unsigned int n)
{
volatile u16 i = 0;
volatile u16 j = 0;
for (i=n;i>0;i--)
{
for (j=680;j>0;j--)
{
_NOP();
}
}
}
/*
* main.c
*/
int main(void)
{
int i = 0;
_DINT(); // disable_interrupt
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
InitSystemClock();
InitPWM(); //TA1
Init_IO();
_EINT() ; // enable_interrupt
while(1)
{
LED1();
if (++i == 1)
{
PWM1_duty_cycle = 0;
}
else if (i == 2)
{
i = 0;
PWM1_duty_cycle = PWM1_duty_cycle_Val;
}
DelaymS(3000);
}
//return 0;
}
找时间我在板子上验证下
一周热门 更多>