LowPowerLab Forum

Hardware support => Moteino => Topic started by: donaldhwong on September 15, 2015, 03:24:21 PM

Title: Port Reading
Post by: donaldhwong on September 15, 2015, 03:24:21 PM
In trying read digital pin 4, does any of the following code work: ( i can not get it to work).


Many thanks,
Title: Re: Port Reading
Post by: Felix on September 16, 2015, 08:16:08 AM
Googling a solution is sometimes great (https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=avr%20atmega328p%20port%20manipulation):

https://www.arduino.cc/en/Reference/PortManipulation
http://maxembedded.com/2011/06/port-operations-in-avr/
http://tronixstuff.com/2011/10/22/tutorial-arduino-port-manipulation/
Title: Determine timing of two interrupts of AtMega1284p
Post by: donaldhwong on September 16, 2015, 03:56:55 PM
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
Title: Re: Port Reading
Post by: donaldhwong on September 16, 2015, 07:30:29 PM
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.
Title: Re: Port Reading
Post by: Felix on September 17, 2015, 10:44:31 AM
Why would you do port reading and not use digitalRead?
Title: Re: Port Reading
Post by: donaldhwong on September 19, 2015, 12:27:43 PM
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.
Title: Re: Port Reading
Post by: Felix on September 20, 2015, 08:29:10 PM
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.