2020-10-07 06:46 AM
I was trying to program STM32 to read the values and toggle the LED, but it didn't work. Please help
Solved! Go to Solution.
2020-10-07 09:06 AM
You use bitwise and with a bitmask of 0, so the result = 0. (0&0) = 0 = false.
2020-10-07 06:51 AM
(0 << 1) evaluates to 0 and if (0) never executes!
2020-10-07 07:24 AM
Sorry I didnt fully understood, can you explain more ?
2020-10-07 08:09 AM
if (GPIOA->IDR & (0 << 1)) will never get true!
2020-10-07 08:49 AM
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
2020-10-07 09:06 AM
You use bitwise and with a bitmask of 0, so the result = 0. (0&0) = 0 = false.
2020-10-07 09:15 AM
lol , thanks, that makes sense
the 0&0=1 was an oopsie
2020-10-07 09:58 AM
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)
2020-10-07 10:48 AM
Better use BSRR: Only one access:
GPIOC->BSRR = (1 << 13); // set PC13
GPIOC->BSRR = (1 << (13 + 16)); // reset PC13