0
\$\begingroup\$

I have designed a PCB based on the PIC18F45K22 microcontroller. I want to use an external crystal (4Mhz crystal with two 22pF capacitors). I can successfully program the code but the PIC won't run. When changing the oscillator mode in the config settings to use the internal clock, it works fine.

The code that I'm trying to test is a simple blink LED code.

note that when the microcontroller didn't run, i couldn't enter to debug mode.

I have tried various values of caps but that didn't help. I noticed that bad soldering actually added some stray capacitance between the OSC1/OSC2 pins and the ground (measured using an RLC meter) so I tried lowering the value of the capacitors to 15pF.

I have tried various crystal (replaced the 4MHz, then tried a 16MHz crystal) but that didn't help. I also replaced the microcontroller but nothing changed.

I have tried to make the exact same connection on a bread board and it worked normally using external crystal mode. I only poppulated MCLR, VSS, VDD, PGD, PGC and an LED with a series resistance.

I have redesigned the layout of the board, thinking its a layout problem with the crystal but that didn't help.

I have searched about similar problem but i can't find any helpful answer

part number of crystal: ATS040B-E datasheet of crystal: http://www.mouser.com/ds/2/96/008-0309-0-32667.pdf

I just managed to make this board work... The ground plane that connects the capacitors of the crystal is connected to GND at one point as recommended by many application note... but there is a capacitor 100nF between this plane and +5V for decoupling... this was a mistake by the layout designer. I removed this capacitor and it worked. There is another one between GND and +5V near the PIC's pins as it should be.

still I can't understand why did this solve the problem...

I did test earlier to check if there is any AC voltage between this plane and GND of the PIC... and the AC voltage between this plane and +5V of at the micocontroller. It was about 5mV so I guessed it was OK...

Now the question is how the hell did this 100nF cap messed up the oscillator circuit to prevent it from oscillating!?

\$\endgroup\$
  • 3
    \$\begingroup\$ You should post a schematic or at the very least the part number of your oscillator. \$\endgroup\$ – scld May 4 '15 at 13:21
  • \$\begingroup\$ Are you using XT or HS oscillator mode? \$\endgroup\$ – Olin Lathrop May 4 '15 at 17:27
  • \$\begingroup\$ I tried both modes HSMP, HSHP, and XT modes with no result \$\endgroup\$ – fhlb May 5 '15 at 8:06
  • \$\begingroup\$ I edited the post and added the datasheet for the crystal. \$\endgroup\$ – fhlb May 5 '15 at 8:33
  • \$\begingroup\$ If it works on a breadboard, with the same software and the same components, then it's probably not the first choice to suspect those as the likely source of the problem. \$\endgroup\$ – Roger Rowland May 5 '15 at 9:37
3
\$\begingroup\$

Have you consulted the data sheet? Usually when it does not do anything it is a config problem.

I have absolutely no knowledge on this but from PIC18(L)F2X/4XK22 Data Sheet.

Note 1: Quartz crystal characteristics vary according to type, package and manufacturer. The user should consult the manufacturer data sheets for specifications and recommended application.

2: Always verify oscillator performance over the VDD and temperature range that is expected for the application.

3: For oscillator design assistance, refer to the following Microchip Application Notes:

• AN826, “Crystal Oscillator Basics and Crystal Selection for rfPIC® and PIC® Devices” (DS00826)

• AN849, “Basic PIC® Oscillator Design” (DS00849)

• AN943, “Practical PIC® Oscillator Analysis and Design” (DS00943)

• AN949, “Making Your Oscillator Work (DS00949)

Looks like oscillator problems are common. So I'd start there. I know you have a prototype working, but if the right bit is not set no magic happens.

And:

FIGURE 2-6: QUARTZ CRYSTAL OPERATION (LP, XT OR HS MODE)

Note 1: A series resistor (RS) may be required for quartz crystals with low drive level.

2: The value of RF varies with the Oscillator mode selected (typically between 2MΩ to 10MΩ.

Or:

FIGURE 2-7: CERAMIC RESONATOR OPERATION (XT OR HS MODE)

Note 1: A series resistor (RS) may be required for ceramic resonators with low drive level.

2: The value of RF varies with the Oscillator mode selected (typically between 2 MΩ to 10 MΩ).

3: An additional parallel feedback resistor (RP) may be required for proper ceramic resonator operation.

Also from Making Your Oscillator Work

Question: I am trying to use a 4 MHz resonator with VDD at 3V. The data sheet allows this, but the oscillator does not work. What am I doing wrong?

Answer: You may be using the XT mode oscillator. When VDD is raised, the oscillator may start working. Select the HS mode oscillator instead. This will allow the oscillator to work at a lower voltage, but will also draw additional current. Alternatively, change the resonator to a crystal, use XT mode as before and check the loading capacitor selection. When making such a change, retest the oscillator circuit to ensure that it will perform as expected.

Not sure if this applies, but it did make me smile.

I'd also check my programming software to see if it actually is setting the config word the way you want, because I've run into system defaults on PIC's which were not what I selected. Oscillator selected but running very slowly because RC timing was the systems default. (OK I have some knowledge, just enough to make me dangerous - Good luck SSR).

\$\endgroup\$
  • \$\begingroup\$ Just double checked the config bits. Oscillator mode is correct. I also tried to read the config settings after programming the PIC. All programmed correctly. As I stated before, the circuit runs well on a breadboard with the same external crystal and caps as the ones on the pcb. \$\endgroup\$ – fhlb May 5 '15 at 8:39
  • \$\begingroup\$ They reference 4 App Notes in their data sheet. It's not a trivial problem, start at the app notes. Then you have to consult with Microchip App Engineers because it appears to be a problem not unique to you. The first thing they will say is check the app notes. As a designer, one of the first thing you check is the errata. The fact they have 4 published app notes raises flags for me because I don't think it is unreasonable to expect a 4MHz crystal to work. \$\endgroup\$ – StainlessSteelRat May 5 '15 at 13:23
  • \$\begingroup\$ Your absolutely right. I am working with these micro's for 3 years now... designed about a dozen boards with no such problem. It's frustrating to see something that is supposed to be so simple turning to be a real pain in the ... I spent over 3 days now trying to figure this out but I thought brain storming with you guys could bring up some forgotten ideas... I tried another layout today (minimized stray capacitance) and only populated the minimum components... same problem. I also added series and parallel resistors to the crystal... still the same... \$\endgroup\$ – fhlb May 5 '15 at 17:19
  • \$\begingroup\$ I also tried powering the board directly from a power supply and on different voltages... Same problem... it always fail to work on external crystal but works fine using the internal. \$\endgroup\$ – fhlb May 5 '15 at 17:20
  • \$\begingroup\$ I'd go with 15pF, since Xtal data sheet calls for 18pF. On page 3 of Making Your Oscillator Work, they talk about Open Oscillator Loop for starting using a function generator and scope. I'd concentrate my efforts there. \$\endgroup\$ – StainlessSteelRat May 7 '15 at 17:21
1
\$\begingroup\$

The value of the capacitors affects the final crystal frequency more than it affects the ability to oscillate. So an extra stray 1 pf shouldn't kill it. But maybe it's time to apply a little research. The crystal oscillator is just an amplifier with feedback. The OSC2 pin is the output--it should be sitting high or low (not in between) if it's not oscillating. The OSC1 pin is an input. It should be in the opposite state of the output (otherwise you have found something wrong). Being an input, you should be able to raise or lower it using a large value resistor connected to + or ground. Dragging it to the opposite voltage should flip the state of OSC2. Anything that doesn't work here suggests a mis-configuration, or a problem with the circuit.

Crystals can go bad, too, so it's good that you've been able to try more than one.

Also, many is the time it started oscillating while I was poking at the connections, so keep watching for that.

I've had configuration problems, and only believed it after I had tried all that other stuff. Sometimes you just have to check everything else to convince yourself.

\$\endgroup\$
  • \$\begingroup\$ verified that OSC2 pin is 5V, OSC1 pin is input... \$\endgroup\$ – fhlb May 6 '15 at 7:48
  • \$\begingroup\$ actually now that you mentioned it, I managed to get the crystal oscillating at some points... I tried to cut (with a knife) all the traces that are connected to the micro's pins except those related to supply/oscillator/programming and left a single blinking LED. When I did that, the crystal oscillated but not all the times... that is unplugging power then plugging power back on had a different behavior each time. I did make sure that the supply on the micro was zero before I plugged back. I actually shorted the VDD and VSS before plugging \$\endgroup\$ – fhlb May 6 '15 at 7:59
0
\$\begingroup\$

It actually worked after further improvements on the layout... I can't say I have actually pinpointed where the problem was. I then edited the 2 boards that were not working... connected traces by wires and removed the PCB traces (traces for crystal,caps,and their GND).. they worked fine.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.