Skip to main content
JJack.1
Associate II
February 8, 2022
Solved

STM32 NUCLEO GPIO IDR BIT MASK PROBLEM

  • February 8, 2022
  • 2 replies
  • 1417 views

Hello,

I'm using STM32 NUCLEO board.

In my application, I want to read GPIO PORT A input register (IDR) & (pins setup as inputs).

With that variable, I want to do something, if specific pins are HIGH, execute the if cycle.

As an example, I want to execute my logic if PIN 0 and PIN1 both of them HIGH, All I do in while cycle is:

read=GPIOA->IDR;

if(read &0b0000000000000011)

{

Do something if PIN 0 and PIN1 are HIGH

}

else

{

Do something if the statement above was FALSE.

}

In debugger mode I can see the status of the pins, and if I changing them between high/low the register see that, but my if cycle doesn't seem to work. Sometimes it executes if, sometimes executes else statement. Is it something wrong with the bitmask?

This topic has been closed for replies.
Best answer by waclawek.jan

> if(read &0b0000000000000011)

This evaluated true, if either one or both pins are high.

If you want to test for both pins being high, not only one of them, use

if ((read & 0b0000000000000011) == 0b0000000000000011) {

do_something();

}

JW

2 replies

waclawek.jan
waclawek.janBest answer
Super User
February 8, 2022

> if(read &0b0000000000000011)

This evaluated true, if either one or both pins are high.

If you want to test for both pins being high, not only one of them, use

if ((read & 0b0000000000000011) == 0b0000000000000011) {

do_something();

}

JW

JJack.1
JJack.1Author
Associate II
February 8, 2022

Thanks for a reply! Seems reasonable. Let me try this tomorrow see if it works!