cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F091RC IO toggle

lukevassallo95
Associate II
Posted on November 06, 2016 at 14:58

<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>

4 REPLIES 4
Posted on November 06, 2016 at 15:13

You want & not &=

You should observe that 65535 & 64 = 64, not 1

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lukevassallo95
Associate II
Posted on November 06, 2016 at 21:57

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.
Posted on November 06, 2016 at 22:41

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);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lukevassallo95
Associate II
Posted on November 07, 2016 at 00:25

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 :)