cancel
Showing results for 
Search instead for 
Did you mean: 

Rising and falling edge on GPIO_STM32G4

sireevenkat1
Senior

Hi,

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.

sireevenkat1_0-1696936253459.jpeg

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Read HAL_GetTick within your interrupt and store as a global variable. To check if signal is lost see if difference from current HAL_GetTick and the one you stored is >10ms. If so, signal has been lost.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

6 REPLIES 6
AScha.3
Principal III

you could do it with a retriggerable monoflop , set to 10ms ; this would change output, if 10ms no trigger coming.

on a cpu this could be a timer ( ie. Tim 2..4) , set to Retriggerable one pulse mode , update interrupt (enable : UIE ) will trigger, when full time of timer is reached (if set to 10ms , get int then).

AScha3_0-1696938313149.png

 

If you feel a post has answered your question, please click "Accept as Solution".

Hi @AScha.3 ,

Thanks for the reply. are you telling to use timer instead of GPIO?. I need to use GPIO only for my application. Can't I check without timer? Can you clarify.

retriggerable monoflop - concept new to me but I will check. 

Thanks

the trigger in is a gpio pin...just look in Cube , when you select ETR (external trigger)

AScha3_0-1696945017475.png

you just cannot use any pin you like , every timer has some fixed pin numbers for this function.

If you feel a post has answered your question, please click "Accept as Solution".
TDK
Guru

Read HAL_GetTick within your interrupt and store as a global variable. To check if signal is lost see if difference from current HAL_GetTick and the one you stored is >10ms. If so, signal has been lost.

If you feel a post has answered your question, please click "Accept as Solution".
sireevenkat1
Senior

Hi @TDK,

is it correct?

volatile uint32_t last_input_change=0;

const uint32_t pulse_timeout = 10;

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{

if(GPIO_Pin==Zerocross_detect_Pin){

last_input_change =HAL_GetTick();

}

}

void fault()

{

uint32_t last_input_change_copy = last_input_change;

if(HAL_GetTick()-last_input_change_copy>=pulse_timeout)

      HAL_GPIO_WritePin(led_GPIO_Port,led_Pin,GPIO_PIN_SET);
}

TDK
Guru

Looks good to me.

If you feel a post has answered your question, please click "Accept as Solution".