2013-02-14 65 views
0

我工作的项目,我需要显示不同的颜色上的RGB领导。我使用pwm在LED上驱动不同的颜色。我的PIC是PIC24FJ64GA004,现在我正在使用它。这个项目的基本概念是使用开关来控制颜色。在RGB参数PWM设置在PIC24FJ64GA002

颜色的LED将根据今年天,一个月可以。为此,我使用带切换功能的7段数字计算日期和月份。

问题此刻正下面的代码。我正在尝试通过以下设置更改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 
//**********************************// 
+3

什么是“奇怪的事情”? – Throwback1986 2013-02-14 15:56:20

+0

你能更清楚地定义a)你在引脚上得到了什么,b)你期望在引脚上看到什么? – 2013-02-14 16:03:01

+0

嗨,我想要引脚上的可变PWM。因为我使用三个通道的RGB LED进入LED的不同引脚。我想让LED给我看不同的颜色。 – user12318 2013-02-14 16:08:27

回答

2

您每次都在更新PWM周期。

这是非常不可能的柜台有时间到期,因此,你不断进行复位它有机会去“做”一个PWM周期之前。

在循环结束后,你需要等待(至少一个)您的PWM已经过期。

的另一种方式可能是建立在你的循环希望下一个值,并有一个中断服务程序拷贝到PWM寄存器时,计时器到期。发生这种情况后,您可以设置下一个值:)您需要注意如何在主循环和ISR之间传输值,因为它们实际上是不同的“线程”上下文。

+0

你好,有可能举一些例子。我是嵌入式C的新手,需要一些指导。我了解你的概念,但我不知道如何实施。即使小演示将非常有用。在此先感谢 – user12318 2013-02-15 12:24:33

+0

对不起,我对你的特定芯片不​​够熟悉,只是甩掉了一些我认为可行的代码......而我现在还没有时间去创造一些严谨的东西。 – 2013-02-15 14:06:07