CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

0% PWM issue on 16F1827 - [SOLVED]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ve4edj



Joined: 08 May 2014
Posts: 3

View user's profile Send private message

0% PWM issue on 16F1827 - [SOLVED]
PostPosted: Thu May 08, 2014 9:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 08, 2014 10:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 08, 2014 10:04 am     Reply with quote

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! Smile
ve4edj



Joined: 08 May 2014
Posts: 3

View user's profile Send private message

PostPosted: Thu May 08, 2014 10:10 am     Reply with quote

Problem solved. Thanks @stinky!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group