cancel
Showing results for 
Search instead for 
Did you mean: 

Digital input timing

marcogrossi89
Associate II
Posted on September 27, 2017 at 11:43

I work on a STM32F334 Nucleo board featuring the STM32F334R8 microcontroller.

I have the need to check the value of the lowest 8 bits of two digital ports (only PC0-7 is used in this example) as quick as possible.

I tried the following code

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);

if ((uint8_t)((GPIOC)->IDR) > 250)

my_port = my_port+1;

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);

and monitored PA7 (digital output) with an oscilloscope. The result is two pulses: the first of width about 900 ns and the second of about 700 ns. Since the second pulse is only from setting and resetting PA7, I assume that the code

if ((uint8_t)((GPIOC)->IDR) > 250)

my_port = my_port+1;

takes about 200 ns to complete and only the first instruction is executed since 

(GPIOC)->IDR) is lower than 250.

Since the clock frequency is set to 64 MHz (8 MHz internal oscillator with PLL) this means that about 12 clock cycles are needed only for the first instruction. Since I need to make the comparison on two different ports then I need a minimum of 400 ns for the execution and that means that the whole operation can have a maximum frequency of about 2 MHz.

Is there any way to increase such frequency (faster digital port acquisition and compare)?

Thanks.

#digital-input-timing
3 REPLIES 3
Posted on September 27, 2017 at 14:14

uint8_t *p = (uint8_t *)&GPIOC->IDR;

...

if (*p > 250)

 my_port++;

Will work most efficiently if the pointer is kept held in a register.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 27, 2017 at 14:19

... and runs from a zero latency program memory...

JW

Posted on September 27, 2017 at 15:29

I'd also engage infinite improbability drive...

Could perhaps pull via DMA and decimate via HT/TC

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