I have problems with the crystal startup on the DCO. For some reason the crystal doesn't start up properly, and the Fail-Safe Clock Monitor kicks in and starts the internal oscillator, which runs at 16MHz (The external crystal runs at 32MHz). Switching over to the external crystal again after startup works fine.
Someone on the mikroe forum suggested that this was due to a too high stray capacitance on the breadboard. Today I read up on crystals and capacitors, and tested a few combinations to see if I could get it to boot up at the right speed.
First of all, in the datasheet of the crystals, the parameter C load tells what capacitance to use. For my crystals this is 18pF.
I always thought this meant that each of the two caps should be 18pF. This is definitely incorrect. Instead, the total capacitance should be 18pF. But what does this mean?
First of all, the two caps connected from the crystal to ground are in reality two caps in series between the two legs of the crystal. Two equal caps in series have a equivalent capacitance of half the capacitance of one of them. So running two 18pF caps in parallel would yield an equivalent capacitance of 9pF.
But in addition to this, the crystal sees an additional capacitance CS. CS is the stray capacitance of the circuit and the input/output capacitance of the inverter or microprocessor chip at the Crystal 1 (C1) and Crystal 2 (C2) pins, plus any parasitic capacitances (here). This is normally around 5pF but may be higher.
The total capacitance C load should then be CS + C1 * C2 / (C1 * C2), which means that C1 * C2 / (C1 * C2) = C load - CS.
C1 * C2 / (C1 * C2), as mentioned, will be half of C1 if C1 = C2.
For my crystal then, C1/2 = 18pF - 5pF = 13pF, which means C1 should be 26pF (or 27pF, which is a standard value).
So, would changing from 18pF to 27pF on the breadboard change anything?
I tried the following combinations with the following results (half frequency means the MCU fell back to the internal oscillator):
18pF: half frequency
22pF: half frequency
33pF: half frequency
36pF (two 18pF in parallel for each side): half frequency
9pF (two 18pF in series for each side): CORRECT frequency!
This is strange and interesting. At 9pF the serial equivalent is 4.5pF (leaving "room" for CS up to 12.5pF). At 18pF the serial equivalent is 9pF (with CS up to 9pF). If my reasoning is correct, the stray capacitance is more than 9pF. It does not sound implausible. I will have to retest this once I get a PCB made, in theory at least 27pF caps should be the ones to use.