1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Inaccurate timing in PIC18F4455

Discussion in 'Microcontrollers' started by Vizier87, Aug 7, 2017.

  1. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia
    Hi guys,

    I'm revisiting my old project for a bigger job now. I'm configuring the timing for my PIC18F4455 with a 1000-ms delay for a blinking LED using MikroC. I'm using a 20-MHz crystal oscillator.

    Now, I understand that you have to set the speed from the configuration settings. Here's mine:

    Untitled.png

    However, the blinking period doesn't match the computer clock (I watch the sync between the clock ticking and the LED blink) which I'm using as a reference. It seems the PIC is going a tad bit faster but definitely noticeable. This has made my subsequent programming (which involves UART and other comm protocols) a problem.

    I've tried some combinations like changing the Prescaler, CPU postscaler, and whatnot but this hasn't helped. I've definitely understood this whole clocking part poorly.

    Anybody can point me in the right direction?

    Thanks.
    Vizier87

    EDIT: I've set the oscillator as 19 MHz as part of my attempts to see if this makes it more accurate. It seems to have changed but still a very messy thing to do. Hasn't worked also though.
     
    Last edited: Aug 7, 2017
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,160
    Likes:
    340
    Location:
    Brisbane Australia
    ONLINE
    Shouldn't your oscillator frequency be 20MHz not 19?

    Edit, whoops just saw your edit.

    Mike.
     
  3. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia
    LOL.

    Any thoughts on how I should approach it? I've been changing the values between 20-25 MHz. The closest accuracy I could achieve is 23. But still not good enough.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK

    I don't use MikroC for 8 bit only 32bit! But when I use an 8Mhz crystal and PLL to 80Mhz, the setting is for the latter.

    I wouldn't think you would see much difference 20 ~ 25Mhz on an led A 1 second pulse at 20Mhz will be .8mS at 25Mhz , agreed! With USART there will be a great difference..

    Looking at the CPU clock... (osc1 / osc2 ) / 3 = 6.66Mhz ??? Is there another option here?
     
  6. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia

    Hi Ian, here are the options:
    Untitled.png

    The highlighted one gives a much faster output and the top one gives a slower blinking rate.. I'd say by 1.5 times.

    Help! :(
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    RightyO!!

    Quick understanding of said datasheet and we're off!!

    20Meg crystal.....96Mhz PLL src: /1 = Micro = 20Mhz
    96Mhz PLL src: /2 = Micro = 10Mhz
    96Mhz PLL src: /3 = Micro = 6.6Mhz
    96Mhz PLL src: /4 = Micro = 5Mhz​
    With HSPLL.........96Mhz PLL src: /2 = Micro = 48Mhz
    96Mhz PLL src: /3 = Micro = 32Mhz
    96Mhz PLL src: /4 = Micro = 24Mhz <- your Osc speed!!
    96Mhz PLL src: /6 = Micro = 16Mhz​

    So even though you have a 20Meg crystal your OSC block is running at 24Mhz...

    The system crystal / 5 gives a 4Mhz starting block... This is jumped up to 96Mhz... then the cpu OSC is divided down

    so OSC1/OSC2 src: /3 [96Mhz / 4] = 24Mhz is the correct setting...

    You need.. CPU System Clock Prescaler needs to be /4, and the OSC Frequency needs to be 24Mhz.
     
    • Thanks Thanks x 1
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    Just for clarification... (osc1 / osc2 ) / 3.. means both bits (0x11) not a divisor!!

    MikroE making it difficult!!
     
    • Thanks Thanks x 1
  9. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia

    Looks like I owe you some Malaysian hospitality. ;)

    I followed your instructions:
    Untitled.png


    The whole thing is a poor man's work. Just trying to earn something from here if what follows would work. No wonder my previous transmission with an Xbee was totally in disarray!

    Thanks a million, Ian. I'm gonna put your name in my code.

    Vizier87
     

Share This Page