📄 onewirecontainer21.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 1999 - 2001 Dallas Semiconductor Corporation, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Dallas Semiconductor * shall not be used except as stated in the Dallas Semiconductor * Branding Policy. *--------------------------------------------------------------------------- */package com.dalsemi.onewire.container;// importsimport com.dalsemi.onewire.utils.CRC16;import com.dalsemi.onewire.utils.Convert;import com.dalsemi.onewire.*;import com.dalsemi.onewire.adapter.*;import java.util.*;/** * <P> 1-Wire® container for a Thermochron iButton, DS1921. * This container encapsulates the functionality of the 1-Wire family type <B>21</B> (hex). * </P> * * <H3> Features </H3> * <UL> * <LI> Logs up to 2048 consecutive temperature measurements in nonvolatile, read-only memory * <li> Real-Time clock with programmable alarm * <LI> Programmable high and low temperature alarms * <li> Alarm violation times and durations recorded in nonvolatile, read-only memory * <li> Automatically 'wakes up' and logs temperature at user-programmable intervals * <li> 4096 bits of general-purpose read/write nonvolatile memory * <li> 256-bit scratchpad ensures integrity of data transfer * <li> On-chip 16-bit CRC generator to verify read operations * <li> Long-term histogram with 2° C resolution * </UL> * * <H3> Memory </H3> * * <P> The memory can be accessed through the objects that are returned * from the {@link #getMemoryBanks() getMemoryBanks} method. </P> * * The following is a list of the MemoryBank instances that are returned: * * <UL> * <LI> <B> Scratchpad with CRC </B> * <UL> * <LI> <I> Implements </I> {@link com.dalsemi.onewire.container.MemoryBank MemoryBank}, * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * <LI> <I> Size </I> 32 starting at physical address 0 * <LI> <I> Features</I> Read/Write not-general-purpose volatile * <LI> <I> Pages</I> 1 page of length 32 bytes * <LI> <I> Page Features </I> page-device-CRC * <li> <i> Extra information for each page</i> Target address, offset, length 3 * </UL> * <LI> <B> Main Memory </B> * <UL> * <LI> <I> Implements </I> {@link com.dalsemi.onewire.container.MemoryBank MemoryBank}, * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * <LI> <I> Size </I> 512 starting at physical address 0 * <LI> <I> Features</I> Read/Write general-purpose non-volatile * <LI> <I> Pages</I> 16 pages of length 32 bytes giving 29 bytes Packet data payload * <LI> <I> Page Features </I> page-device-CRC * </UL> * <LI> <B> Register control </B> * <UL> * <LI> <I> Implements </I> {@link com.dalsemi.onewire.container.MemoryBank MemoryBank}, * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * <LI> <I> Size </I> 32 starting at physical address 512 * <LI> <I> Features</I> Read/Write not-general-purpose non-volatile * <LI> <I> Pages</I> 1 pages of length 32 bytes * <LI> <I> Page Features </I> page-device-CRC * </UL> * <LI> <B> Alarm time stamps </B> * <UL> * <LI> <I> Implements </I> {@link com.dalsemi.onewire.container.MemoryBank MemoryBank}, * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * <LI> <I> Size </I> 96 starting at physical address 544 * <LI> <I> Features</I> Read-only not-general-purpose non-volatile * <LI> <I> Pages</I> 3 pages of length 32 bytes * <LI> <I> Page Features </I> page-device-CRC * </UL> * <LI> <B> Temperature histogram </B> * <UL> * <LI> <I> Implements </I> {@link com.dalsemi.onewire.container.MemoryBank MemoryBank}, * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * <LI> <I> Size </I> 128 starting at physical address 2048 * <LI> <I> Features</I> Read-only not-general-purpose non-volatile * <LI> <I> Pages</I> 4 pages of length 32 bytes * <LI> <I> Page Features </I> page-device-CRC * </UL> * <LI> <B> Temperature log </B> * <UL> * <LI> <I> Implements </I> {@link com.dalsemi.onewire.container.MemoryBank MemoryBank}, * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * <LI> <I> Size </I> 2048 starting at physical address 4096 * <LI> <I> Features</I> Read-only not-general-purpose non-volatile * <LI> <I> Pages</I> 64 pages of length 32 bytes * <LI> <I> Page Features </I> page-device-CRC * </UL> * </UL> * * <H3> Usage </H3> * * <p>The code below starts a mission with the following characteristics: * <ul> * <li>Rollover flag enabled. This means if more than 2048 samples are * taken, the newer samples overwrite the oldest samples in the temperature * log.</li> * <li>High alarm of 28.0° and a low alarm of 23.0° C. If the alarm is violated, * the Temperature Alarm log will record when and for how long the violation occurred.</li> * <li>The clock alarm enabled to Mondays at 12:30:45 pm.</li> * <li>Sets the Thermocron's Real-Time Clock to the host system's clock.</li> * <li>The mission will start in 2 minutes.</li> * <li>A sample rate of 1 minute.</li> * </ul> * This code also ensures that the Thermocron's clock is set to run, and that the * clock alarm is enabled.</p> * <pre><code> * // "ID" is a byte array of size 8 with an address of a part we * // have already found with family code 12 hex * // "access" is a DSPortAdapter * OneWireContainer21 ds1921 = (OneWireContainer21) access.getDeviceContainer(ID); * ds1921.setupContainer(access,ID); * // ds1921 previously setup as a OneWireContainer21 * ds1921.clearMemory(); * // read the current state of the device * byte[] state = ds1921.readDevice(); * // enable rollover * ds1921.setFlag(ds1921.CONTROL_REGISTER, ds1921.ROLLOVER_ENABLE_FLAG, true, state); * // set the high temperature alarm to 28 C * ds1921.setTemperatureAlarm(ds1921.ALARM_HIGH, 28.0, state); * // set the low temperature alarm to 23 C * ds1921.setTemperatureAlarm(ds1921.ALARM_LOW, 23.0, state); * // set the clock alarm to occur weekly, Mondays at 12:30:45 pm * ds1921.setClockAlarm(12, 30, 45, 2, ds1921.ONCE_PER_WEEK, state); * // set the real time clock to the system's current clock * ds1921.setClock(System.currentTimeMillis(), state); * // set the mission to start in 2 minutes * ds1921.setMissionStartDelay(2,state); * // make sure the clock is set to run * ds1921.setClockRunEnable(true, state); * // make sure the clock alarm is enabled * ds1921.setClockAlarmEnable(true, state); * // write all that information out * ds1921.writeDevice(state); * // now enable the mission with a sample rate of 1 minute * ds1921.enableMission(1); * </code></pre> * * <p>The following code processes the temperature log:</p> * <code><pre> * byte[] state = ds1921.readDevice(); * byte[] log = ds1921.getTemperatureLog(state); * Calendar time_stamp = ds1921.getMissionTimeStamp(state); * long time = time_stamp.getTime().getTime() + ds1921.getFirstLogOffset(state); * int sample_rate = ds1921.getSampleRate(state); * * System.out.println("TEMPERATURE LOG"); * * for (int i=0;i < log.length;i++) * { * System.out.println("- Temperature recorded at : "+(new Date(time))); * System.out.println("- was : "+ds1921.decodeTemperature(log[i])+" C"); * time += sample_rate * 60 * 1000; * } * </pre></code> * * <p>The following code processes the alarm histories:</p> * <code><pre> * byte[] high_history = ds1921.getAlarmHistory(ds1921.TEMPERATURE_HIGH_ALARM); * byte[] low_history = ds1921.getAlarmHistory(ds1921.TEMPERATURE_LOW_ALARM); * int sample_rate = ds1921.getSampleRate(state); * int start_offset, violation_count; * System.out.println("ALARM HISTORY"); * if (low_history.length==0) * { * System.out.println("- No violations against the low temperature alarm."); * System.out.println("-"); * } * for (int i=0;i < low_history.length/4; i++) * { * start_offset = (low_history [i * 4] & 0x0ff) * | ((low_history [i * 4 + 1] << 8) & 0x0ff00) * | ((low_history [i * 4 + 2] << 16) & 0x0ff0000); * violation_count = 0x0ff & low_history[i*4+3]; * System.out.println("- Low alarm started at : "+(start_offset * sample_rate)); * System.out.println("- : Lasted "+(violation_count * sample_rate)+" minutes"); * } * if (high_history.length==0) * { * System.out.println("- No violations against the high temperature alarm."); * System.out.println("-"); * } * for (int i=0;i < high_history.length/4; i++) * { * start_offset = (high_history [i * 4] & 0x0ff) * | ((high_history [i * 4 + 1] << 8) & 0x0ff00) * | ((high_history [i * 4 + 2] << 16) & 0x0ff0000); * violation_count = 0x0ff & high_history[i*4+3]; * System.out.println("- High alarm started at : "+(start_offset * sample_rate)); * System.out.println("- : Lasted "+(violation_count * sample_rate)+" minutes"); * } * </pre></code> * * <p>The following code processes the temperature histogram:</p> * <code><pre> * double resolution = ds1921.getTemperatureResolution(); * double histBinWidth = ds1921.getHistogramBinWidth(); * double start = ds1921.getHistogramLowTemperature(); * System.out.println("TEMPERATURE HISTOGRAM"); * for (int i=0;i < histogram.length;i++) * { * System.out.println("- Histogram entry : " * + histogram [i] + " at temperature " * + start + " to " * + ( start + (histBinWidth - resolution)) + " C"); * start += histBinWidth; * } * </pre></code> * * <p>Also see the usage examples in the {@link com.dalsemi.onewire.container.TemperatureContainer TemperatureContainer} * and {@link com.dalsemi.onewire.container.ClockContainer ClockContainer} * interfaces.</p> * * For examples regarding memory operations, * <uL> * <li> See the usage example in * {@link com.dalsemi.onewire.container.OneWireContainer OneWireContainer} * to enumerate the MemoryBanks. * <li> See the usage examples in * {@link com.dalsemi.onewire.container.MemoryBank MemoryBank} and * {@link com.dalsemi.onewire.container.PagedMemoryBank PagedMemoryBank} * for bank specific operations. * </uL> * * <H3> DataSheet </H3> * <DL> * <DD><A HREF="http://pdfserv.maxim-ic.com/arpdf/DS1921L-F5X.pdf">http://pdfserv.maxim-ic.com/arpdf/DS1921L-F5X.pdf</A> * </DL> * * Also visit <a href="http://www.ibutton.com/ibuttons/thermochron.html"> * http://www.ibutton.com/ibuttons/thermochron.html</a> for links to more * sources on the DS1921 Thermocron. * * @see com.dalsemi.onewire.container.OneWireSensor * @see com.dalsemi.onewire.container.SwitchContainer * @see com.dalsemi.onewire.container.TemperatureContainer * * @version 0.00, 28 Aug 2000 * @author COlmstea, KLA * */public class OneWireContainer21 extends OneWireContainer implements TemperatureContainer, ClockContainer{ private final byte FAMILY_CODE = ( byte ) 0x21; private boolean doSpeedEnable = true; /* privates! * Memory commands. */ private final byte WRITE_SCRATCHPAD_COMMAND = ( byte ) 0x0F; private final byte READ_SCRATCHPAD_COMMAND = ( byte ) 0xAA; private final byte COPY_SCRATCHPAD_COMMAND = ( byte ) 0x55; private final byte READ_MEMORY_CRC_COMMAND = ( byte ) 0xA5; private final byte CLEAR_MEMORY_COMMAND = ( byte ) 0x3C; private final byte CONVERT_TEMPERATURE_COMMAND = ( byte ) 0x44; // Scratchpad access memory bank private MemoryBankScratchCRC scratch; // Register control memory bank private MemoryBankNVCRC register; // Alarms memory bank private MemoryBankNVCRC alarm; // Histogram memory bank private MemoryBankNVCRC histogram; // Log memory bank private MemoryBankNVCRC log; // Buffer to hold the temperature log in private byte[] read_log_buffer = new byte [64 * 32]; //64 pages X 32 bytes per page // should we update the Real time clock? private boolean updatertc = false; // Maxim/Dallas Semiconductor Part number private String partNumber = "DS1921"; // Temperature range low temperaturein degrees Celsius // calculated through 12-bit field of 1-Wire Net Address private double temperatureRangeLow = -40.0; private double temperatureRangeHigh = 85.0; // Temperature range width in degrees Celsius // calculated through 12-bit field of 1-Wire Net Address //private double temperatureRangeWidth = 125.0; // Temperature resolution in degrees Celsius // calculated through 12-bit field of 1-Wire Net Address private double temperatureResolution = 0.5; // The temperature range at which the device will operate. private double temperatureOperatingRangeLow = -40.0; private double temperatureOperatingRangeHigh = 85.0; // Is this 1-Wire device a DS1921HZ? private boolean isDS1921HZ = false; ///////////////////////////////////////////// // //PUBLIC's // ///////////////////////////////////////////// /** * Address of the status register. Used with the <code>getFlag</code> * and <code>setFlag</code> methods to set and * check flags indicating the Thermochron's status. * * @see #getFlag(int,byte,byte[]) * @see #getFlag(int,byte) * @see #setFlag(int,byte,boolean) * @see #setFlag(int,byte,boolean,byte[]) */ public static final int STATUS_REGISTER = 0x214;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -