
#include <reg51.h>

// T0 must be running in mode 1


unsigned int read_t0_on_the_fly( void )
{
  unsigned char tl, th;
  th = TH0;
  tl = TL0;
  if( (tl & 0x80) == 0 )
    th = TH0;
  return tl | th << 8;
}


void delay_us( unsigned int t )         // 20 ... 60000us at 12MHz
{
  unsigned int d = read_t0_on_the_fly();

  while( read_t0_on_the_fly() - d < t );
}
