View previous topic :: View next topic |
Author |
Message |
ve4edj
Joined: 08 May 2014 Posts: 3
|
0% PWM issue on 16F1827 - [SOLVED] |
Posted: Thu May 08, 2014 9:27 am |
|
|
I am trying to fade LEDs connected to a PIC16F1827 on and off based on a time read from a RTC chip (MCP79410). But at the moment, I am having an issue with 0% PWM not turning the LED off (it gets fairly dim, but is NOT off)
My setup code:
Code: | void initPWMs() {
setup_ccp2(CCP_PWM);
set_pwm2_duty(0);
setup_ccp3(CCP_PWM);
set_pwm3_duty(0);
setup_ccp4(CCP_PWM);
set_pwm4_duty(0);
setup_timer_2(T2_DIV_BY_4, 100, 1);
APFCON0 |= 0x08;
}
|
At this point all three LEDs are off. However, after adding this code:
Code: | if (seconds % 2 == 0) {
set_pwm2_duty(1023);
set_pwm3_duty(0);
} else {
set_pwm2_duty(0);
set_pwm3_duty(1023);
}
|
The LEDs alternate between very bright and kind of dim every second. Can anybody shed some light on this issue? Thanks in advance!
--- EDIT ---
Forgot to mention the compiler version I am using. I'm on v5.023
Last edited by ve4edj on Thu May 08, 2014 10:11 am; edited 1 time in total |
|
|
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Thu May 08, 2014 10:02 am |
|
|
Don't remember if this is well documented in the manual. The PWM is 10 bit in this family. set_pwmx_duty(0) only clears the MSB of the duty register. The lower two bits could still be hi. Check your datasheet.
Fix With Cast:
Code: | set_pwm2_duty(0L);
002C: MOVLB 05
002D: CLRF CCPR2L
002E: MOVF CCP2CON,W
002F: ANDLW CF
0030: MOVWF CCP2CON
0031: GOTO 02D |
Code: | set_pwm2_duty(0);
002C: MOVLB 05
002D: CLRF CCPR2L
002E: GOTO 02D |
Explicitly casting it as a long will get the compiler to address the two lowest bits in the duty register. |
|
|
ve4edj
Joined: 08 May 2014 Posts: 3
|
|
Posted: Thu May 08, 2014 10:04 am |
|
|
stinky wrote: | Don't remember if this is well documented in the manual. The PWM is 10 bit in this family. set_pwmx_duty(0) only clears the MSB of the duty register. The lower two bits could still be hi. Check your datasheet.
|
I must have read through that manual 50 times, and I'm almost sure that is mentioned nowhere. Thanks for the prompt reply, I'll try it out and let you know! |
|
|
ve4edj
Joined: 08 May 2014 Posts: 3
|
|
Posted: Thu May 08, 2014 10:10 am |
|
|
Problem solved. Thanks @stinky!!! |
|
|
|