Question about reading an input port

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So I'm testing out the I/O on a new Xmega32A4 board of mine. I've hooked up a normally high sensor to C.0 and a scope probe, and another scope probe on D.0. The idea is when there's a sensor hit (C.0 goes low), I'll set D.0 high, and vice-versa. The following code, however, hangs on the line (PROBLEM LINE) waiting for the sensor to clear (return high). Even though the scope and AS4 both show C.0 high, the while loop never exits.

On the other hand, if I replace PROBLEM LINE with ALTERNATE code, everything works fine. What's a newbie to do?

Thanks.

int main(void)
{
    char x;
    ClockConfig();
    PORTE.OUTSET = 0xf0;    
    PORTD.OUTCLR = 1;
    PORTE.DIR = 0xf9;       
    PORTC.DIR = 0;          // Input
    PORTD.DIR = 0xff;       // Output
    while (1)
    {
        while (PORTC.IN & 1)    // Wait for sensor hit
            ;
        PORTD.OUTSET = 1;       // Set D.0 high
        while (~(PORTC.IN & 1)) // PROBLEM LINE.  Wait for
                                // sensor to clear (go
                                // high again).
            ;
        PORTD.OUTCLR = 1;       // Set D.0 low
    }
    return 0;
}

ALTERNATE CODE

while (1)
{
    if (PORTC.IN & 1)
        break;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ooops! That ~ is supposed to be a !. Shame on me. Please disregard post.