cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 NUCLEO GPIO IDR BIT MASK PROBLEM

JJack.1
Associate II

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 discussion is locked. Please start a new topic to ask your question.
1 ACCEPTED SOLUTION

Accepted Solutions

> 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

View solution in original post

2 REPLIES 2

> 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

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