Microcontroller delay synchronization !!!

Status
Not open for further replies.

hmahanna

New Member
Hi friends .....

I only want to know how does the delay code in pic c compiler (delay_ms(x)) synchronize with the Microcontroller clocking ???

Thanx ......
 
If I understand what you mean, then your oscillator will have to be defined in order for the delay routine to know how many cycles makes up your required delay.

For example, out of the C30 help file:

Default Behavior: This function relies on a user-supplied definition of FCY to represent the instruction clock frequency. FCY must be defined before header file libpic30.h is included. The specified delay is converted to the equivalent number of instruction cycles and passed to __delay32(). If FCY is not defined, then __delay_ms() is declared external, causing the link to fail unless the user provides a function with that name.

So in this example you would need to do:
Code:
#define FCY 5000000
For a 5MHz instruction cycle.
 
Last edited:
Thanx Gobbledok for helping ....... but what i mean that if i increase or even decrease the frequency of the oscillator , do that affect the delay routine ???

Thank you agian brother ...
 
Yes if you change the oscillator frequency then it will change the delay routines. They are (of course) directly linked to your clock speed.

If you change the crystal or PLL settings (for example) then you just redefine your instruction cycle frequency (FCY, above) to the correct one.

If you change the frequency on the fly then you will have to calculate the correct delay in your program based on your oscillator settings.
 
So How can i calculate the correct delay in my program based on my oscillator settings ???
 
I don't know, it depends on your program.

How many different oscillator speeds will you be using?? If it will only be a couple (full-speed and power-saving, for example) then maybe just have a different delay routine for each speed.

Or maybe have a register that represents your oscillator speed which you update when you change your oscillator speed. Then your delay routine checks that register and chooses the correct delay based on that.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…