Pulsein()

The code:
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
{
// cache the port and bit of the pin in order to speed up the
// pulse width measuring loop and achieve finer resolution. calling
// digitalRead() instead yields much coarser resolution.
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
uint8_t stateMask = (state ? bit : 0);
unsigned long width = 0; // keep initialization out of time critical area

// convert the timeout from microseconds to a number of times through
// the initial loop; it takes 16 clock cycles per iteration.
unsigned long numloops = 0;
unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;

// wait for any previous pulse to end
while ((*portInputRegister(port) & bit) == stateMask)
if (numloops++ == maxloops)
return 0;

// wait for the pulse to start
while ((*portInputRegister(port) & bit) != stateMask)
if (numloops++ == maxloops)
return 0;

// wait for the pulse to stop
while ((*portInputRegister(port) & bit) == stateMask) {
if (numloops++ == maxloops)
return 0;
width++;
}

// convert the reading to microseconds. There will be some error introduced by
// the interrupt handlers.

// Conversion constants are compiler-dependent, different compiler versions
// have different levels of optimization.
#if GNUC==4 && GNUC_MINOR==3 && GNUC_PATCHLEVEL==2
// avr-gcc 4.3.2
return clockCyclesToMicroseconds(width * 21 + 16);
#elif GNUC==4 && GNUC_MINOR==8 && GNUC_PATCHLEVEL==1
// avr-gcc 4.8.1
return clockCyclesToMicroseconds(width * 24 + 16);
#elif GNUC<=4 && GNUC_MINOR<=3
// avr-gcc <=4.3.x
#warning “pulseIn() results may not be accurate”
return clockCyclesToMicroseconds(width * 21 + 16);
#else
// avr-gcc >4.3.x
#warning “pulseIn() results may not be accurate”
return clockCyclesToMicroseconds(width * 24 + 16);
#endif

}

Here is some information on what is with the registers:

Can someone explain me the code behind pulsein function.

Yes, if you can say what part you don't understand.

May someone tell me how it measure time or signals and then convert it into ms.

It counts the number of times it runs through a loop. It multiplies that by the number of clock cycle each time through the loop takes (21 or 24 depending on the compiler version). It adds the amount of fixed overhead cycles (16) to get total clock cycles during the pulse. It then divides that by the number of clock cycles per microsecond (16 on a 16 MHz Arduino) to get the pulse length in microseconds (not milliseconds).

What is with registers

It is faster to read a full 8-bit I/O register and use the AND operator (&) to isolate one pin from that register than it is to use digitalRead() to read an individual pin.

and how the function know that the state of signal is "1" and begin with measurment and when is "0" to stop.

The 'stateMask' is set to the bit mask when you are looking for a HIGH pulse and 0 when you are looking for a LOW pulse. The loops do logical operations on the input register contents and the 'stateMask' to determine if the input pin is IN a pulse or BETWEEN pulses.

The first loop waits for the input to be BETWEEN pulses so we don't measure a pulse that is already in progress.

The second loop waits for the input to change back to being IN a pulse. This marks the start of a pulse.

The third loop counts until the input changes back to BETWEEN pulses. It is this third loop that measures the pulse width.