OMG, it is 5 years old explanation, but I'll try - I think iMo wanted to say the values used in that math were always max 50000, even though a value off the timer registers is 16bit or max 65535, therefore the term "modulo 50000".
PS: for example a counter in "modulo 5" means it counts 0,1,2,3,4,0,1,2,3,4,0,1,2.. - the counter wraps around when it reaches the "modulus" 5.
Thank you very Much iMo, and you're right talking to you in third person as a 5-year-old answer is like another person's answer
So, please correct me if I'm wrong, as I'm trying to understand Lars code, and it seems this line is one of the most important:
Timer_us = timer_us + 50000 - (((timer1CounterValue - timer1CounterValueOld) * 200 + TIC_Value - TIC_ValueOld)+50000500)/1000;
- The TIC_Value, if I understand correctly, measures time difference (in nanoseconds) between PPS rising edge and 1MHz rising edge. This is done by knowing the RC ramp behavior, measuring the voltage and hence estimating the time (time_diff_ns = ADC_read * 1ns/bit). So, if the ADC reads 500 and the old read was 450, that means 50ns off time difference
- The timer1CounterValue is what I do not understand. If timer1CounterValue - timer1CounterValueOld = 5, it means that there has been 100 overflows of 10ms and 5 additional counts? (which means the 5MHz signal counted 5million pulses 1us before the PPS).
With that said. Let's put an example:
Timer1CounterValueOld = 5
Timer1CounterValue=49995 (because the 5MHz signal goes slower than PPS). Without going to the formula, this means a difference of 10 pulses (10*200ns = 2us difference).
Supposing that TIC_Value-TIC_ValueOld = 0 (to simplify the example), let's go to the formula:
Timer_us = 50000 - (((49995 - 5) * 200) + 50000500)/1000;
Timer_us = 50000 - ((49990 * 200) + 50000500)/1000;
Timer_us = 50000 - ((49990 * 200) + 50000500)/1000;
Timer_us = 50000 - (9998000 + 50000500)/1000
Timer_us = 50000 - 59998500/1000
Timer_us = 50000 - 59998
Timer_us = -9998
Maybe I have done something wrong on this example, because I do not understand the actual/physical meaning of this timer_us variable.
Thank you again, I know this topic is very old