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 C0 of 16LF1938

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



Joined: 21 Mar 2018
Posts: 48

View user's profile Send private message

PWM on C0 of 16LF1938
PostPosted: Thu Nov 21, 2019 4:58 pm     Reply with quote

Code:
 setup_timer_2(T2_DIV_BY_16, 250, 1); // F = 4E6/(16*250*1) = 1KHz
 setup_timer_4(T4_DIV_BY_16, 250, 1); // F = 4E6/(16*250*1) = 1KHz
 setup_ccp1(CCP_PWM); // setup CCP1 as PWM
 setup_ccp2(CCP_PWM); // setup CCP2 as PWM
 setup_ccp3(CCP_PWM | CCP_TIMER4 | CCP_P2B_C0);
 set_pwm3_duty(125);


I'm using Timer 2 for PWM1 and PWM 2 and both work fine. I'm trying to get a PWM signal out of C0 using Timer 4 but not getting anything. Thanks for having a look. Compiler is v5.080
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 21, 2019 5:07 pm     Reply with quote

I don't use that PIC but....
If that PIC has 'programmable pins', either 'pin select' or through the APFregisters, you'll need to 'assign' the pin BEFORE the setup...pwm...code


Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 21, 2019 7:50 pm     Reply with quote

Looking at the .LST file, it looks like the wrong pin is used for TRIS.
Quote:

.................... setup_ccp3(CCP_PWM | CCP_TIMER4 | CCP_P2B_C0);
003F: MOVLB 02
0040: BCF APFCON.CCP3SEL
0041: MOVLB 01
0042: BCF TRISC.TRISC6
0043: MOVLB 02
0044: BCF LATC.LATC6
0045: MOVLW 0C
0046: MOVLB 06
0047: MOVWF CCP3CON
0048: CLRF PWM3CON
0049: CLRF CCP3AS
004A: MOVLB 05
004B: BSF CCPTMRS0.C3TSEL0
004C: BCF CCPTMRS0.C3TSEL1
004D: MOVLW 01
004E: MOVLB 06
004F: MOVWF PSTR3CON


Try adding the line shown below in bold. Put it in the position shown.
Quote:

setup_timer_2(T2_DIV_BY_16, 250, 1); // F = 4E6/(16*250*1) = 1KHz
setup_timer_4(T4_DIV_BY_16, 250, 1); // F = 4E6/(16*250*1) = 1KHz
setup_ccp1(CCP_PWM); // setup CCP1 as PWM
setup_ccp2(CCP_PWM); // setup CCP2 as PWM
setup_ccp3(CCP_PWM | CCP_TIMER4 | CCP_P2B_C0);
output_low(PIN_C0);
set_pwm3_duty(125);
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Nov 22, 2019 1:46 am     Reply with quote

Personally if it is a TRIS problem I'd just use:

output_drive(PIN_C0);

Which will just set the TRIS to 0 without changing the output latch.

Doesn't 'matter', since the PWM will override the latch value, but seems
a tiny fraction 'tidier'... Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 22, 2019 3:13 am     Reply with quote

At first I thought that too, but I decided to copy what CCS does in the
posted .LST file code.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Nov 22, 2019 3:19 am     Reply with quote

Interesting.
I suspect this is done on the assumption that if this is a PWM, then if the PWM
is disabled, the output should go low for safety. Obviously a wrong assumption
if this is for example the 'high' transistor of a pair, but likely to be right
a lot of the time. So if you then disable the PWM on the pin, it'll have the
latch set low.
Good reason, and makes sense. Smile
randy.shaffer



Joined: 21 Mar 2018
Posts: 48

View user's profile Send private message

PostPosted: Fri Nov 22, 2019 9:14 am     Reply with quote

Thanks so much for the replies. I may be trying to use the wrong CCP & pin. The data sheet shows CCP3 at Pins C6 or B6 and C0 is P2B. Do I have to use CCP2 for C0? Ultimately, I'm hoping to get PWM at C0 that is based on a different timer than the ones used on C1 & C2.
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