PIC24FJ64GA002中的可变PWM设置

时间:2013-02-14 15:47:49

标签: c embedded pwm

我正在研究我需要在RGB led上显示不同颜色的项目。我正在使用pwm在LED上驱动不同的颜色。我的Pic是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
//**********************************//

1 个答案:

答案 0 :(得分:2)

每次循环时都在更新PWM。

计数器不太可能有时间到期,因此在它有机会“完成”PWM循环之前你会继续重置它。

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

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