Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to calculate delay from microcontroller oscillator frequency?

Status
Not open for further replies.

hukiju

Newbie level 6
Newbie level 6
Joined
Jun 28, 2012
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,381
Hi all,

I have a PIC18F4550 with oscillator crystal 20MHz (C Compiler), I would like to understand more on how to calculate 1 second delay from this 20MHz.

Refering to thread (https://www.edaboard.com/threads/193407/), Post #6,
---------------------------------------------------------------------------------------------------------
Hi,
For having an approximately 1 second delay i've ued a delay function same as the following:
In a C compiler for PIC (PIC18F4550-selected frquency 48MHz) the 1 second delay function is as below:
void delay (void){
unsigned long i;//(long in this compiler is 4 Bytes)
for(i = 0;i<1000000;i++);
}
---------------------------------------------------------------------------------------------------------

He mentioned that he is using PIC18F4550-selected frquency 48MHz and his 1 second delay function is:
void delay (void){
unsigned long i;//(long in this compiler is 4 Bytes)
for(i = 0;i<1000000;i++);
}

How do he know that he need to loop for 1000000 times to get 1 second delay?

Appreciate for any help given!

Thanks

Lee
 

Hello!

For any processor, there should be a bunch of papers that most engineers think of as
useless. These papers are sometimes called user manual, specs sheet, etc...

In the user manual, there should be sections stating explicitly how many clocks you need for
one asm instruction. For example: branch might be 5 clocks, 16-bit x 16 bits multiply = 5 clocks,
etc...
Now for one loop, you have basically one compare, one increment (addition) and one jump.
If you add these 3, then you will know the number of clocks you need for an empty loop.
In your example, it would mean that the empty loop takes 48 clocks, which is not the case,
I guess. It's very processor dependent and I don't know PIC, but on the processors I am used
to, one loop is about 10 clocks. If the loop needs 4 bytes values as in your case, that might
be slightly higher, but for sure not 48 clocks.

Dora.
 
  • Like
Reactions: hukiju

    hukiju

    Points: 2
    Helpful Answer Positive Rating
Hi doraemon,

Thanks for reply. Ya, i know there is a user manual for this PIC. However, since I'm still quite new to PIC, I unable understand it easily. Frankly, I don't really 100% understand the things you explained to me.

Oh ya, btw, I'm using C compiler instead of asm.

is it 48 clocks mean 48 MHz?

As far as I know, the more instruction I have, the longer it takes to complete one loop, am I right?

How do I calculate the time required to complete one loop?

Thanks!
 

best possible way that I can think of using interrupt timer and count logic for generating the dealy....and make some the pin of the pin toggle on counter and observe the wave form of that pin using CRO ..... This will give you the proper delay measured for the delay generated wave form from the crystal on the board..... Also as mention in the above the datasheet, user manual, spec sheet....are some of the ways to understand the chip better....

Good Luck
 
  • Like
Reactions: hukiju

    hukiju

    Points: 2
    Helpful Answer Positive Rating
best possible way that I can think of using interrupt timer and count logic for generating the dealy....and make some the pin of the pin toggle on counter and observe the wave form of that pin using CRO ..... This will give you the proper delay measured for the delay generated wave form from the crystal on the board..... Also as mention in the above the datasheet, user manual, spec sheet....are some of the ways to understand the chip better....




Good Luck

Hi, do you mind to show me how you do it? Thanks bro
 

If you have a spare timer in your PIC, follow milind.a.kulkarni's good suggestion and set it up to generate a delay (monostable mode). Then start it running when your delay needs to begin and poll the timeout flag of the timer in a loop until it ends.

As said by others here, I'm afraid this isn't the site to tutor you in how to use a PIC. You'll have to read all this up yourself, there's plenty of free documentation. Understand how microprocessor machine code instructions are executed, you have to know that stuff well to get the best out of it in a real-time environment. Is this a college/university assignment?

Happy reading...
 
  • Like
Reactions: hukiju

    hukiju

    Points: 2
    Helpful Answer Positive Rating
If you have a spare timer in your PIC, follow milind.a.kulkarni's good suggestion and set it up to generate a delay (monostable mode). Then start it running when your delay needs to begin and poll the timeout flag of the timer in a loop until it ends.

As said by others here, I'm afraid this isn't the site to tutor you in how to use a PIC. You'll have to read all this up yourself, there's plenty of free documentation. Understand how microprocessor machine code instructions are executed, you have to know that stuff well to get the best out of it in a real-time environment. Is this a college/university assignment?

Happy reading...

Hi, I'm learning this is just simply a hobby and home usage. "If you have a spare timer in your PIC", did you mean that the internal timer like Timer0, Timer1, Timer2? Mind to briefly explain how to use these timer? It's ok if you don't as I know this is not a site to tutor me in how to use a PIC.

Thanks.

Lee
 

Hello!

is it 48 clocks mean 48 MHz?

No, not exactly. The reason is as follows.
The program you quoted pretends that when you count up to 1 million with a clock frequency
of 48 MHz (48 million clocks per second), then you will get a delay of one second.
If a million loop is achieved with 48 million clocks, then on loop is achieved in 48 clocks.

As far as I know, the more instruction I have, the longer it takes to complete one loop, am I right?

Yes. And as this loop does nothing, I can't believe it takes 48 clocks. But again, I don't know PIC.

Dora.
 
  • Like
Reactions: hukiju

    hukiju

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top