Skip to main content
AMat.1
Associate II
October 7, 2020
Solved

Inability to read 0 value from INPUT.

  • October 7, 2020
  • 4 replies
  • 2158 views

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.
Best answer by Uwe Bonnes

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

4 replies

Uwe Bonnes
Chief
October 7, 2020

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

AMat.1
AMat.1Author
Associate II
October 7, 2020

Sorry I didnt fully understood, can you explain more ?

Uwe Bonnes
Chief
October 7, 2020

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

AMat.1
AMat.1Author
Associate II
October 7, 2020

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
October 7, 2020

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

AMat.1
AMat.1Author
Associate II
October 7, 2020

lol , thanks, that makes sense

the 0&0=1 was an oopsie

Tesla DeLorean
Guru
October 7, 2020

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 VenmoUp vote any posts that you find helpful, it shows what's working..