trying to read rc reciever using arduino

there folks,
i was going through the code provided in arduino community http://playground.arduino.cc/Code/ReadReceiver ,to read rc receaver ,but i am not able to understand this part of the code

tmp = ( (TCNT1>tmp) ? (tmp) : (ICR1-TCNT1)+ICR1 );//if we are counting down add the top value
//to how far we have counted down
return ((tmp*1000L)/(F_CPU /1000L))<<scale;

,it is a part of readTimer1() function ,i think it is AVR level code. i want to know that wat value will tmp will give ,cos i am not known to the terms TCNT1,ICR1 ,1000L etc etc…i know the bitwise logic , but i don’t know what these terms gives as output… please help…

thanks in advance

Are you asking about the code just so you can learn? Does your program work okay?

I also think the code looks confusing and I have a feeling there’s a lot involved in understanding it.

The author left a lot of clues about what sort of things to learn about.

I think the code looks very clever.

Maybe someone will educate us.

I think you might get more help if you read the “How to use this forum” and used code tags when posting code. I think it’s item #7. I’ve noticed a lot of people prefer code inside code blocks like this.

    tmp = (  (TCNT1>tmp) ? (tmp) : (ICR1-TCNT1)+ICR1  );//if we are counting down add the top value
                                                        //to how far we have counted down
    return ((tmp*1000L)/(F_CPU /1000L))<<scale;

Have a read of the datasheet for the ATMega328p, in particular the section 15.11 which is the register description for the 16-bit Timer/Counter1 with PWM.

"The Input Capture is updated with the counter (TCNT1) value each time an event occurs on the
ICP1 pin (or optionally on the Analog Comparator output for Timer/Counter1). The Input Capture
can be used for defining the counter TOP value.
The Input Capture Register is 16-bit in size. To ensure that both the high and low bytes are read
simultaneously when the CPU accesses these registers, the access is performed using an 8-bit
temporary High Byte Register (TEMP). This temporary register is shared by all the other 16-bit
registers."

Hope this helps