Using crystals with PICs and their accuracy

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hi,
I am in the habit of using crystals when making PCBs with PICs. What are the reasons for using crystals, and are they really necessary?
Camerart.
 

bertus

Joined Apr 5, 2008
22,270
Hello,

Depends. Some pics do have internal oscillators, some don't.
Most times a crystal will be more stable as the internal oscillator.
See the datasheet of the pic for more details.

Bertus
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
Hello,

Depends. Some pics do have internal oscillators, some don't.
Most times a crystal will be more stable as the internal oscillator.
See the datasheet of the pic for more details.

Bertus
Hi B,
The PICs I use all have internal Oscillators.
I suppose I'm thinking about timing. I actually doubt that the project I'm working on: https://forum.allaboutcircuits.com/watched/threads
needs very accurate timing, and as I'm making new PCBs I would like to leave them off, for easier track laying.
Thanks, C.
 

MrChips

Joined Oct 2, 2009
30,706
If your project requires UART serial communications then use a crystal for frequency stability.
If you have no strict requirements with regards to frequency then you can use internal oscillator or external RC components.

BTW, when your application requires a crystal you need to be careful on how you lay out the crystal and loading capacitors on the PCB. Otherwise you may end up with wonky behaviour.
 

jpanhalt

Joined Jan 18, 2008
11,087
Usually, you want the PIC at some high frequency, e.g., 8 MHz to 32 MHz. I use the internal oscillator for that. Then use a tuning fork crystal for Timer1 so you can get seconds easily and more accurately.
 

AlbertHall

Joined Jun 4, 2014
12,345
My present project uses an 18.432MHz crystal. It accurately divides down for baud rates between 1200 and 115,200 and also to 1ms for a timebase.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,724
If your project requires UART serial communications then use a crystal for frequency stability.
If you have no strict requirements with regards to frequency then you can use internal oscillator or external RC components.

BTW, when your application requires a crystal you need to be careful on how you lay out the crystal and loading capacitors on the PCB. Otherwise you may end up with wonky behaviour.
Hi Mr C,
As I'm using UARTs, I'll carry on adding a crystal.
Thanks to all, C.
 

MrAl

Joined Jun 17, 2014
11,389
Hi,
I am in the habit of using crystals when making PCBs with PICs. What are the reasons for using crystals, and are they really necessary?
Camerart.
Using crystals seems straightforward but it is actually a little tricky.
I recommend reading MicroChips app note about using crystals. It talks about loading capacitors and stuff like that.

Generally you use a crystal when you need better than about 1 percent frequency accuracy. A crystal can easily be only 100 parts per million which is 0.01 percent (one hundredth of one percent) and even though that is not good enough for a wall clock it si good for RS232 timing and stuff like that. If you want to do a wall clock you should look into a temperature compensated crystal IC chip which has onboard correction with temperature, or use a quality real time clock board (RTC) which interfaces with any uC chip.
 
Internal oscillators have come a long way with respects to initial tolerance. Most times it will be accurate enough.

But......over the whole temperature range, or certain critical circuits, you may require an external crystal.

Having said this, I have had excellent results with ceramic resonators. They are not as accurate as a properly loaded crystal, but it is better than the internal oscillator.
Another advantage is that it has already the required load capacitors, so they are essentially plug and play.
 

John P

Joined Oct 14, 2008
2,025
Here is the internal oscillator accuracy graph for the processor I'm using most lately, the PIC16F18345, where I use the internal oscillator and it seems to be fine, though obviously I'm not expecting precision timing. Basically they're claiming that you'd be in a 3-sigma band that's within 2% of nominal accuracy over the rated power supply and temperature range (except below 3V supply, it can deviate slightly more). I'm not a statistician, but I think that means it'll meet the limits 99.7% of the time, although the ominous word "typical" does appear. Maybe "typical" just means it's a variable that they can't totally control, but they're telling you the limits. It would be interesting to hear whether any units that fail to meet the 2% limit are "rogues" that you could test and discard, or whether it's a random occurrence for any particular unit (which I doubt, but there's no spec for it).

A 2% error seems to be good enough to operate a UART with another highly accurate UART and never drop bits. If a character has 10 bits, wouldn't any error have a maximum of 20% of a bit time at the end of a character? That's still an operable link. But if you had one PIC talking to another, and they were both using the internal oscillator, and one was 2% high and the other was 2% low, then it looks as if things would be pretty marginal.
 

Attachments

Last edited:

MrAl

Joined Jun 17, 2014
11,389
Here is the internal oscillator accuracy graph for the processor I'm using most lately, the PIC16F18345, where I use the internal oscillator and it seems to be fine, though obviously I'm not expecting precision timing. Basically they're claiming that you'd be in a 3-sigma band that's within 2% of nominal accuracy over the rated power supply and temperature range (except below 3V supply, it can deviate slightly more). I'm not a statistician, but I think that means it'll meet the limits 99.7% of the time, although the ominous word "typical" does appear. Maybe "typical" just means it's a variable that they can't totally control, but they're telling you the limits. It would be interesting to hear whether any units that fail to meet the 2% limit are "rogues" that you could test and discard, or whether it's a random occurrence for any particular unit (which I doubt, but there's no spec for it).

A 2% error seems to be good enough to operate a UART with another highly accurate UART and never drop bits. If a character has 10 bits, wouldn't any error have a maximum of 20% of a bit time at the end of a character? That's still an operable link. But if you had one PIC talking to another, and they were both using the internal oscillator, and one was 2% high and the other was 2% low, then it looks as if things would be pretty marginal.
Yes those scenarios are why crystals are recommended for some apps. There is also the aging issue.
I've used the internal oscillator for RS232 also, but never subjected the chip to any extremes.
 

MrChips

Joined Oct 2, 2009
30,706
With a 10-bit character you want to be within half a bit at the end of the character. That's 5% error.
Now suppose the other unit is off in the opposite direction, now you're down to 2%.
Add to that, variation with supply voltage and temperature, you're down to 1%
Give yourself some margin for error and you're now at 0.5%.

So if you want reliable operation, the oscillator should be stable to 0.5% or better.
 

John P

Joined Oct 14, 2008
2,025
How are you getting that, Mr Chips? I agree with your half a bit and 5%, and that would be made up from the errors in two processors, so say 2% each. But the graph I showed which indicates 2% error does claim to cover all combinations of temperature and supply voltage. In fact it goes 2% low, but only about 1.5% high, so it's somewhat more favorable. Also, a lot of the time we'd be using a processor connected to a piece of equipment that did have a crystal, and would be very accurate, so the error would only be on one side.

I wonder if it's feasible to self-correct a processor's oscillator using the tuning feature, based on incoming bit times as seen on a UART. Or maybe define one processor's oscillator as master, and make the other(s) match it.
 

MrChips

Joined Oct 2, 2009
30,706
You are making a rather optimistic assumption that the other party is using a crystal. That is no guarantee.

Furthermore, suppose the other party is using an 8MHz crystal which is a common MCU value, and your UART is set for 9600 baud. The rate divisor is 52.
Hence the actual baud is 8000000 / 16 / 52 = 9615 baud which is 0.16% error.
So even before you begin, there is already a 0.16% error.

The error could be worse depending the the crystal frequency being used.

So you really don't know the error from the other party whether or not they are using a crystal.
 

MrChips

Joined Oct 2, 2009
30,706
If the crystal is 20MHz and the baud is 115200 the error is worse.
The divisor is 11.
Hence the actual baud is 20000000 / 16 / 11 = 113636
The error is 1.4%.

This is why you really need a 18.432MHz crystal.

Note however, if your two boards are using crystals of the same frequency (or multiples of the same frequency or some integer fractions) then your error is practically 0%.
 
Top