Hello,
The input capture values represent the timer count for each edge. You will need to ascertain how many microseconds each timer increment represents. Additionally, the prescale value should be such that the timer overflow period is greater than the period of the minimum frequency (20 ms).
There is a problem with the code -
total = second - first;
frequency = 1/total;
Since all variables are unsigned integer, all calculations will be of integer type. The value of frequency will always be zero because the numerator has not been appropriately scaled.
For example, if the timer increment period was 1.0 microseconds, the value of total would lie between 6666 and 20000. To get an integer frequency value in say, multiples of 0.1Hz, something like the following code would be required -
frequency = 10000000L / total;
This should give a result between 500 and 1500.
An additional point - if the output waveform from the sensor is as shown, I think this should be buffered and "squared up" before being applied to the capture input of the MCU. Presumably you also need robust external protection against transient voltages.
Regards,
Mac