Trying to run a PIC16F883 with a 20MHz external oscillator. - Page 1
Forum section: electronics / beginners

author: cvriv
author_rank: Frequent Contributor
timestamp: July 27, 2020, 12:16:33 am
content: Hello everyone.So i've been reading through the microchip application notes AN826 and AN849 because i'm trying to understand how to properly setup an external oscillator for a PIC16F883 so I can run it at 20MHz. I have the application notes AN943 and AN949 as well, but after having read the first two I mentioned.... there's is no point reading the last two because everything is jumbled in my head already.For the most part I understand everything that's being talked about in these application notes because much of this I learned at the college I attend. I am an EE major, working on my AS currently.Now, I have the datasheet for the 883 and im pretty familiar with it because this is the MCU my professor had us using in our classes. The datasheet says the 883 can operate at 20MHz with the appropriately clock mode selected. I programmed the MCU configuration bits as so:Code:__CONFIG_CONFIG1, _FOSC_HS & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOR_OFF & _IESO_ON & _FCMEN_ON & _LVP_OFF & _DEBUG_OFFNow, I am using a 20MHz crystal I got a long time ago. It came in an assortment of crystals I bought. I dont have the data sheet for this crystal and theres no information on it other than the frequency.I am really confused on what components I should be using in my external oscillator circuit. I know that I need capacitors C1 and C2. From what I've read, Rs is only needed if im over-driving the oscillator output. I also read somewhere that RFext is only needed if the there's an issue with starting the oscillations.Now what about the load capacitance? This is really confusing me. I think I understand but im not sure. I only add a load capacitance if I want to increase the frequency from the series resonance to something higher? I read that a load capacitance with a series resonance crystal is implied to be zero. I read that in AN826. AN849 has me thinking I need to add a load capacitance though. These application notes are confusing as hell. They are written by different people.So... from the typical values mentioned throughout the application notes, I guessed values for C1 and C2 and wire them up with the crystal and it worked!?!?!This is what I found on my Fisher Price scope:It's frequency is spot on and the voltage is about 3.9Vpp. It's not clipping, looks pretty sinusoidal. Is this good? I don't think the crystal is being over-driven. Its been oscillating this whole time i've bee writing this. When I power the MCU down and back up again it starts right away. It can't be this easy.How can I test this? The application notes went into craziness talking about testing with temperature changes and low voltages ect. What can i do or what should I do now? It can't be this easy.

author: cvriv
author_rank: Frequent Contributor
timestamp: July 27, 2020, 02:39:33 am
content: Well, I wrote some code to simply toggle PORTC and this is what I found on my Fisher Price:It's spot on. The toggle requires 3 instruction cycles, with each cycle requiring 200ns of time. It just seems like it was too easy.How do I know that my oscillator is function appropriately? How I test it? How do I prove that it's working correctly? I know engineers aren't just throwing stuff together with a best guess and having things just work.Thanks.

author: Ian.M
author_rank: Super Contributor
timestamp: July 27, 2020, 08:14:06 am
content: AT cut quartz crystals intended for fundamental frequency parallel resonance, used in a well designed Pierce oscillator circuit aren't that fussy. You can usually deviate from the ideal load capacitance by at least a factor of two, with the only significant side effect of 'pulling' the frequency away from what it would be with the correct load capacitance. In fact in critical applications, it used to be usual to use a trimmer as all or part of the load capacitance on one side of the crystal so the resonant frequency could be adjusted to bring it closer to its nominal frequency.Also, most PICs have very well designed remarkably robust oscillator modules that aren't at all fussy about your choice of crystal. Problems only usually start when you try to use low power low frequency 'tuning fork' crystals, poorly chosen ceramic resonators, or need to operate at minimum voltage from a battery supply (which wont have significant noise to 'kickstart' oscillation), or at extreme temperatures.However, you've definitely got lucky as your layout *SUCKS*. Microchip don't provide extra Vss pins (and Vdd pins on larger packages) for fun, they provide them because the PIC needs low impedance ground (and supply) connections. Its mandatory for PICs with multiple Vss pins to wire them all in parallel. Failure to do so can result in various malfunctions or even damage if your application has multiple port pins sinking a lot of current. The same goes for multiple Vdd pins if present.Microchip quite deliberately put a Vss pin right next to OSC1/CLKIN and only one pin further from OSC2/CLKOUT. Its there so you can connect the crystal load capacitors to it and the OSC pins with minimal loop area thus minimising the risk of disturbance to the oscillator due to EMI or of upsetting its biassing due to surface leakage. You should be able to put the OSC1 load capacitor directly in the breadboard contact strip next to the PIC, diagonally, with one pin in the hole adjacent to the OSC1 pin, and the other in whatever hole it fits in the strip with the Vss pin. The OSC2 load capacitor will go between the OSC2 and Vss strips at the furthest holes from the PIC, bent out slightly so it doesn't obscure the furthest hole in the OSC1 strip. The crystal then goes at an angle from that OSC1 hole to whichever hole is the best fit in the OSC2 strip. If its got very short pins, you may need to extend them. Finally the Vss pin must be connected to ground. Run a cut to length jumper over the top of the chip, from the hole nearest the Vss pin by the OSC pin to the hole nearest the Vss pin by the Vdd pin. If you've got a ground busbar on the same side of the PIC as the crystal, also run a jumper to the busbar. I'd also recommend a 100nF ceramic decoupling capacitor across the Vdd and Vss pins as close to the chip as possible (and for larger PICs with multiple Vdd, Vss pin pairs, one 100nF capacitor per pin pair).Probing Pierce oscillators with a scope is generally problematic as even a x10 probe presents about 15pF extra capacitance at its tip, which is comparable to the typical load capacitance for the crystal and will cause a significant frequency shift. It isn't unusual to find that connecting a scope probe either 'kills' the oscillator or makes a non-functional oscillator start working. The input pin is far more sensitive to loading than the output pin, and its asking for trouble probing there. One trick is to probe via a series 'gimmick' capacitor trimmed to 1/9 the probe tip capacitance as measured on a good capmeter. With a x10 probe, that gives you x100 attenuation, AC coupled only, and reduces the additional load on the oscillator to a couple of pF.

author: cvriv
author_rank: Frequent Contributor
timestamp: July 28, 2020, 01:41:24 am
content: Quote from: Ian.M on July 27, 2020, 08:14:06 amAmazing information right there. Thank you so much.AT cut quartz crystals intended for fundamental frequency parallel resonance, used in a well designed Pierce oscillator circuit aren't that fussy. You can usually deviate from the ideal load capacitance by at least a factor of two, with the only significant side effect of 'pulling' the frequency away from what it would be with the correct load capacitance. In fact in critical applications, it used to be usual to use a trimmer as all or part of the load capacitance on one side of the crystal so the resonant frequency could be adjusted to bring it closer to its nominal frequency.I don't have any trimmer caps. I guess I need to get some. The application notes talked about them.Run a cut to length jumper over the top of the chip, from the hole nearest the Vss pin by the OSC pin to the hole nearest the Vss pin by the Vdd pin.This is hilarious because I use to do this when I first started working with MCU's in class. I did it because I thought I had to do it, not for the reasons you mentioned. I stopped doing it because a buddy of mine said those VSS pins were tied together inside. Ill do exactly what you mentioned.So what you basically describing to me with the movement of the external oscillator is that I need to get it as close to the MCU as possible to cut down on stray capacitance and induced noise?I'd also recommend a 100nF ceramic decoupling capacitor across the Vdd and Vss pins as close to the chip as possible (and for larger PICs with multiple Vdd, Vss pin pairs, one 100nF capacitor per pin pair).This is to send noise from the power supply right to ground so it doesn't enter the MCU? Decoupling noise?One trick is to probe via a series 'gimmick' capacitor trimmed to 1/9 the probe tip capacitance as measured on a good capmeter. With a x10 probe, that gives you x100 attenuation, AC coupled only, and reduces the additional load on the oscillator to a couple of pF.Awesome. I didnt know that I could do that. Never done it before so I'll have to mess around with that.Thank you so much!

author: Ian.M
author_rank: Super Contributor
timestamp: July 28, 2020, 02:44:21 am
content: That looks a lot better. Does it run reliably?The purpose of a decoupling capacitor for MCUs and other digital logic isn't to remove noise from the PSU, its to provide a local source for the high current transient spikes the chip draws as its internal logic circuits switch. Without decoupling, the transients can cause the supply and ground rails to 'bounce' disturbing input thresholds and glitching output levels.Also seehttps://en.wikipedia.org/wiki/Gimmick_capacitorNot as convenient as a trimmer, but when you only need a few pF of 'set once' adjustment range, its a handy trick to know.N.B. there's no point in attempting to trim a crystal oscillator unless you've got a good frequency counter with a highly accurate reference, e.g. a GPSDO.

author: cvriv
author_rank: Frequent Contributor
timestamp: July 28, 2020, 04:03:40 am
content: It's running great. Starts right up and it's been running since my last post. I accidentally left the old layout running over night, last night, and it was running in the morning. This is awesome.I understand what you mean about the decoupling now. I just wish I understood all that more greatly. I wonder at what point, with all my schooling or apprenticeship, will I start to really understand all this. I feel a bit overwhelmed with how much I don't know and understand.I'll check out the gimmick capacitor wiki. Thanks.

author: cvriv
author_rank: Frequent Contributor
timestamp: July 28, 2020, 10:37:06 pm
content: So I found that I can start and run a 30Mhz crystal with the same capacitors (C1 = C2 = 20pF) and it's pretty stable. It has and nice sinusoidal waveform with a voltage of about 3.1vpp, which is just about the same as the 20Mhz crystal.Now, when I tried a 40Mhz crystal with two 5pF capacitors, I found that it starts up nicely and runs with a nice sinusoidal waveform at about 3.1vpp, but it's only operating at a 1/3 of it's operating frequency. I saw many times on the application notes about pulling the crystal up to higher harmonics, but i didnt see anything about crystals running at lower harmonics?What would cause this to happen? Is it because there's not enough gain? I tried with a 32MHz crystal with different caps and it had a gain near 5vpp, but it was still operating at about a third.

author: Ian.M
author_rank: Super Contributor
timestamp: July 29, 2020, 07:17:45 am
content: The higher the frequency, the thinner the slab of AT cut Quartz must be to resonate at the correct frequency, and the more fragile it will be, so fewer of the blanks make it through the production line in usable condition. Somewhere up around 50MHz, it becomes commercially impractical to produce fundamental frequency crystals, so overtone crystals become the only option.Your 40Mhz crystal must be a third overtone one. Without a circuit to suppress its fundamental frequency, it prefers to oscillate atapproximately1/3 of its nominal (third overtone) frequency.N.B. although older 8 bit PICs seem to be remarkably tolerant of over-clocking, severe over-clocking is not advisable. You've got no idea of what race conditions may exist in the core logic and therefore how instruction execution may 'glitch' Its easy to get into a situation where you get apparently random failure to correctly execute an instruction or even flow of control failures, where execution fails to move through the program in the expected sequence. There's also a small risk of permanent damage, increasing at higher temperatures and higher Vdd. For a professional design, operating outside datasheet limits without the cooperation of the manufacturer's application engineering support team to qualify the part for that usage (only available if you are a *BIG* customer) is like playing russian roulette. For a one-off hobby project it may be the only viable option e.g. to generate accurate hi-res video signal timings programmatically, but you'd be well advised to keep the overclock below 50%, and test it over an extended voltage and temperature range so you can be reasonably sure its not going to be right on the edge of its stable zone in normal operation.If you want to continue to experiment with fast crystal oscillators, a PIC isn't a good platform for it. Get yourself an unbuffered CMOS inverter chip e.g. 74HCU04, and build your own Pierce oscillator!