| View previous topic :: View next topic |
| Author |
Message |
SkeeterHawk
Joined: 16 Oct 2010 Posts: 63 Location: Florissant, CO
|
| Using a Crystal just for Timer1 |
Posted: Thu Jul 16, 2026 12:35 pm |
|
|
Hello Friends,
I want to make sure that I am on the right track, so I would appreciate your input.
What I want to do is simple. I have a PIC18F57Q84 that I would like to have an accurate timer on so that I can count every 10mS with only a small amount of deviation in about 15 minutes or so. After doing some research, it appears that Timer1 is the one to use. To use the internal oscillator for the system clock, and only use the crystal on Timer1, is this how I would configure it?
Drop crystal across RA6 & RA7
| Code: |
#FUSES XT // Enable the crystal
#use delay(internal=64Mhz) // Set the system clock to use the internal 64MHz clock
SETUP_TIMER_1(T1_EXTOSC)
#INT_TIMER1
void TenMSTimer_isr (void)
{
set_timer1(0xD8F0); // 65536 - 10000
} |
Is that basically what I would have to do? Thanks so much for the input!! I've already told my customer that I can do this, so I want to make sure that I don't make a mistake. Thanks again for the feedback!
Jonathan _________________ Life is too short to only write code in assembly... |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9648 Location: Greensville,Ontario
|
|
Posted: Thu Jul 16, 2026 4:47 pm |
|
|
I had a very quik look at the data sheet....
don't see how you can 'just add a crystal as the ext clk to timer 1
1st it's a PPS PIC....
2nd no 'clk osc' section ahead of the timer1 input.
I suggest using a real crystal+2 caps for the main PIC clock, then using it as the input to timer1
This gives you a very stable 'main clock' so ALL PIC timings will be accurate, doesn't cost any 'real estate' either !
Others that use that PIC , or family will know more than my 'gut'. |
|
 |
SkeeterHawk
Joined: 16 Oct 2010 Posts: 63 Location: Florissant, CO
|
|
Posted: Thu Jul 16, 2026 7:57 pm |
|
|
Thank you very much for interjecting, Temtronic!
I must admit that I "cheated" at the onset and asked AI this question. Here are my thoughts, and is why I am asking you more experienced developers on here to make sure that I am on the right track.
In my datasheet for the PIC18F57Q84 (DS40002213F) it has Timer 1 as chapter 25. In the initial bullet points (P.405), the second bullet says | Quote: | | Programmable internal or external clock source | On page 406, it says to | Quote: | | See TxCLK for clock source selections | and this brings you to table 25-4 on P416. Selection 01000 call out for Timer1 to use the "EXTOSC", but that is under the column Timer3...but I don't think that it is actually for Timer3. Instead, I think that it is saying that at register address 0x321, that addresses Timer1 and writing 01000 to that register should use the external crystal. Table 24.14 on page 420 (Registry Summary) seems to confirm this theory.
I would be surprised if you could reassign the oscillator using PPS because of the impedance issues with a crystal. The PPS table only says that RA6 & RA7 are UART3 CTS and UART3 Rx respectively...which are both inputs.
It is interesting to note that figure 12-1 on page 207 shows the clock sources. It shows CLKIN/OSC1 and CLKOUT/OSC2 as going through the PLLEN as going "to peripherals". It also shows the SOSCI & SOSCO as going "to peripherals" along with the LFINTOSC. Looking at the description for register OSCEN in table 12.5.8 on page 231 says that bit 0 PLLEN just selects whether the PLL is used to boost the EXTOSC by four, but it appears to be available to the peripherals regardless. Additionally, in the same table bit 7 is EXTOEN which is clear by default and it says | Quote: | | EXTOSC can be enabled by a peripheral request | . I think that if I assign the NOSC bits in table 12-2 on page 215 to "110", I will be using the HFINTOSC for the system clock, and my peripheral (Timer1) should be able to use the EXTOSC.
As you look for other EXTOSC reference in the data sheet, it appears that you can assign the EXTOSC to be the clock for just about anything...like UARTS, etc. _________________ Life is too short to only write code in assembly... |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20110
|
|
Posted: Fri Jul 17, 2026 1:28 am |
|
|
You can do this.
First thing though is that the XT fuse is not wanted.
What you do is use the secondary oscillator. This chip offers routing this
to other peripherals (on most chips it'll only go to Timer1 by default).
You need the fuse RSTOSC_SOSC which enables the secondary oscillator.
This is then routed to Timer1 using T1_SOSC in the timer1 setup.
This oscillator uses the SOSCI and SOSCO pins that are not remappable.
Pins RC0 and RC1. You cannot put these on RA6/7,
The SOSC pins are special. Offering very low power operation and a low drive
for a 32KHz crystal.
You could do it using an external oscillator, then your EXTOSC setting would
be right, but you would need an oscillator module not just a crystal. |
|
 |
SkeeterHawk
Joined: 16 Oct 2010 Posts: 63 Location: Florissant, CO
|
|
Posted: Fri Jul 17, 2026 12:09 pm |
|
|
Thank you very much for your input, as always, Ttelmah!
I was planning to use a 4MHz crystal (Sorry I didn't mention that detail in my original post), and I was under the impression that the SOSC could only power a 32.768kHz crystal and I wouldn't be able to get an accurate 10mS tick from that frequency.
I can use an oscillator module if that is my option. I would rather keep this counter internal to the chip than trying to find a real time clock that will give me 10mS ticks since this is a pretty straight forward application. I'd prefer not to add a bunch of bus traffic to my timing because the timing is critical on this application.
Is it true that I can't run a 4MHz crystal on either the SOSC or the EXTOSC and route it to Timer1, would I be running an external oscillator to SOSCIN (RC1) or to CLKIN (RA7)? I am assuming that it is RA7 since you said to keep the EXTOSC. So, if I am required to use an external oscillator, I am thinking that CB3-3C-4M000000 from CTS (Digi-Key P/N 110-CB3-3C-4M000000CT-ND) would be a good choice.
The goal here is to have a really accurate timer that counts 100 "ticks" a second and is accurate to at least 15 minutes, but preferably to 30 minutes. I want to have this as an internal timer so I can clear the counter and start the timer counting at exactly the same time as I toggle an I/O pin. If it is better to use another timer and possibly an external trigger source, that would be good too (Like TOCKI/T0CKIPPS on Timer0). I don't really care how it gets done as long as it is accurate.
Thanks so much for your input to make sure that this application works out of the box!! _________________ Life is too short to only write code in assembly... |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20110
|
|
Posted: Fri Jul 17, 2026 12:55 pm |
|
|
Yes, the secondary oscillator can only power a 32KHz crystal.
Why would you want to use 4MHz????
Uses more power, and generally will not be as accurate. The 'point' about
the 32KHz crystals is that they are a different cut from standard crystals
that offers better temperature stability and accuracy, without having to
go to things like TCXO's. Hence for 'timers' they are the preferred crystal. |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 365
|
|
Posted: Fri Jul 17, 2026 1:33 pm |
|
|
If you need a 10ms interrupt then a 32kHz crystal will not work well.
You have not stated what your definition of accurate is. Crystal accuracy is usually given in parts per million (ppm) and temperature deviation in ppm/'C
Accurate over 15 minutes does not really give a definition of what the level of error tolerated would be. |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 365
|
|
Posted: Fri Jul 17, 2026 1:48 pm |
|
|
| Also instead of loading timer1 in the interrupt it will be more accurate to use the compare function and update the compare value in the interrupt (add the number of counts you want to the current compare value). That way the timer is free running. When writing the timer in the interrupt you need to figure in the interrupt latency and time to execute the write as well. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9648 Location: Greensville,Ontario
|
|
Posted: Fri Jul 17, 2026 1:49 pm |
|
|
OK, I/m curious..
Why not use the 4MHz xtal as your primary clock, then connect Timer1 to it 'somehow' ?
This will give you very accurate timing for the entire program. |
|
 |
|