⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 onewirecontainer30.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
   {      byte[] buffer = new byte [3];      doSpeed();      adapter.reset();      if (adapter.select(address))      {         /* setup the read */         buffer [0] = READ_DATA_COMMAND;         buffer [1] = ( byte ) memAddr;         buffer [2] = ( byte ) 0xFF;         adapter.dataBlock(buffer, 0, 3);         return buffer [2];      }      else         throw new OneWireException("OneWireContainer30-Device not found.");   }   /**    * Reads bytes from the DS2760.  Note that there is no error-checking as    * the DS2760 does not perform a CRC on the data.    * <p>    * Note: This function should only be used when reading the register    * memory of the DS2760. The EEPROM blocks (addresses 32-64) should be    * accessed with writeBlock/readBlock.    *    * @param memAddr the address to read (0-255)    * @param buffer buffer to receive data    * @param start start position within buffer to place data    * @param len length of buffer    *    * @throws OneWireIOException Error reading data    * @throws OneWireException Could not find part    */   public void readBytes (int memAddr, byte[] buffer, int start, int len)      throws OneWireIOException, OneWireException   {      doSpeed();      adapter.reset();      if (adapter.select(address))      {         for (int i = start; i < start + len; i++)            buffer [i] = ( byte ) 0x0ff;         adapter.putByte(READ_DATA_COMMAND);         adapter.putByte(memAddr & 0x0ff);         adapter.dataBlock(buffer, start, len);      }      else         throw new OneWireException("OneWireContainer30-Device not found.");   }   /**    * Writes a register byte to the memory of the DS2760.  Note that the    * DS2760 does not make any use of cyclic redundancy checks (error-checking).    * To ensure error free transmission, double check write operation.    * <p>    * Note: This method should only be used when writing to the register memory    * of the DS2760. The EEPROM blocks (addresses 32-64) require special treatment    * and thus the writeBlock/readBlock functions should be used for those.    *    * @param memAddr  address to write (0-255)    * @param data     data byte to write to memory    *    * @throws OneWireIOException Error writing data    * @throws OneWireException Could not find part    */   public void writeByte (int memAddr, byte data)      throws OneWireIOException, OneWireException   {      byte[] buffer = new byte [3];      doSpeed();      adapter.reset();      if (adapter.select(address))      {         /* first perform the write */         buffer [0] = WRITE_DATA_COMMAND;         buffer [1] = ( byte ) memAddr;         buffer [2] = data;         adapter.dataBlock(buffer, 0, 3);         //don't read it back for verification...some addresses         //have some R-only bits and some RW bits.  let the user         //verify if they want to      }      else         throw new OneWireException("OneWireContainer30-Device not found.");   }   /**    * Reads a 16 byte data block from one of the user EEPROM blocks.    * Note that there is no error-checking as the DS2760 performs    * no CRCs.    *    * @param blockNumber the EEPROM block number to read,    * acceptable parameters are 0 and 1    *    * @return 16 data bytes    *    * @throws OneWireIOException Error reading data    * @throws OneWireException Could not find part    */   public byte[] readEEPROMBlock (int blockNumber)      throws OneWireIOException, OneWireException   {      byte[] buffer = new byte [18];      byte[] result = new byte [16];      // calculate the address (32 and 48 are valid addresses)      byte memAddr = ( byte ) (32 + (blockNumber * 16));      /* check for valid parameters */      if ((blockNumber != 0) & (blockNumber != 1))         throw new IllegalArgumentException(            "OneWireContainer30-Block number " + blockNumber            + " is not a valid EEPROM block.");      /* perform the recall/read and verification */      doSpeed();      adapter.reset();      if (adapter.select(address))      {         /* first recall the memory to shadow ram */         buffer [0] = RECALL_DATA_COMMAND;         buffer [1] = memAddr;         adapter.dataBlock(buffer, 0, 2);         /* now read the shadow ram */         adapter.reset();         adapter.select(address);         buffer [0] = READ_DATA_COMMAND;         // buffer[1] should still hold memAddr         for (int i = 0; i < 16; i++)            buffer [i + 2] = ( byte ) 0xff;         adapter.dataBlock(buffer, 0, 18);         // keep this result         System.arraycopy(buffer, 2, result, 0, 16);         //user can re-read for verification         return result;      }      else         throw new OneWireException("OneWireContainer30-Device not found.");   }   /**    * Writes a 16 byte data block to one of the user blocks. The block may be    * rewritten at any time, except after it is locked with <CODE>lockBlock()</CODE>.    * This method performs error checking by verifying data written.    *    * @param blockNumber block to write,    * acceptable parameters are 0 and 1    * @param data 16 bytes of data to write    *    * @throws OneWireIOException Error writing data    * @throws OneWireException Could not find part    */   public void writeEEPROMBlock (int blockNumber, byte[] data)      throws OneWireIOException, OneWireException   {      byte[] buffer = new byte [18];      // the first block is at address 32 and the second is at address 48      byte memAddr = ( byte ) (32 + (blockNumber * 16));      /* check for valid parameters */      if (data.length < 16)         throw new IllegalArgumentException(            "OneWireContainer30-Data block must consist of 16 bytes.");      if ((blockNumber != 0) && (blockNumber != 1))         throw new IllegalArgumentException(            "OneWireContainer30-Block number " + blockNumber            + " is not a valid EEPROM block.");      // if the EEPROM block is locked throw a OneWireIOException      if (((blockNumber == 0) && (getFlag(EEPROM_REGISTER, EEPROM_BLOCK_0_LOCK_FLAG)))              || ((blockNumber == 1)                  && (getFlag(EEPROM_REGISTER, EEPROM_BLOCK_1_LOCK_FLAG))))         throw new OneWireIOException(            "OneWireContainer30-Cant write data to locked EEPROM block.");      /* perform the write/verification and copy */      doSpeed();      adapter.reset();      if (adapter.select(address))      {         /* first write to shadow rom */         buffer [0] = WRITE_DATA_COMMAND;         buffer [1] = memAddr;         for (int i = 0; i < 16; i++)            buffer [i + 2] = data [i];         adapter.dataBlock(buffer, 0, 18);         /* read the shadow ram back for verification */         adapter.reset();         adapter.select(address);         buffer [0] = READ_DATA_COMMAND;         // buffer[1] should still hold memAddr         for (int i = 0; i < 16; i++)            buffer [i + 2] = ( byte ) 0xff;         adapter.dataBlock(buffer, 0, 18);         // verify data         for (int i = 0; i < 16; i++)            if (buffer [i + 2] != data [i])               throw new OneWireIOException(                  "OneWireContainer30-Error writing EEPROM block"                  + blockNumber + ".");         /* now perform the copy to EEPROM */         adapter.reset();         adapter.select(address);         buffer [0] = COPY_DATA_COMMAND;         // buffer[1] should still hold memAddr         adapter.dataBlock(buffer, 0, 2);      }      else         throw new OneWireException("OneWireContainer30-Device not found.");   }   /**    * Permanently write-protects one of the user blocks of EEPROM.    *    * @param blockNumber block number to permanently write protect,    * acceptable parameters are 0 and 1.    *    * @throws OneWireIOException Error locking block    * @throws OneWireException Could not find part    */   public void lockBlock (int blockNumber)      throws OneWireIOException, OneWireException   {      // compute the byte location      byte memAddr = ( byte ) (32 + (blockNumber * 16));      /* check if the block is valid */      if ((blockNumber != 0) & (blockNumber != 1))         throw new IllegalArgumentException(            "OneWireContainer30-Block " + blockNumber            + " is not a valid EEPROM block.");      /* perform the lock */      doSpeed();      adapter.reset();      if (adapter.select(address))      {         adapter.putByte(LOCK_COMMAND);         adapter.putByte(memAddr);      }      else         throw new OneWireException("OneWireContainer30-Device not found.");   }   /**    * Checks the specified flag in the specified register.  <BR>    * Valid registers are:    * <A HREF="#PROTECTION_REGISTER"><CODE>PROTECTION_REGISTER</CODE></A>,    * <A HREF="#STATUS_REGISTER"><CODE>STATUS_REGISTER</CODE></A>,    * <A HREF="#EEPROM_REGISTER"><CODE>EEPROM_REGISTER</CODE></A> and    * <A HREF="#SPECIAL_FEATURE_REGISTER"><CODE>SPECIAL_FEATURE_REGISTER</CODE></A>.    *    * @param memAddr registers address. Pre-defined    * fields for each register are defined above.    * @param flagToGet bitmask of desired flag, the acceptable parameters    * pertaining to each register are defined as constant fields above    *    * @return value of the flag: <CODE>true</CODE> if flag is set (=1)    *    * @throws OneWireIOException Error reading data    * @throws OneWireException Could not find part    *    */   public boolean getFlag (int memAddr, byte flagToGet)      throws OneWireIOException, OneWireException   {      // read the byte and perform a simple mask to determine if that byte is on      byte data = readByte(memAddr);      if ((data & flagToGet) != 0)         return true;      return false;   }   /**    * Sets one of the flags in one of the registers.<BR>    * Valid registers are:    * <A HREF="#PROTECTION_REGISTER"><CODE>PROTECTION_REGISTER</CODE></A>,    * <A HREF="#STATUS_REGISTER"><CODE>STATUS_REGISTER</CODE></A>,    * <A HREF="#EEPROM_REGISTER"><CODE>EEPROM_REGISTER</CODE></A> and    * <A HREF="#SPECIAL_FEATURE_REGISTER"><CODE>SPECIAL_FEATURE_REGISTER</CODE></A>.    *    * @param memAddr register address, these addresses are    * predefined above    * @param flagToSet bitmask of flag to set, valid    * parameters pertaining to each register are defined    * as constant fields above    * @param flagValue value to set the flag to    *    * @throws OneWireIOException Error setting flag    * @throws OneWireException Could not find part    */   public void setFlag (int memAddr, byte flagToSet, boolean flagValue)      throws OneWireIOException, OneWireException   {      // the desired default value for the status register flags has to be      // set in a seperate register for some reason, so I treat it specially.      if (memAddr == STATUS_REGISTER)         memAddr = 49;      byte data = readByte(memAddr);      if (flagValue)         data = ( byte ) (data | flagToSet);      else         data = ( byte ) (data & ~(flagToSet));      writeByte(memAddr, data);   }   /**    * Gets the instantaneous current.    *    *    * @param state device state    * @return current in Amperes    *    * @throws OneWireIOException Error reading data    * @throws OneWireException Could not find part    */   public double getCurrent (byte[] state)      throws OneWireIOException, OneWireException   {      // grab the data      int data = ((state [14] << 8) | (state [15] & 0x00ff));      data = data >> 3;      double result;      // when the internal resistor is used, the device calculates it for you      // the resolution is .625 mA      if (internalResistor)         result = (data * .625) / 1000;         // otherwise convert to Amperes      else         result = data * .000015625 / Rsens;      return result;   }   /**    * Allows user to set the remaining capacity. Good for accurate capacity    * measurements using temperature and battery life.    * <p> By measuring the battery's current and voltage when it is fully    * charged and when it is empty, the voltage corresponding to an empty battery    * and the current corresponding to a full one can be derived. These    * values can be detected in user program and the remaining capacity can    * be set to the empty/full levels accordingly for nice accuracy.    *    * @param remainingCapacity remaining capacity in mAH    *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -