Listing 2—The algorithm
for receiving a bit looks for a rising edge and measures the time to the next
edge. Pulse duration discriminates the resulting logic value. The routine can
also return a third value (BIT_INVALID) in case of inconsistent timing during
the process. The code is independent of the input processing implementation hidden
behind the function pair is_zero() and is_one().
static T_ZERO_ONE_INVALID receive_bit( void )
{
unsigned long ticks;
unsigned int duration;
Timer16_restart();
while( is_zero() )
{ if ( timeout )
return( BIT_INVALID );
};
Timer16_restart();
while( is_one())
{ if ( timeout )
return( BIT_INVALID );
}
while( is_zero() )
{ if ( timeout )
return( BIT_INVALID );
}
ticks = ( TICKS_TIMEOUT - Timer16_wReadTimer() );
Timer16_Stop();
duration = ( ticks * 1000L ) / Timer16_FREQ;
if ( duration < MIN_DURATION )
return BIT_INVALID;
if ( duration < MAX_SHORT_DURATION )
return BIT_ONE;
else
return BIT_ZERO;
}