View previous topic :: View next topic |
Author |
Message |
apakSeO
Joined: 07 Dec 2016 Posts: 60 Location: Northeast USA
|
PIC18: How to select which timer is used by ECCP1/2 for PWM? |
Posted: Thu Jan 12, 2017 2:04 pm |
|
|
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
|
|
Posted: Thu Jan 12, 2017 9:02 pm |
|
|
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
|
|
Posted: Fri Jan 13, 2017 2:24 am |
|
|
You don't have to fiddle.
It's just designed to confuse!...
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
|
|
Posted: Fri Jan 13, 2017 3:37 am |
|
|
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
|
|
Posted: Fri Jan 13, 2017 4:08 am |
|
|
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. |
|
|
|