Why doesn't my GPIO pin stay high below 200 Hz in my frequency counter code?

I'm using a frequency counter code from Deep Blue Embedded to monitor RPM via a tachometer and trigger a relay when the frequency drops below 200 Hz. However, the output on pin PB14 doesn't stay high at low frequencies (tested between 100 Hz and 500 Hz). The signal on the oscilloscope fluctuates instead of staying steady. Here's the relevant portion of the code I'm working with:
while (1)
{
if(gu32_Freq <= 200)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); // Set pin high if frequency <= 200 Hz

if(gu32_Freq > 200)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); // Set pin low if frequency > 200 Hz
}
while (1)
{
if(gu32_Freq <= 200)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); // Set pin high if frequency <= 200 Hz

if(gu32_Freq > 200)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); // Set pin low if frequency > 200 Hz
}
Even when the frequency drops below 200 Hz, the pin doesn’t stay high as expected. Could the leftover UART code (without a connected hardware) be causing this? Also, I tried using PA0, but it didn’t work either. How can I safely remove the UART code without breaking the program?
Solution:
The frequency counter might not be reading consistently at lower frequencies.add a small debounce delay or hysteresis to stabilize the pin state. The UART code likely isn't affecting this, but if unused, you can comment it out or disable UART in the initialization code to avoid resource conflicts. For PA0 not working, ensure it's not reserved for other functions, like input capture.
Jump to solution
2 Replies
Solution
wafa_ath
wafa_ath2mo ago
The frequency counter might not be reading consistently at lower frequencies.add a small debounce delay or hysteresis to stabilize the pin state. The UART code likely isn't affecting this, but if unused, you can comment it out or disable UART in the initialization code to avoid resource conflicts. For PA0 not working, ensure it's not reserved for other functions, like input capture.
Sterling
Sterling2mo ago
Hi @wafa_ath This was really helpful, I added a small debounce delay and some hysteresis, which stabilized the pin state, and now the output behaves as expected at lower frequencies. I also commented out the UART code as suggested, and it didn’t seem to affect anything negatively. As for PA0, I realized it was reserved for input capture, so that explained why it wasn’t working for this case. Everything is working smoothly now! Thanks again!
Want results from more Discord servers?
Add your server