I am working with STM32G491RE.I am giving pulse to GPIO pin from function generator. That pulse will be 2 msec on time and 8msec off time.I need to make flag high if the signal from function generator lost. Can anyone suggest me how to do this?

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
 HAL_UART_Transmit(&huart2, (uint8_t *)"rising:",7, HAL_MAX_DELAY);
else if(HAL_GPIO_ReadPin(detect_GPIO_Port,detect_Pin)==GPIO_PIN_RESET){
 HAL_UART_Transmit(&huart2, (uint8_t *)"falling:",8, HAL_MAX_DELAY);

I tried with EXTI interrupt but If input is there from generator its detecting both rising and falling edge and if there is no signal from generator I am not getting in to else condition.

img: pulse input from function generator.

1.If I want to make flag high for no signal from function generator,above image offtime also it considering as no signal.

can anyone suggest how to do?


1 Answer 1


I do not know how to use the HAL API you are using. I will instead suggest an approach based on the Arduino API, which is more suitable for this site.

Take note of the current time on every transition you detect. When the time since the last detected transition is longer than a predefined timeout, you know the pulse train has stopped:

const uint8_t pulse_pin = 2;
const uint32_t pulse_timeout = 10;  // milliseconds

volatile uint32_t last_input_change;  // last time we detected a change

void on_input_change()
    last_input_change = millis();

void setup() {
            on_input_change, CHANGE);

void loop() {
    // Avoid a data race while reading last_input_change.
    uint32_t last_input_change_copy = last_input_change;

    if (millis() - last_input_change_copy >= pulse_timeout)
        Serial.println("The pulse train stopped.");

Note that, for this to work, your whole code should be non-blocking, which is a good practice in any case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.