View previous topic :: View next topic |
Author |
Message |
beaker404
Joined: 24 Jul 2012 Posts: 163
|
PWM on 16F876 |
Posted: Wed Mar 13, 2019 3:26 pm |
|
|
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
|
|
Posted: Wed Mar 13, 2019 3:58 pm |
|
|
Show us the rest of the test program. Show us the line that sets the
pwm duty cycle. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19585
|
|
Posted: Wed Mar 13, 2019 11:58 pm |
|
|
Have a look at this thread:
<http://www.ccsinfo.com/forum/viewtopic.php?t=57767> |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 306
|
|
Posted: Thu Mar 14, 2019 5:49 am |
|
|
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: 19585
|
|
Posted: Thu Mar 14, 2019 9:19 am |
|
|
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: 306
|
|
Posted: Thu Mar 14, 2019 9:33 am |
|
|
You are right. I shouldn't post so early without sufficient caffeine. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Thu Mar 14, 2019 10:15 am |
|
|
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. |
|
|
|