# External oscillator problem

I have this sample program to blink LED with PIC18F4550 using external oscillator.

I'm using a 20.000 MHz crystal oscillator with 30pF capacitors.

The problem is when I wire the capacitors the the ground the LED doesn't blink (still off), but when I disconnect them from ground (not wired) the LED starts blinking. Blink speed change with _XTAL_FREQ.

Program works well with internal oscillator (just by setting FOSC = INTOSCIO_EC)

Is there direction wiring of capacitor or oscillator?

Ground here is PIC Vss

This is when using PICKIT2 directly. When using the circuit alone like the scheme, nothing happens with FOSC = INTOSCIO_EC configuration (not working), but works well with internal oscillator.

#include<p18f4550.h>
#include "delays.h"

/* COMPILER DIRECTIVES FOR CHIP CONFIGURATION BITS*/

#pragma config FOSC = HS //INTOSCIO_EC    /* Sets the oscillator mode to HS */
#pragma config WDT = OFF   /* Turns the watchdog timer off */
#pragma config LVP = OFF   /* Turns low voltage programming off */
#pragma config DEBUG = OFF /* Compiles without extra debug code */

#define _XTAL_FREQ   12000000  //delay_ms change by changing this

void main(void) {
TRISD = 0xF0; // PORT B Setting: Set all the pins in port B to Output.
while (1) {

LATDbits.LATD0 = 1; // RD-0 to High
LATDbits.LATD1 = 1; // RD-1 to High

__delay_ms(50); // Half sec delay

LATDbits.LATD0 = 0; // RD-0 to LOW
LATDbits.LATD1 = 0; // RD-1 to LOW

__delay_ms(50); // Half sec delay
}
}
/* THE END */


• 16 or 20 MHz crystal? 30pF or 22pF? There seems to be a contradiction. Also a layout view would help. Fairly certain it's a layout problem grounding the caps. – Andy aka Aug 22 '14 at 21:36
• @Andyaka yes it 20MH I edited it sorry, what kind of layout problem ,I dont understand, I the caps wired to PIC ground (vss) – mikmik Aug 22 '14 at 21:40
• Circuit board? Bread-board? – Andy aka Aug 22 '14 at 21:47
• it's breadboard – mikmik Aug 22 '14 at 21:47
• Even if Andy's answer doesn't ultimately solve your problem, it's definitely part of the problem. It's very clear from your photo that the xtal is way too far from the chip. – Dan Laks Aug 22 '14 at 22:07

OK, the picture tells the big story - it's a layout problem - the xtal needs to be mounted right up at the pic pins and the two capacitors need to connect to 0V via the shortest possible route. At the moment, the wires to the xtal and caps has too much inductance and not surprising this will not work correctly.

• it' not working fine fine, but at least it blink in some way, thanks – mikmik Aug 22 '14 at 22:16
• just for your answer, without capacitor is works well, but with capacitor not working?? I mean I just put the xtal on pins 13,14 and it start to work, but when adding capacitor and wiring them to ground, the led stop working – mikmik Aug 22 '14 at 22:28
• That is probably due to parasitic capacitive coupling. If it does not work with capacitors attached, try lower value like 22, 18 or 15 pF. Sometimes 1Mohm across the crystal helps to start oscillation. – venny Aug 22 '14 at 22:33
• Are you sure the capacitors are the correct value and not 22nF instead of 22pF? – Andy aka Aug 23 '14 at 9:34

Just in case Andy's answer doesn't solve the problem, here's something else I see. You've got _XTAL_FREQ set to 12MHz with a 20MHz crystal.

Plus, your delay functions are delaying for 50ms, not 500ms like your comment indicates. With the mismatched _XTAL_FREQ value and the mis-typed delay time, the actual delay will be (12/20)*50ms = 30ms.

That's 33Hz. The human eye may not be able to tell it's blinking at that rate.

Try these configuration bits:

#pragma config PLLDIV   = 5         /* PICs 96 MHz PLL prescaler divided
by 5 for 20MHz input */
#pragma config CPUDIV   = OSC1_PLL2 /* System clock post scaler
[OSC1/OSC2 Src: /1][96MHz PLL Src: /2] */
#pragma config FOSC     = HSPLL_HS  /* HS oscillator, PLL enabled */


You will end up having clock frequency of 96/2MHZ = 48MHz which will produce instruction frequency of 12MHz which is the maximum for PIC18F4550.

Also PIC18F4550 datasheet http://ww1.microchip.com/downloads/en/devicedoc/39632c.pdf on page 27 recommends using 15pF capacitors for 20MHz crystal.

Looking at your picture I'm pretty sure you have a small typo in there. 2 of oscillators wires are connected wrong way

after ensuring that your hardware connections are fine as pointed out, the other error must have been your capacitors value. I had exactly the same error where the MCU would blink the LED without the capacitors but turned off immediately after plugging the capacitors. The capacitors value I used were "223pF" instead of "22pF". Ceramic capacitors value are always in picofarads and therefore you have to do the correct calculations. "223 = 22000pf" while "0.022nF = 0.022x1000pF = 22pF." hope it helped you.