Programming for Time


The time value determines relative time. For example, if a loop is executed for only 2 s it could be coded as:

time-out = Tick Count( ) + 120 ; /* 60 tics per second = 2 seconds */

do

{

got_one = Check_Result( ) ;

}

while( ( ! got_one ) && ( SystemTick( ) <timeout ) ) ;

This code fragment continues to call the Check_Result function until it returns a true value, or until 2 s have elapsed.

This technique is independent of processor speed. A faster processor makes thousands of trips through the loop, while a slower one only makes a few hundred. Anticipated result codes arrive within the 2 s real-time window.

Carefully consider when to start the timing loop. If an AT command string is sent, then a loop executed, the time interval may also include the time required to send the AT command (if data is buffered and sent by an ISR).

At 300 bits/s, where each character takes 33 ms just to transmit (10/300), a 40 character AT command takes over 1 s to transmit. This means a 2 s loop spends more than half of its time waiting for the AT command process to complete, leaving only a fraction of a second for the modem to respond with the result (again at 33 ms per character).

To avoid this, wait until all data is transmitted by an ISR before entering the result code scan loop. Alternatively, you can provide more time for loops to process results. A second option is to measure idle time rather than elapsed time.

Click here to return to the Contents page.