Main Menu

Port Reading

Started by donaldhwong, September 15, 2015, 03:24:21 PM

donaldhwong

In trying read digital pin 4, does any of the following code work: ( i can not get it to work).


  • PIND & (1<<PD4)
  • PORTD & (1<<PD4)
  • PIND & 0x04

Many thanks,


donaldhwong

#2
Thank you Felix for your usual prompt reply.  I am so happy that I have embarked on your platform from the beginning.  I am really having fun. 

What I wanted to do is to measure the difference of the reception of two RFM69 via the two interrupts.  My code is as follows:
long getDifference() {
  long difference = 0, distance = 0;
  long timeout =0;
  boolean finished = false;
  boolean oneFire = false;
  boolean twoFire = false;
  long setPoint1 = 0;
  long setPoint2 = 0;

  //delay(5);
  
  while ((oneFire == 0) || (twoFire == 0)) {
    if (((PINB & BIT(2)) == 0) && !oneFire) { //MoteinoMEGA attached RFM69
      oneFire = true;
      setPoint1 = timeout;
    }
    if (((PIND & BIT(3)) == 0) && !twoFire) { // second RFM69
      twoFire = true;
      setPoint2 = timeout;
    }
    delayMicroseconds(1);
    timeout++;
    if (timeout > 100000)
      break;
  }

  difference = setPoint1 - setPoint2;
  distance = (setPoint1 + setPoint2) / 10;

  return (difference);
}

Unfortunately, it does not work.  Can anyone help?

Thank you,
Donald

donaldhwong

BTW,  the port reading code works with ultrasonic module.  It does not work with RFM69 interrupt.  I suspect that I could have initiated at the wrong time.  I initiate the code right after I get a RFM available.  Perhaps it was too soon, or too late.

Please advise.

Felix

Why would you do port reading and not use digitalRead?

donaldhwong

Hi Felix,

The DigitalRead is too slow to capture the time of flight of RFM69.

Even with port reading, I am not sure that I can capture the difference in time for two RFM69 reception.

Felix

Time of flight? :)
Look, just flip a digital IO with digitalWrite(HIGH/LOW), before and after transmission is done (ie after the interrupts are fired). Actually the interrupt will fire D2 so you can also watch that.
If you hook up a logic analyzer and watch your IO flagging the start and end, that's a very good approximation of when the packet has been fully modulated into the receiver.
Also if you watch the SPI lines and D2 on the receiver you will clearly see the packet being received into the receiver.