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.

delay calculation help

Status
Not open for further replies.

georgetwo

Member
hi
im new to pic programming, I saw a led c project online which worked well on my pic18f4550 but i want to edit the time delay. I need help in calculating the delay with respect to any osc. in c im using 12MHz crystal
Thanks
 
OK for PIC its simply :

1 CYCLE = 1/(FOSC/4)

example: 1 Cycle for a 12Mhz Clock on a PIC would be = 1/(12000000/4) = 1/3000000 = 0.000 000 3 = 300ns

so now that you have one cycle.. which here is 300ns. to make a delay for 100us we have to divide

100us / 300ns = 0.0001 / 0.0000003 = 333 .. thats how many cycles it takes to make 100us delay...

So if we delay 333 cycles its a 100us delay...

Understand it?
 
whith tha fomular, 1sec.= 1/300ns =3333333cycles
this gave me outrageous time. this is the time code

void Wait()
[
unsigned char i;
for (i=0;i<100;i++)
_delay(60000);
]
how do i calculate the delay time and adjust to the value i want?
thanks 4 your help so far
 
ok exactly 12mhz = 3 mhz internal clock:

1/3Mhz = 3.333333333333333e-7 = 0.0000003333333333333333

which is 0.0000003333333333333333 aka 333.3333333333333ns each cycle

1 second = 1 / 333.3333333333333ns = 3000000.0003= 3,000,000 Cycles... and yes its correct... check out this link:




To Calc it on the fly make some functions like ...

1 second = 3,000,000 cycles
1 millisecond = 3,000 cycles
1 microsecond = 3 cycles

Make loops for each like:
Code:
void delay_ms(int time)
{
    int i;
    for (i=0;i<time;i++)
    _delay(30000);
}

Now you can delay 10 ms with : delay_ms(10);

Be aware it wont be 100% accurate since you have to calculate how long it takes to make the call to the function... how long it takes for 1 loop and then to leave the function. But if 1ms off is ok im sure this will do
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top