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

PIC18: How to select which timer is used by ECCP1/2 for PWM?

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



Joined: 07 Dec 2016
Posts: 60
Location: Northeast USA

View user's profile Send private message

PIC18: How to select which timer is used by ECCP1/2 for PWM?
PostPosted: Thu Jan 12, 2017 2:04 pm     Reply with quote

Using CCS v5.065, MPLAB X, ICD3, and a PIC18F46J50 44-pin QFN device.

In the datasheet for PIC18F46J50, the register TCLKCON and its associated bits T3CCP<2:1> control the source of timing for ECCP1 and ECCP2.



In my application, I am using ECCP2 and I want to choose timer4 as the source for ECCP2 setup to a PWM mode. From the datasheet pictured above, if ECCP2 is chosen for PWM mode, it appears the options I can use are Timer4 and Timer2. I cannot find a reference in the CCS User Guide/Manual on how to modify the timer source of any ECCP module.

How do I tell CCS that I want to source of ECCP2's timing to come from Timer2 vs. Timer4? Is there a method I am missing here? Is this possible to do through CCS syntax or must I manually modify these registers?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 12, 2017 9:02 pm     Reply with quote

I don't know if CCS supports this. But here is a work-around function
and a test program that shows how to call it:

Code:
#include <18F46J50.h>
#fuses INTRC_IO,NOWDT
#use delay(clock=4M)

// Add the following block of code to your program.
// Place it above main().
//-----------------------------------
// Call the function select_pwm_timer() with one
// of the following parameters to select Timer2
// and/or Timer4 for the ECCP modules.

#define ECCP_BOTH_T2        0x00
#define ECCP_BOTH_T4        0x02
#define ECCP1_T2_ECCP2_T4   0x01 

#byte TCLKCON = getenv("SFR:TCLKCON")
#define select_pwm_timer(x) TCLKCON = x
//------------------------------------


//==============================================
void main()
{
select_pwm_timer(ECCP1_T2_ECCP2_T4);  // Then call it

while(TRUE);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19529

View user's profile Send private message

PostPosted: Fri Jan 13, 2017 2:24 am     Reply with quote

You don't have to fiddle.
It's just designed to confuse!... Very Happy

If you look, the bit is the same one that if you are using the CCP, selects timer3. CCS include it in the timer3 setup.
So though you want to use timer4, you have to set this up on timer3:
Code:

   setup_timer_3(T3_CCP1); //if you are using T3, then or the other settings
   //OR
   setup_timer_3(T3_CCP1_TO_2); //same comment

These respectively give the 01, and 10 settings for the bits in TCLKCON.

Though they forget to tell you that this affects the PWM......
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 13, 2017 3:37 am     Reply with quote

I made a test program using your two lines:
Code:
#include <18F46J50.h>
#fuses INTRC_IO,NOWDT
#use delay(clock=4M)

//===========================
void main(void)
{
setup_timer_3(T3_CCP1);
setup_timer_3(T3_CCP1_TO_2);

while(TRUE);
}


It produces this in the .LST file. It's not writing to TCLKCON:
Code:
.................... setup_timer_3(T3_CCP1); 
0026:  MOVLW  01
0028:  MOVWF  T3GCON
002A:  CLRF   T3CON
.................... setup_timer_3(T3_CCP1_TO_2); 
002C:  MOVLW  02
002E:  MOVWF  T3GCON
0030:  CLRF   T3CON
Ttelmah



Joined: 11 Mar 2010
Posts: 19529

View user's profile Send private message

PostPosted: Fri Jan 13, 2017 4:08 am     Reply with quote

OK. Well that's just a fault with how they have it done then. The bit is meant to control the timer selected.

It's funny actually this may be recent. Will have to do some research. Reason is I have a program that uses T4 for a PWM on exactly this chip, and found it would not work after a particular compiler version. Since I didn't need any of the features from the later compiler, I just flagged it as 'must use x.xxx', and kept using the old compiler, and didn't bother to work out what was wrong...
Suspect you may have identified what went wrong. Smile
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