cancel
Showing results for 
Search instead for 
Did you mean: 

Detect GPIO Low/High state without using interrupt or busy/wait strategy

deckhard
Associate III

Hi

I want to detect a GPIO turning low/high in less than 1 mili/sec granularity.

I can use an interrupt (edge) or I can busy/wait with HAL_GPIO_ReadPin which would be in 1 mili/sec granularity because of the systick.

Is there any other way of doing that?

5 REPLIES 5

Timer Input Capture.

JW​

Can you please refer me to an example?

  • enable GPIOx clock in RCC, enable TIMx clock in RCC
  • set TIMx_CHx pin to AF in GPIOx_MODER
  • set that pin to given AF number for TIMx in GPIOx_AFR[]
  • set TIMx_CHx to Input Capture in TIMx_CCMRx.CCxS
  • optionally set polarity in TIMx_CCER.CCxP (and CCxNP) and filter in TIMx_CCMRx.ICxF; but the reset values are OK for first experiments so you can simply leave them untouched
  • enable Input Capture for TIMx_CHx by setting TIMx_CCER
  • enable TIMx counting by setting TIMx_CR1.CEN

Then, whenever you read TIMx_CCRx, it contains the value of TIMx_CNT which was at the moment of latest input edge on TIMx_CHx pin.

Read Input capture mode subchapter of TIM chapter in RM.

  

If you'd want not just the last edge but also a "history", you'd need to set up DMA to read out TIMx_CCRx into memory upon each capture event, but that's step 2.

JW

deckhard
Associate III

What if I need to know exactly the first input edge occurs, than I must use an interrupt driven design right?

One could presumably use DMA to capture time-stamps into a ring buffer

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..