Strange oscillator behavior, 18f2455

Author
JFDuval
Starting Member
  • Total Posts : 45
  • Reward points : 0
  • Joined: 2004/12/26 11:00:59
  • Location: Québec/San Francisco
  • Status: offline
2008/07/29 14:09:14 (permalink)
0

Strange oscillator behavior, 18f2455

Hello,

I'm using a 18F2455 with a 4MHz crystal.  My configuration is the following:

#pragma config PLLDIV = 1, CPUDIV = OSC1_PLL2, USBDIV = 2
#pragma config FOSC = HSPLL_HS, FCMEN = OFF, IESO = OFF
#pragma config VREGEN = ON, PWRT = OFF, BOR = ON, BORV = 2
#pragma config WDT = OFF, WDTPS = 32768
#pragma config CCP2MX = ON, PBADEN = OFF, LPT1OSC = OFF, MCLRE = ON
#pragma config STVREN = ON, LVP = OFF, XINST = OFF


I'm expecting a processor frequency of 48MHz (am I right?).  If I enter 48MHz in MPSIM debugger and I simulate this function with '1' as a parameter:


//Delay 1s*r
void delay_s(int r)
{
    int q;
   
    for(q = 0; q < r; ++q)
    {
        Delay10KTCYx(0);
        Delay10KTCYx(0);
        Delay10KTCYx(0);
        Delay10KTCYx(0);
        Delay10KTCYx(176);
    }
}


I get 1.00006s and that's good.  However, on the real hardware, I measure 3.56s!  I verified the crystal, it's oscillating at 4.00016MHz.

Can bad suited crystal capacitors cause this?  I would think it would simply affect the 4MHz oscillation and not the PLL, but I can't find any logical answer for this 3.56s.

Thanks in advance,

JFDuval
jfduval at aqra dot ca
http://jfduval.ca/
#1

19 Replies Related Threads

    breyno2
    Super Member
    • Total Posts : 535
    • Reward points : 0
    • Joined: 2005/05/20 10:36:36
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/29 17:21:03 (permalink)
    0
    Have you tried FOSC = XTPLL_XT?
     
    FOSC = HSPLL_HS may be over-driving your 4MHz osc.

    Regards,

    Bruce
    http://www.rentron.com
    #2
    JFDuval
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2004/12/26 11:00:59
    • Location: Québec/San Francisco
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/29 17:42:05 (permalink)
    0
    ORIGINAL: breyno2

    Have you tried FOSC = XTPLL_XT?

    FOSC = HSPLL_HS may be over-driving your 4MHz osc.


    Yes, with the exact same result...

    JFDuval
    jfduval at aqra dot ca
    http://jfduval.ca/
    #3
    tunelabguy
    Super Member
    • Total Posts : 1639
    • Reward points : 0
    • Joined: 2005/04/03 08:30:19
    • Location: Hopkins, MN USA
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/29 18:03:08 (permalink)
    0
    How did you measure the 3.56 seconds? Did you set an output just before calling delay_s and then clear it immediately upon return? It might be useful to perform a more primitive experiment, such as:

    while(1)
    {
    set some output high;
    set that output low;
    }

    Then measure the pulse rate of the toggled output on a scope. Also look at the assembly language listing for the that code and count instruction cycles to calculate what the pulse rate ought to be if the clock rate were 48 MHz. If the setting and clearing of the outputs is done with a call to a library routine (Ugh!) then change the code to write to the port register directly, or use in-line assembly code so that you can get a loop that you can analyze. This removes any doubt about what Delay10KTCYx might be doing inside and outside of the simulator. It should be very easy to verify a 3:1 difference in pulse rates.



    Robert Scott
    (also on picforum.ric323.com, which is another forum for PIC users)
     
    #4
    JFDuval
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2004/12/26 11:00:59
    • Location: Québec/San Francisco
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/29 18:23:58 (permalink)
    0
    Hello,

    I'm using this code to test the delay:

        while(1)
        {
            AUTODIALER1 = 0;
            delay_s(1);
            AUTODIALER1 = 1;
            delay_s(1);
        }


    with:


    #define AUTODIALER1 PORTCbits.RC6
    #define TRIS_AUTODIALER1 TRISCbits.TRISC6

    TRIS_AUTODIALER1 = 0;


    With a DSO, I measured exactly 3.56s, and by counting the number of instructions (by the number of 1 cycle delays, I found some weird operating frequency, something like 13.xxMHz of Fosc...

    It's not only this delay that is causing a problem.  Initially I was on a 18F452 and all went well: my code was calibrated and all the menus and displays were well timed.  Now it's really slow (I mean, I have the same lag with the timers: it's not only a delay function problem, but an oscillator one).

    JFDuval
    jfduval at aqra dot ca
    http://jfduval.ca/
    #5
    breyno2
    Super Member
    • Total Posts : 535
    • Reward points : 0
    • Joined: 2005/05/20 10:36:36
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/29 19:01:30 (permalink)
    0
    Can bad suited crystal capacitors cause this?

    Yes. That's what I would check next. Try setting it to XT without PLL enabled, and toggle an output pin while watching it with your DSO. That should tell you if you have an osc or cap problem.
     
    Break it down as simple as possible to verify your oscillator & caps.

    Regards,

    Bruce
    http://www.rentron.com
    #6
    JFDuval
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2004/12/26 11:00:59
    • Location: Québec/San Francisco
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 06:40:23 (permalink)
    0
    Hello,

    This morning I verified my capacitors.  The crystal I'm using needs 20pF ones, so I stacked two 10pF.  If I set CONFIG for "HS: USB-HS", "HS: HS+PLL, USB-HS", "XT: USB-XTS" or "XT: XT+PLL, USB-XT" my crystal always oscillate correctly at 4.0000x MHz. 

    4MHz + PLL = 48MHz = 12MIPs = 83.333ns per instruction.  Waiting 12e6 cycles, I should get 1s, right?  Here is what I measured:


    HS-PLL || XT_PLL : 4MHz => 48MHz (12MIPs)
    Temps attendu : 10ms  Temps obtenu : 35.4ms (3.54x)
    Temps attendu : 1s  Temps obtenu : 3.54s

    XT || HS : 4MHz => 4MHz (1MIPs)
    Temps attendu : 120ms  Temps obtenu : 420ms (3.5x)
    Temps attendu : 1s  Temps obtenu : 3.54s


    Any clue?





    JFDuval
    jfduval at aqra dot ca
    http://jfduval.ca/
    #7
    JFDuval
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2004/12/26 11:00:59
    • Location: Québec/San Francisco
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 06:48:26 (permalink)
    0
    Additional information.  Here is the config settings I'm using:

    [img=http://img519.imageshack.us/img519/1584/configar5.th.png]

    JFDuval
    jfduval at aqra dot ca
    http://jfduval.ca/
    #8
    leon_heller
    Super Member
    • Total Posts : 6401
    • Reward points : 0
    • Joined: 2004/08/17 13:19:45
    • Location: St. Leonards-on-Sea, E. Sussex, UK.
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 06:55:20 (permalink)
    0
    [Edited]

    If your crystal is specified for a 20 pF load, your capacitors are wrong, as they are effectively in parallel. I doubt if that is the cause of the problem, but it is worth trying.

    I've got an 18F2455 board with a 4 MHz crystal here, I'll see if I can get one working at 48 MHz.

    Leon
    post edited by leon_heller - 2008/07/30 07:17:58

    Leon Heller
    G1HSM
    #9
    JFDuval
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2004/12/26 11:00:59
    • Location: Québec/San Francisco
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 07:04:01 (permalink)
    0
    ORIGINAL: leon_heller

    If your crystal is specified for a 20 pF load, your capacitors are wrong, as they are effectively in series. Try 33 pF, that will give you something closer to 20 pF as there will be some stray capacitance. I doubt if that is the cause of the problem, but it is worth trying.

    I've got an 18F2455 board with a 4 MHz crystal here, I'll see if I can get one working at 48 MHz.

    Leon


    Hello,

    I don't understand what you mean.  I have a FOXSDLF/040 crystal, 4MHz, Parallel, 20pf (http://www.foxonline.com/pdfs/hc49sdlf.pdf).  I'm using a 20pF cap between each leg and GND.  My 20pF is a stack of 2 10pF cap (they are put in //).

    JFDuval
    jfduval at aqra dot ca
    http://jfduval.ca/
    #10
    leon_heller
    Super Member
    • Total Posts : 6401
    • Reward points : 0
    • Joined: 2004/08/17 13:19:45
    • Location: St. Leonards-on-Sea, E. Sussex, UK.
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 07:11:08 (permalink)
    0
    If you look at the actual oscillator circuit you will see that the capacitors are actually in parallel (I said series previously). Your two 20 pF capacitors are giving an effective capacitance of over 40 pF, including strays.

    Leon
    post edited by leon_heller - 2008/07/30 07:16:29

    Leon Heller
    G1HSM
    #11
    JFDuval
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2004/12/26 11:00:59
    • Location: Québec/San Francisco
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 07:17:20 (permalink)
    0
    Ok, you mean that each capacitor is in serie with the internal resistor and the other capacitor?

    When a manufacturer says 20pF, isn't it 20pF on each leg or is 20pF when you put the two capacitors in series (so I would need to use 40pF per leg)?

    JFDuval
    jfduval at aqra dot ca
    http://jfduval.ca/
    #12
    leon_heller
    Super Member
    • Total Posts : 6401
    • Reward points : 0
    • Joined: 2004/08/17 13:19:45
    • Location: St. Leonards-on-Sea, E. Sussex, UK.
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 07:19:09 (permalink)
    0
    Have a look at this:

    http://www.foxonline.com/techdata.htm

    It explains how to calculate the value.

    Leon


    Leon Heller
    G1HSM
    #13
    JFDuval
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2004/12/26 11:00:59
    • Location: Québec/San Francisco
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 07:29:57 (permalink)
    0
    First of all, thanks for you precious help Leon.  The FOX document is really interesting.

    CL = (CL1*CL2)/(CL1+CL2)+Cstray




    In the datasheet, for a 4MHz crystal CL = 20PF.  Assuming a 5pF stray capacitance, I'll use 30pF on each leg. 

    I test it now and I come back with the results.

    JFDuval
    jfduval at aqra dot ca
    http://jfduval.ca/
    #14
    JFDuval
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2004/12/26 11:00:59
    • Location: Québec/San Francisco
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 07:36:46 (permalink)
    0
    In practice it makes no difference, it's still 3.5x too slow...

    JFDuval
    jfduval at aqra dot ca
    http://jfduval.ca/
    #15
    leon_heller
    Super Member
    • Total Posts : 6401
    • Reward points : 0
    • Joined: 2004/08/17 13:19:45
    • Location: St. Leonards-on-Sea, E. Sussex, UK.
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 07:38:11 (permalink)
    0
    I didn't think it would. 8-(

    I'll dig out my board and try it.

    I just tried my board with an assembler test program that flashes an LED. There was a clear difference when using the PLL compared with the ordinary 4 MHz operation, the LED was flashing much faster with the PLL. I configured the oscillator from MPLAB using the configuration bits window.

    Leon
    post edited by leon_heller - 2008/07/30 07:58:55

    Leon Heller
    G1HSM
    #16
    JFDuval
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2004/12/26 11:00:59
    • Location: Québec/San Francisco
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 08:08:37 (permalink)
    0
    Hello,

    I get a big difference too, a 12x time factor.  However, my 1MIPs isn't 1MIPs (it's rather 0.28MIPs...) and my 12MIPs is 3.37MIPs.

    JFDuval
    jfduval at aqra dot ca
    http://jfduval.ca/
    #17
    ErnieM
    Super Member
    • Total Posts : 375
    • Reward points : 0
    • Joined: 2007/10/13 02:54:02
    • Location: 0
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 08:46:43 (permalink)
    0
    I had a weird configuration problem in the oscillator section when using a PIC18F4550 and an external oscillator.  Code was done on the C18 compiler inside MPLAB.
     
    What I discovered was certain config bits that defaulted one way so I did not write #pragma statement were being set incorrectly. When I checked the config bits inside MPLAB after compilation everything was OK.
     However, if I flashed a part, then read it back, and imported the hex file into MPLAB then and only then did I see a difference in config bits. It may be a long shot but worth a few minutes to try instead of just pulling hair out.
    #18
    JFDuval
    Starting Member
    • Total Posts : 45
    • Reward points : 0
    • Joined: 2004/12/26 11:00:59
    • Location: Québec/San Francisco
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/07/30 08:49:24 (permalink)
    0
    Ok, problem solved... it's rather mysterious however.

    In my first prototype, I needed 2 interrupts priority levels to deal with my encoder.  On the final board, I use only low-level INT but I left IPEN set.  I suppose that some peripheral was trying to enter my high-level INT function (that was commented out), causing a big time lag because now that I cleared IPEN, my timings are accurate.

    Thanks for your help everybody.  If someone can give me a "real" (ie better than my supposition) answer for this IPEN problem, I'll be glad.

    JFDuval
    jfduval at aqra dot ca
    http://jfduval.ca/
    #19
    DarioG
    Super Member
    • Total Posts : 45271
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Roppen, Austria
    • Status: offline
    RE: Strange oscillator behavior, 18f2455 2008/08/31 14:05:50 (permalink)
    0
    Your explanation definitely makes sense (and explains the difference from SIM to real hardware).
    USB could possibly be one source of those IRQs... just a guess given that PIC.

    Dario Greggio
    --
     
    #20
    Jump to:
    © 2014 APG vNext Commercial Version 4.5