2013-02-14 46 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