Listing 1—This routine for receiving a code word is independent of the implementation details for receiving a single bit. Modular code improves software flexibility and maintainability at no extra cost because modern compilers can do wonders optimizing the resulting code. The code structure closely reflects what code word reception is all about: taking bits one at a time, shifting them into a buffer, and eventually dropping the stop bit.
int receive( void )
{
	BYTE bits;
	T_ZERO_ONE_INVALID new_bit;
	int codeword = CODEWORD_INVALID;

	bits = 0;
	while ( bits < 10 && quadrature_encoder_button() )
	{
	new_bit = receive_bit();
	if (new_bit == BIT_INVALID )
	{
	bits = 0;
	codeword = CODEWORD_INVALID;
	}
	else
	{
	codeword = (codeword << 1) | new_bit;
	bits++;
	};
	};
	skip_stop_bit();
	return codeword;
}