Skip to main content
AMat.1
Associate II
Oct 7, 2020 1:46 PM UTC
Solved

Inability to read 0 value from INPUT.

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

This topic has been closed for replies.
Uwe Bonnes
Uwe BonnesBest answer
Chief
Oct 7, 2020 4:06 PM UTC

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

8 replies

Uwe Bonnes
Chief
Oct 7, 2020 1:51 PM UTC

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

AMat.1
AMat.1Author
Associate II
Oct 7, 2020 2:24 PM UTC

Sorry I didnt fully understood, can you explain more ?

Uwe Bonnes
Chief
Oct 7, 2020 3:09 PM UTC

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

AMat.1
AMat.1Author
Associate II
Oct 7, 2020 3:49 PM UTC

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
Uwe BonnesBest answer
Chief
Oct 7, 2020 4:06 PM UTC

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

AMat.1
AMat.1Author
Associate II
Oct 7, 2020 4:15 PM UTC

lol , thanks, that makes sense

the 0&0=1 was an oopsie

Tesla DeLorean
Guru
Oct 7, 2020 4:58 PM UTC

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..