calculation is: the ending_edge time, when ICP goes set again, plus how many times overflow happened, minus the starting_edge when first ICP was set.
I also can't understand this line:
period_out = ~(clocks / 750);
Well the timer is clocked by Fclk/8 (6MHz / 8 = 750) so 750 counts per ms, so we divide clocks by 750, but then what does the "~" operator there please?!
I would greatly appreciate the help to understand these moments.
It will compile just fine, but variables shared between ISR's and other functions and main must be declaread volatile, example:
volatile unsigned char ov_counter;
Is still a mystery to me, I really need to finish studying this book and fully understand it, please help if you can, by explaining the line. I'll upload a scan of the part the code is explained and attach (start from 2.7.3. on the middle of the first page ignoring the code of first page).
starting_edge and ending_edge are when events of interest occurred. The timer counts up to 65,636, then overflows. Each time it overflows ov_counter is incremented. So, the total time is 65536 * the number of overflows + ending_edge - starting_edge.
Think of it like this. Supposed starting_edge represented a time, like 2:30 Wednesday afternoon, and ending_edge represented a time, like 8:30 Saturday morning. You want to determine the elapsed time between those events. There is part of Wednesday, all of Thursday and Friday, and part of Saturday to consider. All of Thursday and Friday is represented by 2 in ov_counter. So the total time is 9.5 (for Wednesday) + 2 * 24 (hours) + 8.5 (for Saturday).
The ~ operator is bitwise NOT. Its closely related to negation (for 2's complement arithmetic which is what nearly all computers use for signed integers)
~a == -1-a
A final question: Then how I know that the clock (variable, in the formula below) itself will not over flow? Since even a 'long' may not be enough!
You should figure out what the type is for clocks. Then, see what the range of values are for that type. Then, consider what period of time that represents, before the value rolls over.
On the Arduino, millis() returns an unsigned long, which rolls over after approximately 49 days.
PaulS:
starting_edge and ending_edge are when events of interest occurred. The timer counts up to 65,636, then overflows. Each time it overflows ov_counter is incremented. So, the total time is 65536 * the number of overflows + ending_edge - starting_edge.
Think of it like this. Supposed starting_edge represented a time, like 2:30 Wednesday afternoon, and ending_edge represented a time, like 8:30 Saturday morning. You want to determine the elapsed time between those events. There is part of Wednesday, all of Thursday and Friday, and part of Saturday to consider. All of Thursday and Friday is represented by 2 in ov_counter. So the total time is 9.5 (for Wednesday) + 2 * 24 (hours) + 8.5 (for Saturday).
There is a mystery in this still for me!
When you counted Wednesday you took out the 2:30 from the whole, i.e. 12 - 2.5 = 9.5. Next the complete formula is:
12 - 2.5 + 8.5 + x*2*12
According to you model, I think the counting for MCU must be: "65536 - starting_edge + ending_edge + x*65536", but there is one time 65536 lost in the book's formula! Isn't it?