New readTemp() routine

Started by john k2ox, October 06, 2013, 02:28:02 PM

john k2ox

Here is the latest readTemp function.

I have had it run for once a second for the last two days non-stop without an error. 

When run from

        if (radio.receiveDone())
         {
           temp = radio.readTemp(TEMPADJ);
           ...
          }


it executes fastest because the radio is already in OPMODE_STANDBY mode.  Used this way the temp is only read after receiving a valid packet.

If it is run outside this(say in the main loop), the radio is in OPMODE_RECEIVER and the routine will switch the radio into standby and back.  Switching modes adds to the execution time.  Run this way there is the chance that a packet will be missed if it was sent while the radio was switched to STANDBY.  In the case of a collision the routine returns the correct temp and the radio is ready to receive the next packet.  Finally! 

My plan is to use this to correct the freq drift vs. temperature change.  At most I will call tempRead() once every three minutes and handle any collisions.

Please test it and let me know if you run into any issues.  If you can improve the routine let us know.

int RFM69NEW::readTemp(int calFactor)  //je  returns centigrade
 
 /*  This code is very stable it has been run over temp of 20-80C.
     If changes are made to this routine, they should be tested for several hours.
	 Some sections make no sense to me. The while '0', while '1', while '0' is
	 absolutely needed. It would seem the code would get stuck at the last while '0'.
	 The delayMicroseconds(0) must be in place. Deleting the statement or increaseing
	 the delay freezes the code. 
 */
 {
  int diff;
  int del = 0;
  byte r1, r2, msb, mid, lsb;
  byte modeWas = _mode;
  

  
  if (_mode != RF69_MODE_STANDBY)  //temp is fastest when entered in standby (i.e. radio.receiveDone())
	{
    msb = readReg(REG_FRFMSB);     //save user's freq
    mid = readReg(REG_FRFMID);
    lsb = readReg(REG_FRFLSB);
	
	writeReg(REG_FRFMSB, 0xFA);		// set to 1 GHz, this is the most reliable way
	writeReg(REG_FRFMID, 0x00);		// to avoid an asynch rx collision. 
	writeReg(REG_FRFLSB, 0x00);
		
	setMode(RF69_MODE_STANDBY);
      while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0);
      while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 1);
      while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0);
    }
   
  do
  { //Serial.println("r1");
      delayMicroseconds(del); 
	  writeReg(REG_TEMP1, RF_TEMP1_MEAS_START);   					//trig temp reading
	  while ((readReg(REG_TEMP1) & 0x04) == 0);
	  while ((readReg(REG_TEMP1) & 0x04) == 1);   //ready?
	  while ((readReg(REG_TEMP1) & 0x04) == 0);
	  r1 = readReg(REG_TEMP2);
	 // Serial.println("r2"); 
	  delayMicroseconds(del);
	  writeReg(REG_TEMP1, RF_TEMP1_MEAS_START);   					//trig temp reading
	  while ((readReg(REG_TEMP1) & 0x04) == 0);
	  while ((readReg(REG_TEMP1) & 0x04) == 1);   //ready?
	  while ((readReg(REG_TEMP1) & 0x04) == 0);
	  r2 = readReg(REG_TEMP2);
	  
	  diff = abs(r2 - r1);
	//Serial.println("out");
	 
	//  if( diff > 1)
	//	Serial.println("yuc");
  }
  while(diff > 1);
  		
  delayMicroseconds(del);
  setMode(modeWas);      
  
  if (modeWas != RF69_MODE_STANDBY)  //temp is fastest when entered in standby (i.e. radio.receiveDone())
	{
	writeReg(REG_FRFMSB, msb);                    //restore freq
	writeReg(REG_FRFMID, mid);
	writeReg(REG_FRFLSB, lsb);
	}
  
   return COURSE_TEMP_COEF + calFactor - r2;       //get it.     
 }


The above code needs to be placed in a library.

Consider this code experimental at this time!!!

john