我工作的項目,我需要顯示不同的顏色上的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
//**********************************//
什麼是「奇怪的事情」? – Throwback1986 2013-02-14 15:56:20
你能更清楚地定義a)你在引腳上得到了什麼,b)你期望在引腳上看到什麼? – 2013-02-14 16:03:01
嗨,我想要引腳上的可變PWM。因爲我使用三個通道的RGB LED進入LED的不同引腳。我想讓LED給我看不同的顏色。 – user12318 2013-02-14 16:08:27