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 support@ccsinfo.com

PWM on 16F876

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



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

PWM on 16F876
PostPosted: Wed Mar 13, 2019 3:26 pm     Reply with quote

Doing a PWM at 50% duty cycle at 1000Hz with the following code:
Code:
 
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM
setup_timer_2(T2_DIV_BY_4, 249L, 1); // 1000 Hz
   


That works fine, wanted to drop to 500Hz and this code does not work. Just sets the pin to high.

Code:
 setup_timer_2(T2_DIV_BY_16, 124L, 1); // 500 Hz


what am I missing?

Windows 10
PIC16F876 @ 4MHz
PCB,PCM,PCH,PCD V5.064
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 13, 2019 3:58 pm     Reply with quote

Show us the rest of the test program. Show us the line that sets the
pwm duty cycle.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Mar 13, 2019 11:58 pm     Reply with quote

Have a look at this thread:

<http://www.ccsinfo.com/forum/viewtopic.php?t=57767>
gaugeguy



Joined: 05 Apr 2011
Posts: 286

View user's profile Send private message

PostPosted: Thu Mar 14, 2019 5:49 am     Reply with quote

If the first section produces 1000Hz then I would expect the second statement to produce 125Hz. You are making the timer 8 times slower.

T2_DIV_BY_4 -> T2_DIV_BY_16 and 249L -> 124L
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Mar 14, 2019 9:19 am     Reply with quote

No, smaller count means faster.

/4 on the prescaler, then only half the count (so double speed). I think
the most likely this is he is not changing the duty cycle, so the waveform
stays high...
gaugeguy



Joined: 05 Apr 2011
Posts: 286

View user's profile Send private message

PostPosted: Thu Mar 14, 2019 9:33 am     Reply with quote

You are right. I shouldn't post so early without sufficient caffeine.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Thu Mar 14, 2019 10:15 am     Reply with quote

Slightly unrelated to the problem at hand, but why not use #use pwm()?

I started off setting up PWM the way beaker has, but I found #use pwm to be much easier to set frequency, duty cycle, etc.
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