Microcontroller: STM32F401RE Doing input capture on TIM1's channel 1,2,3,4. ARR register value is 65536. The clock is 8Mhz. The 4 input capture channels are connected to 4 microphones. As soon as a sound(bang) occurs all of these channels change their state from 0 to 1. The input capture is set on the rising edge. Whichever microphone is closer to the bang that microphone changes it's state first and with respect to their distance the rest follow. So, let's say my counter is at 100 and a bang occurs causing 4 input captures. As soon as the input capture happens the counter value at that instance is stored in the respective channels CCRx Register.(let's say CCR1 stores 100,CCR2 stores 103, CCR3 stores 104,CCR4 stores 107) Once I get these 4 values I have to find the time between the first input capture and the rest of the input capture.
Eg- CCR2-CCR1=103-100 ; CCR3-CCR1=104-100; CCR4-CCR1=107-100;
As shown above there isn't a problem when these input captures happen at the 100th count. But when it happens at 65520 I get values such as 65520,65525,65535,5. In this scenario, the lowest value I get is actually the event that takes place last and because of the huge difference I don't get a proper answer. I can't use the overflow flag because the event can occur at any time. Even after 5 times it has overflown. The counter is always on and keeps on counting and the event(bang) can occur at any time. The maximum difference possible between the input captures is only 30 counts.
Is there some smart solution to this problem or I should write a code to actually identify this series of numbers and subtract numbers with 655xx with 65536 and numbers from 0 to 20 with 0 and then add the absolute differences?