This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

delay milisecond

Hi all Iam using STR912FW44 based board and I want a delay function that should give the delay in milisecond I made a function


void delay_ms (unsigned long nCount){   /* Wait function                    */
  nCount=nCount+16000000L;

  while (nCount--);
}


when I call

delay_ms(10000);

it gives arround 10 second delay but when I call

delay_ms(20000);
it should give arround 20 second delay but it does not happen

but when I call

delay_ms(10000);
delay_ms(10000);

it gives arround 15 sec delay

please tell me exact way to build a delay function

regards
rupesh

Parents
  • First off - you should replace your "timed" busy-loop with a real delay. Tamir seems to have helped out with options.

    But you should also invest some time into debugging. You are sending in a ms value into your function and then adding (!) a fixed constant. Your function will give almost the same delay even if you send in the delay value zero.

    If course, you will have to _multiply_ the ms value with a constant to get the number of turns through the loop.

    Just reading the code should have caught this. Stepping through the code with different input parameters should have caught this. Testing with the special case 0 should have caught this. Do spend time on debugging before asking for help. If you can't manage to figure out the problem with a single loop - how do you think you will manage to write real code?

    But once more: Do _not_ use this kind of delay. A change in compiler version, compiler optimization or possibly just available register variables may totally change the speed of a loop.

Reply
  • First off - you should replace your "timed" busy-loop with a real delay. Tamir seems to have helped out with options.

    But you should also invest some time into debugging. You are sending in a ms value into your function and then adding (!) a fixed constant. Your function will give almost the same delay even if you send in the delay value zero.

    If course, you will have to _multiply_ the ms value with a constant to get the number of turns through the loop.

    Just reading the code should have caught this. Stepping through the code with different input parameters should have caught this. Testing with the special case 0 should have caught this. Do spend time on debugging before asking for help. If you can't manage to figure out the problem with a single loop - how do you think you will manage to write real code?

    But once more: Do _not_ use this kind of delay. A change in compiler version, compiler optimization or possibly just available register variables may totally change the speed of a loop.

Children