1

I am using an Xmega384C3, and all I am trying to do is send a signal down an output port and read it on an input port. I have PORTC set as an output port, and PORTA set as an input port. The corresponding pins on each port are shorted together (PortA pin 0 to PortC pin 0, etc).

My issue appears in the following code:

uint8_t i;
int count = 0;

for(i=2; i<8; i++) {
    PORTC.OUT = (1 << i);
    if (PORTA.IN == PORTC.OUT) {
        count++;
    }
}

if (count == 6) {
    //success
}

I basically just want to read that when I send a logic high down a pin on PORTC, that I can read it on the corresponding pin on PORTA. When I let the code run normally, it does not find any matches and therefore never increments count. However if I add a break point on the line where the if comparison occurs, it then evaluates to true and increments count as expected. Additionally, I can see the ports have the correct values in Atmel Studio's I/O view feature. Any ideas?

Aaron
  • 2,367
  • 3
  • 25
  • 33
  • 1
    Could it be a timing issue? Try adding a small delay between the write to port C and the read from port A. – Margaret Bloom Jun 14 '16 at 19:42
  • @MargaretBloom Just came back to add that as the answer. I added 1 microsecond delay and that was sufficient. I'm going to dive back into the datasheet right now and see where it gives the pin charge times. If you wanted to add that as a formal answer, I'll gladly upvote + accept it. – Aaron Jun 14 '16 at 20:02
  • Without an official reference on the timings I feel like I can only give an incomplete answer. I believe the rise time depend on the output load but I'm no expert on electronics. If you can find anything in the datasheet post the answer, It'll be useful for future readers! – Margaret Bloom Jun 14 '16 at 20:29

2 Answers2

0

It was a timing issue of not allowing long enough for the value on the pin to reflect what I set it as in the code. The issue was resolved by adding a 1 micro second delay using the delay function _delay_us(1) from the delay.h library.

Aaron
  • 2,367
  • 3
  • 25
  • 33
0

See Any XMEGA Manual > I/O Ports > Overview > General I/O Pin Functionality

Before the IN register there is a synchronizer so the input signal does not instantly appear in the input register. A couple of clock cycles would be enough to wait.