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

PIC24 and External CLK for TIMER1

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



Joined: 03 Dec 2013
Posts: 215

View user's profile Send private message

PIC24 and External CLK for TIMER1
PostPosted: Fri Jun 19, 2015 9:02 pm     Reply with quote

I need to run TIMER1 from a DS32KHz osc. chip.

It seems simple enough but this code does not work.

Compiler: V5.047

Code:

#include <24FJ128GA306.h>
#build (stack=256)
#FUSES NOWDT                   
#FUSES NOJTAG                   
#FUSES CKSFSM                   
#FUSES SOSC_DIG  // set SOSC as digital Inputs               

#device ICD=TRUE
#device ICSP=2
#device adc=12
#use delay(crystal=20000000)

int32 n32T1;
int1 n1NextSample;

////////////////////////
//
#INT_TIMER1
T1_ISR()
{
  set_timer1(65372);
  n32T1++;
  n1NextSample=1;
}
///////////////////////////
//
void main()
{
 
 n32T1=0;
 n1NextSample=0;
 
//setup_timer1(T1_EXTERNAL_SYNC |TMR_DIV_BY_1); // does not work

 setup_timer1(TMR_EXTERNAL | TMR_DIV_BY_1); // 32KHz input at pin 47 RC13

 set_timer1(65372);

 enable_interrupts(INT_TIMER1);
 
 enable_interrupts(INTR_NORMAL);
 
 while(1)
 {
   n1NextSample=0;
   while(!n1NextSample){;}
   
   output_toggle(PIN_B15); // for oscilloscope
 }
 
}


If I change to internal clock the code works but wrong frequency of course.

setup_timer1(TMR_INTERNAL | TMR_DIV_BY_1);

Any suggestions as to what I'm doing wrong.
Ttelmah



Joined: 11 Mar 2010
Posts: 19451

View user's profile Send private message

PostPosted: Sat Jun 20, 2015 9:42 am     Reply with quote

T1_EXTERNAL, uses the T1CK input, not the secondary oscillator.
You either need to program the T1CK input with #pin_select, or tell the timer to use the secondary oscillator. Add:
Code:

#define T1_SECONDARY 0x0002
#define T1_SECONDARY_SYNC 0x0006


and use these in your timer setup.

However, then 'beware'. The timer takes it's input from the secondary oscillator _output_, not it's input pin (look at figure 12-1). The secondary oscillator is not enabled, so the input pin is not going to work.....

Generally, you only use the secondary oscillator, if you are actually enabling the secondary oscillator. I'm not sure you can use this as an input....

I'd personally say you'd be much better to feed your clock into one of the relocatable pins and enable the Timer input:

#PIN_SELECT T1CK = PIN_D1
(for example)
T1_EXTERNAL_SYNC will then work.

General comments. Your interrupt routine, should be void (both on what it returns, and what it receives).
Get rid of setting the timer 'to' a value. I suspect you are actually wanting to count 164, to give approximately 200Hz? (no comments makes it hard to know). If so, program the timer to this value:
Code:

setup_timer1(T1_EXTERNAL_SYNC| TMR_DIV_BY_1, 163);
jeremiah



Joined: 20 Jul 2010
Posts: 1339

View user's profile Send private message

PostPosted: Sat Jun 20, 2015 10:30 am     Reply with quote

I am not as familiar with using an actual oscillator chip for timer1. I tend to use a 32kHz crystal. I know with the crystal I have to use the unlock sequence by specifying

T1_EXTERNAL_RTC

That tells the compiler you are using the secondary oscillator and it will include the unlock sequence.

Is that needed for their external chip as well or just for crystals?
soonc



Joined: 03 Dec 2013
Posts: 215

View user's profile Send private message

Interesting
PostPosted: Sat Jun 20, 2015 10:44 am     Reply with quote

Thank you Ttelmah.

My reason for using pin 47 as the input for the secondary osc. is what I read in the data sheet which read as if it can be done ! In light of this text the schematic looks strange but I'm not convinced it's detailed enough.

Source: DS30009996G-page 152

9.5.2 EXTERNAL (DIGITAL) CLOCK
MODE (SCLKI)
The SOSC can also be configured to run from an
external 32 kHz clock source, rather than the internal
oscillator. In this mode, also referred to as Digital mode,
the clock source provided on the SCLKI pin is used to
clock any modules that are configured to use the
Secondary Oscillator. In this mode, the crystal driving
circuit is disabled and the SOSCEN bit (OSCCON<1>)
has no effect.

I'll try your suggestion this afternoon, I'm sure it will work.

Also the CCS header file for this chip has a list of valid input pins using pin_select, and T1CK is not in alphabetical order but tucked in on the third line. ! When I read your suggestion I thought well but I did not see T1CK.... sure enough it is there.
Thanks I'll report back later.
soonc



Joined: 03 Dec 2013
Posts: 215

View user's profile Send private message

OK it works
PostPosted: Sat Jun 20, 2015 3:09 pm     Reply with quote

Code:
#FUSES SOSC_DIG  // disable xtal osc and use as digital

#pin_select T1CK = PIN_C14

setup_timer1(T1_EXTERNAL_SYNC | TMR_DIV_BY_1, 163);

////////////////////////
//
#INT_TIMER1
T1_ISR()
{
  n32T1++;
  n1NextSample=1;
}

Pin C14 is the SOSCO yet the PPS has it listed as an Input Only (RPI) pin.

Thanks that did the trick.
Ttelmah



Joined: 11 Mar 2010
Posts: 19451

View user's profile Send private message

PostPosted: Sun Jun 21, 2015 3:26 am     Reply with quote

Good. Smile

Yes the logic here is complex. The SOSCO pin is an output, from the secondary oscillator gate, or C14. As a relocatable peripheral, it only supports input.

It's nice that they do have the supported PIN_SELECT names for this chip. Unfortunately a lot of the chips don't have this in the include file, which can be a real pain. Being slightly 'out of order', is a minor annoyance!. Smile
soonc



Joined: 03 Dec 2013
Posts: 215

View user's profile Send private message

PostPosted: Sun Jun 21, 2015 11:27 am     Reply with quote

Ttelmah wrote:
Good. Smile

Yes the logic here is complex. The SOSCO pin is an output, from the secondary oscillator gate, or C14. As a relocatable peripheral, it only supports input.

It's nice that they do have the supported PIN_SELECT names for this chip. Unfortunately a lot of the chips don't have this in the include file, which can be a real pain. Being slightly 'out of order', is a minor annoyance!. Smile


I reorganized the info and saved it to my project include file:

Code:
////////////////////////////////////////////////////////////////// PIN_SELECT
// #pin_select function=pin
// Valid Pins:
//    PIN_B0,PIN_B1,PIN_B2,
//    PIN_B4,PIN_B5,PIN_B6,PIN_B7,PIN_B8,PIN_B9, 
//    PIN_B14,PIN_B15,
//
//    PIN_C14,
//
//    PIN_D0,PIN_D1,PIN_D2,PIN_D3,PIN_D4,PIN_D5,
//    PIN_D8,PIN_D9,PIN_D10,PIN_D11,
//
//    PIN_F2,PIN_F3,PIN_F4,PIN_F5,PIN_F6
//
//    PIN_G6,PIN_G7,PIN_G8,PIN_G9,
// Input Functions:
//    INT1,INT2,INT3,INT4,
//    T1CK,T2CK,T3CK,T4CK,T5CK,
//    IC1,IC2,IC3,IC4,IC5,IC6,IC7,
//    OCFA,OCFB,
//    U1RX,U1CTS,U2RX,U2CTS,U3RX,U3CTS,U4RX,U4CTS
//    SDI1,SDI2,
//    SS1IN,SS2IN,
//    SCK1IN,SCK2IN,
//    MDMIN,
//    MDCIN1,MDCIN2,
// Output Functions:
//    NULL,
//    C1OUT,C2OUT,C3OUT,
//    MDOUT,
//    U1TX,U1RTS,U2TX,U2RTS,U3TX,U3RTS,U4TX,U4RTS,
//    SDO1,SCK1OUT,SS1OUT,SDO2,SCK2OUT,
//    SS2OUT,
//    OC1,OC2,OC3,OC4,OC5,OC6,OC7,
//

Thanks again and for the TIMER1 setup tip. Now it feels more like the 68H05 timers that I used for years.
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