0

我正在研究需要在 RGB LED 上显示不同颜色的项目。我正在使用 pwm 在 LED 上驱动不同的颜色。我的图片是 PIC24FJ64GA004,我现在正在使用它。这个项目的基本概念是使用开关来控制颜色。

RGB LED 上的颜色将根据一年中的日期和月份。为此,我使用 7 段 LED 开关来计算日期和月份。

目前的问题是以下代码。我正在尝试通过以下设置更改 PWM 值。但它没有改变它,而是给了我一些奇怪的东西。我需要你们的帮助。你能帮我解决这个问题吗?

 for( counter=0x0000;counter<=0x4571;counter++){ 
//*****Timer2 starting from here*****//
PR2                 = 0x4571;   // Initialize PR2 with 0x4571 = 17777 as PWM cycle
IFS0bits.T2IF       = 0;        // Clear Output Compare interrupt flag
IEC0bits.T2IE       = 1;        // Enable Output Compare interrupts
T2CONbits.TON       = 1;        // Start Timer2 with assumed settings
//**********************************//
//*****For RED LED OC1 choosed with timer 2*****//
OC1CONbits.OCM      = 0;        // Output compare channel is disabled
OC1R                = 0x0000 ; // Initialize Compare Register1 with 50% duty cycle
OC1RS               = counter;  // Initialize Secondary Compare Register1 with 50% duty cycle
OC1CONbits.OCSIDL   = 0;        // Output capture will continue to operate in CPU Idle mode
OC1CONbits.OCFLT    = 0;        // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC1CONbits.OCTSEL   = 0;        // Timer2 is the clock source for output Compare
OC1CONbits.OCM      = 0x6;      // PWM mode on OC, Fault pin disabled

//*****For Green Led OC2 and OC3 Choosed with timer2 as well*****//

OC2CONbits.OCM      = 0;        // Output compare channel is disabled
OC2R                =0x0000;        // Initialize Compare Register1 with 50% duty cycle
OC2RS               =counter;   // Initialize Secondary Compare Register1 with 50% duty cycle
OC2CONbits.OCSIDL   = 0;        // Output capture will continue to operate in CPU Idle mode
OC2CONbits.OCFLT    = 0;        // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC2CONbits.OCTSEL   = 0;        // Timer2 is the clock source for output Compare
OC2CONbits.OCM      = 0x6;      // PWM mode on OC, Fault pin disabled


//*****For  Blue Led OC2 and OC3 Choosed with timer2 as well*****//
OC3CONbits.OCM      = 0;        // Output compare channel is disabled
OC3R                = 0x0000;           // Initialize Compare Register1 with 50% duty cycle
OC3RS               = counter;  // Initialize Secondary Compare Register1 with 50% duty cycle
OC3CONbits.OCSIDL   = 0;        // Output capture will continue to operate in CPU Idle mode
OC3CONbits.OCFLT    = 0;        // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC3CONbits.OCTSEL   = 0;        // Timer2 is the clock source for output Compare
OC3CONbits.OCM      = 0x6;      // PWM mode on OC, Fault pin disabled

  }

但是这段代码工作正常。我也输入了不同的值。它工作正常。

   //*****For RED LED OC1 choosed with timer 2*****//
OC1CONbits.OCM      = 0;        // Output compare channel is disabled
OC1R                = 0x22B8;   // Initialize Compare Register1 with 50% duty cycle
OC1RS               = 0x22B8;   // Initialize Secondary Compare Register1 with 50% duty cycle
OC1CONbits.OCSIDL   = 0;        // Output capture will continue to operate in CPU Idle mode
OC1CONbits.OCFLT    = 0;        // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC1CONbits.OCTSEL   = 0;        // Timer2 is the clock source for output Compare
OC1CONbits.OCM      = 0x6;      // PWM mode on OC, Fault pin disabled

//*****For Green Led OC2 and OC3 Choosed with timer2 as well*****//

OC2CONbits.OCM      = 0;        // Output compare channel is disabled
OC2R                =0x22B8;    // Initialize Compare Register1 with 50% duty cycle
OC2RS               =0x22B8;//0x22B8;   // Initialize Secondary Compare Register1 with 50% duty cycle
OC2CONbits.OCSIDL   = 0;        // Output capture will continue to operate in CPU Idle mode
OC2CONbits.OCFLT    = 0;        // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC2CONbits.OCTSEL   = 0;        // Timer2 is the clock source for output Compare
OC2CONbits.OCM      = 0x6;      // PWM mode on OC, Fault pin disabled


//*****For  Blue Led OC2 and OC3 Choosed with timer2 as well*****//
OC3CONbits.OCM      = 0;        // Output compare channel is disabled
OC3R                = 0x22B8;   // Initialize Compare Register1 with 50% duty cycle
OC3RS               = 0x22B8;   // Initialize Secondary Compare Register1 with 50% duty cycle
OC3CONbits.OCSIDL   = 0;        // Output capture will continue to operate in CPU Idle mode
OC3CONbits.OCFLT    = 0;        // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC3CONbits.OCTSEL   = 0;        // Timer2 is the clock source for output Compare
OC3CONbits.OCM      = 0x6;      // PWM mode on OC, Fault pin disabled

//*****Timer1 starting from here*****//
PR1                 = 65535;    // Initialize PR2 cycle
T1CONbits.TCKPS     = 2;        // Setting pre-scaler to 1/64
IFS0bits.T1IF       = 0;        // Clear Output Compare interrupt flag
IEC0bits.T1IE       = 1;        // Enable Output Compare interrupts
T1CONbits.TON       = 1;        // Start Timer1 with assumed settings
//*****Timer2 starting from here*****//
PR2                 = 0x4571;   // Initialize PR2 with 0x4571 = 17777 as PWM cycle
IFS0bits.T2IF       = 0;        // Clear Output Compare interrupt flag
IEC0bits.T2IE       = 1;        // Enable Output Compare interrupts
T2CONbits.TON       = 1;        // Start Timer2 with assumed settings
//**********************************//
4

1 回答 1

2

您每次都在循环更新您的 PWM。

计数器不太可能有时间到期,因此您在它有机会“执行”一个 PWM 周期之前不断重置它。

在循环结束时,您需要等待(至少其中一个)PWM 过期。

另一种方法可能是在循环中设置您想要的下一个值,并在定时器到期时让中断服务例程将其复制到 PWM 寄存器。一旦发生这种情况,您就可以设置下一个下一个值 :) 您需要注意如何在主循环和 ISR 之间传输值,因为它们实际上是不同的“线程”上下文。

于 2013-02-15T12:06:52.557 回答