cancel
Showing results for 
Search instead for 
Did you mean: 

Inability to read 0 value from INPUT.

AMat.1
Associate II

I was trying to program STM32 to read the values and toggle the LED, but it didn't work. Please help

1 ACCEPTED SOLUTION

Accepted Solutions
Uwe Bonnes
Principal II

You use bitwise and with a bitmask of 0, so the result = 0. (0&0) = 0 = false.

View solution in original post

8 REPLIES 8
Uwe Bonnes
Principal II

(0 << 1) evaluates to 0 and if (0) never executes!

AMat.1
Associate II

Sorry I didnt fully understood, can you explain more ?

Uwe Bonnes
Principal II

if (GPIOA->IDR & (0 << 1)) will never get true!

if IDR is all 0's and its ANDed with 0 , how will it never be true? for example , IDR = 0 and 1st bit = 0 (0&0) = True = 1

Uwe Bonnes
Principal II

You use bitwise and with a bitmask of 0, so the result = 0. (0&0) = 0 = false.

lol , thanks, that makes sense

the 0&0=1 was an oopsie

You also probably don't want to blind write into the entire ODR, as it will impact all bits. Use BSRR for such writes

GPIOC->ODR |= (1 << 13); // set PC13

GPIOC->ODR &= ~(1 << 13); // reset PC13 (uses tilde, stupid forum prints as minus)

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

Better use BSRR: Only one access:

GPIOC->BSRR = (1 << 13); // set PC13

GPIOC->BSRR = (1 << (13 + 16)); // reset PC13