• 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.

Inaccurate timing in PIC18F4455

Status
Not open for further replies.

Vizier87

Active Member
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:

Pommie

Well-Known Member
Most Helpful Member
Shouldn't your oscillator frequency be 20MHz not 19?

Edit, whoops just saw your edit.

Mike.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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?
 

Vizier87

Active Member
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?

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! :(
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Looking at the CPU clock... (osc1 / osc2 ) / 3 = 6.66Mhz ??? Is there another option here?
Just for clarification... (osc1 / osc2 ) / 3.. means both bits (0x11) not a divisor!!

MikroE making it difficult!!
 

Vizier87

Active Member
Just for clarification... (osc1 / osc2 ) / 3.. means both bits (0x11) not a divisor!!

MikroE making it difficult!!

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
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top