Continue to Site

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.

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

How to calculate the timing for each instruction using PIC MCU

Status
Not open for further replies.

skyflyer54

New Member
Hi,

I'm currently using PIC MCU clock with 4MHz Crystal Oscillator and I have set the frequency to 32MHz using PLL.

Can someone guide me on how to calculate the timing for executing 1 instruction. Should I use the 4MHz or 32MHz to calculate for the time of each instruction.

Thanks.
 
Assuming that your using a third of 96MHz to obtain 32MHz then each instruction cycle is 1/(32,000,000/4) = 125nS. Be aware that some instructions can be 2 or 3 cycles long.

Mike.
 
Assuming that your using a third of 96MHz to obtain 32MHz then each instruction cycle is 1/(32,000,000/4) = 125nS. Be aware that some instructions can be 2 or 3 cycles long.

Mike.

Thanks Mike..

That is to mean I have to use the 32MHz to calculate each instruction timing and NOT the external 4MHz oscillator right?

Do you have any reference website to recommend for which instruction take how many cycles long? :)
 
Thanks Mike..

That is to mean I have to use the 32MHz to calculate each instruction timing and NOT the external 4MHz oscillator right?

Do you have any reference website to recommend for which instruction take how many cycles long? :)

hi,
This may help till Mike comes back online.:)
Look at the Program Cycles in the datasheet.
 

Attachments

  • esp01 Jul. 24.gif
    esp01 Jul. 24.gif
    46.3 KB · Views: 1,160
Sorry Sky,
I couldn't see why you was referring to a 4Mhz xtal. Mike's formula is right. (unless the instruction requires more than 1 cycle to complete as Mike said) ie base it on 32Mhz/4

Regards
Jim
 
Another way to see how many cycles are used by your code is to use the "Simulator trace" in the view options of MPLAB.
You can then step through some code and see the cumulative number of cycles used.
If you right click in the trace window, you can then change the option to display the time in hex,decimal cycle count or seconds elapsed etc.
 
Sorry Sky,
I couldn't see why you was referring to a 4Mhz xtal. Mike's formula is right. (unless the instruction requires more than 1 cycle to complete as Mike said) ie base it on 32Mhz/4

Regards
Jim

Hi Jimmy,

I know Mikes formula was correct but what I mean was, I used a 4MHz xtal to clock my microcontroller, but in the software part, I set the internal osc to 8MHz than followed by using the PLL to make it to 32MHz.

Also, I understand that some instruction may take more than 1 cycle to execute.

So my instruction calculation is based on the final 32MHz that i have set or based on the 4MHz xtal.

I suppose each instruction executing timing is using the 4MHz and the 32MHz that I have set is for Timer. Can anyone correct me if I made mistakes?

Million Thanks.. :)
 
Last edited:
Depending on what you set as you device clock. Why do you have an external xtal if you want to use the internal one?

To use the internal clock, you must set it more or less like this:
CONFIG OSC=INTIO67
and
Code:
	movlw	b'01100110'	; configure the cpu clock
	movwf	OSCCON		; Internal OSC @ 4MHZ
	movlw	b'01000000'	; with PPL ON = 16MHz
	movwf	OSCTUNE		; code execute at 16MHz/4=4Mhz (0.25us)
will set your device to use the internal clock.
 
Last edited:
Its like Boomslang said, you can't use both external crystal AND Internal oscillator. Use INTIO67 to enable the internal oscillator, and then the pins that your xtal were connected to, can be used as a couple of extra I/O..
so... now your are set as Boomslang said, your calculation is based on 4mhz (internal osc) with osctune cleared(0) or 16mhz with osctune set (1)

Personally, I rarely used xtals, unless really necessary

Good luck
Jim :)
 
I'm sorry to ask, but with is the PPL and for what is usefull? As I read, means that the MCU can achive highers speeds activating the PPL to multiply the normal OSC freq?

Best regards
 
Last edited:
Hi Amsa84.

PLL (not PPL) is a 4x clock multiplier that can be used to achieve a higher clock speed when using the internal oscillator ie 4 x 8= 32Mhz. It can be configured by your software to run at slower speeds, and speed up when needed. This would be useful in a portable situation, to save power..
 
Depending on what you set as you device clock. Why do you have an external xtal if you want to use the internal one?

To use the internal clock, you must set it more or less like this:
CONFIG OSC=INTIO67
and
Code:
	movlw	b'01100110'	; configure the cpu clock
	movwf	OSCCON		; Internal OSC @ 4MHZ
	movlw	b'01000000'	; with PPL ON = 16MHz
	movwf	OSCTUNE		; code execute at 16MHz/4=4Mhz (0.25us)
will set your device to use the internal clock.

Hi Boomslang,

I did have the following lines on my code.

#pragma config OSC = INTIO67

OSCCONbits.IRCF2 = 1; /* ---------------------------- */
OSCCONbits.IRCF1 = 1; /* Set clock frequency to 8MHz */
OSCCONbits.IRCF0 = 1; /* ---------------------------- */

OSCTUNE = 0b01011111; /* Enable Frequency Multiplier PLL & Max
frequency tuning to 32MHz */

Does this mean that my micro-controller is using the internal clock, not the external 4MHz?

By setting #pragma config OSC = INTIO67, is it mean that the external 4MHz crystal does not have effect on the micro-controller?

Thus, my each instruction timing calculation will be based on the Internal 32MHz? Am I right? :)

Please advise.. Thankz.
 
Its like Boomslang said, you can't use both external crystal AND Internal oscillator. Use INTIO67 to enable the internal oscillator, and then the pins that your xtal were connected to, can be used as a couple of extra I/O..
so... now your are set as Boomslang said, your calculation is based on 4mhz (internal osc) with osctune cleared(0) or 16mhz with osctune set (1)

Personally, I rarely used xtals, unless really necessary

Good luck
Jim :)

OH... meaning that if i enable the internal oscillator than the external crystal will have no effect on the micro-controller? Am I right to say that? :)

If i'm right, can i simply plug out the external crystal if I enable the internal oscillator?

I do have the few lines that Boomslang reply in my code. :)
 
I'm sorry to ask, but with is the PPL and for what is usefull? As I read, means that the MCU can achive highers speeds activating the PPL to multiply the normal OSC freq?

Best regards

Hi AMSA84,

PLL will multiply your internal oscillator by 4 times where you can achieve a higher clock speed. You have to configure the PLL by software. I have the C version on how to configure it 2 post ago.
 
But in wich situation we need to active the PLL? How useful it is?

If you don't know what it is, then you don't require it!.

Essentially it's used to give 40MHz from a 10MHz crystal, but it can be used for any lower frequencies as well. You can't buy 40MHz crystals, so it's easier to multiply a 10MHz.
 
Right, I've already understood that (but as I said, is only for curiosity), maybe I'm not exaplaining myself.

What I want to know, is for what that is usefull. In what situation we need to apply the PLL?

Best regards
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top