2016-11-06 05:58 AM
<p>Hello, I just started out with arm mcu's. I am using the Nucleo Board with the STM32F091RC mcu. </p>
<p>I encountered a problem that seems logically correct, however I receive no response from the board whatsoever. </p><code> if((GPIOC->IDR &= (1ul << 6)) == 0) { setRest_io(8,1); Delay(500); setRest_io(8,0); Delay(500); }</code><p>This is the code I am using such that when I press a switch(on PC.6), the output(PC.8) will start toggling. Set Rest(pin, set/Reset) function toggles the pin. </p><p>My problem is in the first statement i.e. <code> if((GPIOC->IDR &= (1ul << 6)) == 0) </code> When I assign it to 1, i.e. == 1, the output doesn't respond to switch inputs </p><p>Logically I am seeing no error, I expect when assigning the statement to 0, the output will toggle when asserted by the switch and when assigned to 1 the output will <strong>not</strong> toggle </p><p><em>Note that the switch is active low, i.e. by default the input pin, PC.6 is at logic high </em></p>2016-11-06 06:13 AM
You want & not &=
You should observe that 65535 & 64 = 64, not 12016-11-06 12:57 PM
But won't that fail when having additional inputs on Port C?
What I had in mind was to check that specific bit.However I do see that using GPIOC->IDR will use all of the 16 data bits.2016-11-06 01:41 PM
I'm not sure why that would fail, you seem not to grasp the bitwise or C implications here. I'm saying the answer isn't 1 for all cases except & 1.
if((GPIOC->IDR & (1ul << 6)) != 0) { // When the bit is set, vs your == 0 when the bit is not set
setRest_io(8,1);
Delay(500);
setRest_io(8,0);
Delay(500);
}
2016-11-06 03:25 PM
Yes of course! I was confusing the and operation.
As we shift to the left the result will always be larger than 1 other than the case that it's 1. The comparison operator '==' of course wasn't going to work! Thanks for clearing this up! Much appreciated :)