Tuesday, December 5, 2006

Modifications performed in the software of short-range sensing system

In the previous post, we have given the code segment running in the host microcontroller which requests and then receives sensor reading from each 12F683. This code waits for the sensor reading to be sent indefinitely, which may hang up the host microcontroller. Watchdog timer of the host microcontroller is enabled to overcome this problem, which is actually quite successful. It is preferred to modify the code segment to prevent hang up, instead of using watchdog timer in 16F877A (watchdog timer is still used in 12F683's). The code is modified and is given below:

timeout = 0;
output_toggle(SENSOR0_TX);
while (!kbhit(SENSOR0)&&(++timeout<100))
...delay_us(1);
if(timeout<100)
...sensorData[0] = getc(SENSOR0);
else
...sensorData[0] = 0;

In this code, the host microcontroller waits for approximately 100 us for the sensor reading to be sent and if it is not received it sets the reading to 0 and passes to the next sensor. In this way, the host doesn't hang up indefinetly. Two important points to note in this code segment are:

  • delay_us(1): The delay should be as small as possible not to miss the start bit.
  • if(timeout<100): This if condition is used to test whether the program exited while condition due to timeout condition or reception of startbit. In this if statement, timeout condition should be checked instead of kbhit() because if the starbit is received, checking kbhit() will make 16F877A miss the next data bit (since kbhit() function will treat the next data bit as the start bit).

No comments: