Skip to main content
lukevassallo95
Associate
November 6, 2016
Question

STM32F091RC IO toggle

  • November 6, 2016
  • 4 replies
  • 828 views
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>

    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    November 6, 2016
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    lukevassallo95
    Associate
    November 6, 2016
    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.
    Tesla DeLorean
    Guru
    November 6, 2016
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    lukevassallo95
    Associate
    November 6, 2016
    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 :)