1

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)
{
if(GPIO_Pin==detect_Pin){
if(HAL_GPIO_ReadPin(detect_GPIO_Port,detect_Pin)==GPIO_PIN_SET){
 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);
 }
else
{
 HAL_GPIO_WritePin(led_GPIO_Port,led_Pin,GPIO_PIN_SET);
} 

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?

Thanks

asked Oct 10, 2023 at 11:27

1 Answer 1

2

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() {
 Serial.begin(9600);
 attachInterrupt(digitalPinToInterrupt(pulse_pin),
 on_input_change, CHANGE);
}
void loop() {
 // Avoid a data race while reading last_input_change.
 noInterrupts();
 uint32_t last_input_change_copy = last_input_change;
 interrupts();
 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.

answered Oct 10, 2023 at 12:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.