Setting a Delay
The word ms will wait for n milliseconds before continuing, up to a maximum of 65,535 milliseconds. For example, to turn the LED on, wait for 2 seconds, then turn the LED off:
ledon 2000 ms ledoff
The word ms puts the PIC24 processor into "IDLE" mode during execution, thus reducing power consumption.
Measuring Intervals
The word ticks will place a 16-bit count in milliseconds onto the stack. The tick count rolls over to zero every 65,535 milliseconds (65.535 seconds). ticks can be useful in determining how long your code takes to execute. For example, we can determine how long blink takes to execute by using ticks to place its count on the stack before and after blink, then subtract the second count from the first to determine the delay:
ticks blink ticks – abs .
As it is possible for the tick counter to roll through zero between the two calls of ticks, giving a negative result from the subtraction, we use the word abs (absolute) to convert the result of the subtraction to an absolute value.
Note that there will be an extra few milliseconds due to the overhead of word execution. For example:
Note that there will be an extra few milliseconds due to the overhead of word execution. For example:
ticks 500 ms ticks – abs .
gives an output of 504 milliseconds or so, rather than 500 milliseconds. This is the (small) overhead associated with calling the words ms and ticks.