Hello, @camerart. I was about to start a new thread so as to ask a question about the use of crystals in PIC MCU's when I happened to stumble upon yours. The reason I'm making my post here is because I think that it would not only make things easier for me, but also because I believe I can make a valuable contribution to this thread. If you feel that I'm somehow intruding or hijacking your thread, please let me know and I'll be more than glad to start a new one.
Here's my conundrum:
I've been working on a project that requires fairly precise timekeeping because the MCU (a PIC16LF1823) has the task of logging the moment an event (the event in question being when one of its input pins goes low) takes place.
For this purpose, I acquired several 32.7680KHz crystals (CFS-20632768HZFB) that boast an accuracy of ±5ppm. This should translate into an error of about ±7 seconds per year, if I'm not mistaken. So what I did is install said crystal in the MCU's circuitry so as to make it work as an asynchronous timing source for Timer1. And then I enabled Timer1's interrupt in such a way so as to never ever miss a single tick (one second) to be counted in its logic. I've gone through the code many times and so far I haven't found a fault within it.
Things worked rather nice at first, and everything seemed ok when I assembled the PCB and finished the code. I then tested it by leaving it counting seconds overnight (I had previously synched it with my PC's clock via a RS232-UART interface). You can imagine my disappointment when I checked how things were going the next morning and confirmed that my circuit's clock was 5 seconds behind my PC's after 12 straight hours of timekeeping.
I've repeated the same test several times, and the result has always been the same: about 5 to 7 seconds are lost every twelve hours in my circuit's timekeeping count, even though I'm using one of the best crystals available out there. This sort of error is a monstrosity that seriously jeopardizes my project, and it's imperative that it gets fixed.
My question to the members of this forum is, how can I make sure that the crystal oscillator is producing a nice and stable square wave and is not wandering around?
@MrChips mentioned that layout is critical. Well, here's a copy of my circuit's layout:
Dimensions are in inches, for reference. The crystal is shown outlined in red (upper left). The crystal's pins are soldered to the two orifices immediately below it. The 220k resistor is 0805 size, and the two tiny 22pF caps are 0402
I based my decision to use 22pF caps and a 220K resistor on the recommendations stated in Microchip's TB097 application note. But I suspect that that's only a ballpark sort of thing and one has to go into the details to land a definite design.
This is the application note's sample circuit that I copied into mine:
Following common-sense recommendations, I placed all of the elements as close as possible, but I must confess on two things:
Any thoughts or suggestions on this matter?
Here's my conundrum:
I've been working on a project that requires fairly precise timekeeping because the MCU (a PIC16LF1823) has the task of logging the moment an event (the event in question being when one of its input pins goes low) takes place.
For this purpose, I acquired several 32.7680KHz crystals (CFS-20632768HZFB) that boast an accuracy of ±5ppm. This should translate into an error of about ±7 seconds per year, if I'm not mistaken. So what I did is install said crystal in the MCU's circuitry so as to make it work as an asynchronous timing source for Timer1. And then I enabled Timer1's interrupt in such a way so as to never ever miss a single tick (one second) to be counted in its logic. I've gone through the code many times and so far I haven't found a fault within it.
Things worked rather nice at first, and everything seemed ok when I assembled the PCB and finished the code. I then tested it by leaving it counting seconds overnight (I had previously synched it with my PC's clock via a RS232-UART interface). You can imagine my disappointment when I checked how things were going the next morning and confirmed that my circuit's clock was 5 seconds behind my PC's after 12 straight hours of timekeeping.
I've repeated the same test several times, and the result has always been the same: about 5 to 7 seconds are lost every twelve hours in my circuit's timekeeping count, even though I'm using one of the best crystals available out there. This sort of error is a monstrosity that seriously jeopardizes my project, and it's imperative that it gets fixed.
My question to the members of this forum is, how can I make sure that the crystal oscillator is producing a nice and stable square wave and is not wandering around?
@MrChips mentioned that layout is critical. Well, here's a copy of my circuit's layout:
Dimensions are in inches, for reference. The crystal is shown outlined in red (upper left). The crystal's pins are soldered to the two orifices immediately below it. The 220k resistor is 0805 size, and the two tiny 22pF caps are 0402
I based my decision to use 22pF caps and a 220K resistor on the recommendations stated in Microchip's TB097 application note. But I suspect that that's only a ballpark sort of thing and one has to go into the details to land a definite design.
This is the application note's sample circuit that I copied into mine:
Following common-sense recommendations, I placed all of the elements as close as possible, but I must confess on two things:
- My PCB is single-sided and neither the crystal nor the caps are surrounded by a ground plane
- I glued the crystal to the PCB using LocTite 495, which is a rigid type sort of adhesive. The crystal's datasheet states that soft glue should be used. I don't know if that would affect things as much so as to produce such a notorious effect on my circuit. My goal at this moment is to give as much info as possible to see if someone with more experience than me can help me wit this.
Any thoughts or suggestions on this matter?
Last edited: