📄 onewirecontainer20.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 1999,2000 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;import java.util.Vector;import java.util.Enumeration;import com.dalsemi.onewire.*;import com.dalsemi.onewire.utils.*;import com.dalsemi.onewire.adapter.*;import java.io.*;/** *<P>1-Wire® container that encapsulates the functionality of the 1-Wire *family type <b>20</b> (hex), Dallas Semiconductor part number: <B> DS2450, * 1-Wire Quad A/D Converter</B>.</P> * * * <H3>Features</H3> * <UL> * <LI>Four high-impedance inputs * <LI>Programmable input range (2.56V, 5.12V), * resolution (1 to 16 bits) and alarm thresholds * <LI>5V, single supply operation * <LI>Very low power, 2.5 mW active, 25 µW idle * <LI>Unused analog inputs can serve as open * drain digital outputs for closed-loop control * <LI>Operating temperature range from -40°C to * +85°C * </UL> * * <H3>Usage</H3> * * <P>Example device setup</P> * <PRE><CODE> * byte[] state = owd.readDevice(); * owd.setResolution(OneWireContainer20.CHANNELA, 16, state); * owd.setResolution(OneWireContainer20.CHANNELB, 8, state); * owd.setRange(OneWireContainer20.CHANNELA, 5.12, state); * owd.setRange(OneWireContainer20.CHANNELB, 2.56, state); * owd.writeDevice(); * </CODE></PRE> * * <P>Example device read</P> * <PRE><CODE> * owd.doADConvert(OneWireContainer20.CHANNELA, state); * owd.doADConvert(OneWireContainer20.CHANNELB, state); * double chAVolatge = owd.getADVoltage(OneWireContainer20.CHANNELA, state); * double chBVoltage = owd.getADVoltage(OneWireContainer20.CHANNELB, state); * </CODE></PRE> * * <H3>Note</H3> * * <P>When converting analog voltages to digital, the user of the device must * gaurantee that the voltage seen by the channel of the quad A/D does not exceed * the selected input range of the device. If this happens, the device will default * to reading 0 volts. There is NO way to know if the device is reading a higher than * specified voltage or NO voltage.</P> * * <H3> DataSheet </H3> * * <A HREF="http://pdfserv.maxim-ic.com/arpdf/DS2450.pdf"> http://pdfserv.maxim-ic.com/arpdf/DS2450.pdf</A> * * @version 0.00, 28 Aug 2000 * @author JK,DSS */public class OneWireContainer20 extends OneWireContainer implements ADContainer{ //-------- //-------- Static Final Variables //-------- /** Offset of BITMAP in array returned from read state */ public static final int BITMAP_OFFSET = 24; /** Offset of ALARMS in array returned from read state */ public static final int ALARM_OFFSET = 8; /** Offset of external power offset in array returned from read state */ public static final int EXPOWER_OFFSET = 20; /** Channel A number */ public static final int CHANNELA = 0; /** Channel B number */ public static final int CHANNELB = 1; /** Channel C number */ public static final int CHANNELC = 2; /** Channel D number */ public static final int CHANNELD = 3; /** No preset value */ public static final int NO_PRESET = 0; /** Preset value to zeros */ public static final int PRESET_TO_ZEROS = 1; /** Preset value to ones */ public static final int PRESET_TO_ONES = 2; /** Number of channels */ public static final int NUM_CHANNELS = 4; /** DS2450 Convert command */ private static final byte CONVERT_COMMAND = ( byte ) 0x3C; //-------- //-------- Variables //-------- /** * Voltage readout memory bank */ private MemoryBankAD readout; /** * Control/Alarms/calibration memory banks vector */ private Vector regs; //-------- //-------- Constructors //-------- /** * Default constructor */ public OneWireContainer20 () { super(); // initialize the memory banks initMem(); } /** * Creates a container with a provided adapter object * and the address of the 1-Wire device. * * @param sourceAdapter adapter required to communicate with * this device * @param newAddress address of this 1-Wire device */ public OneWireContainer20 (DSPortAdapter sourceAdapter, byte[] newAddress) { super(sourceAdapter, newAddress); // initialize the memory banks initMem(); } /** * Creates a container with a provided adapter object * and the address of the 1-Wire device. * * @param sourceAdapter adapter required to communicate with * this device * @param newAddress address of this 1-Wire device */ public OneWireContainer20 (DSPortAdapter sourceAdapter, long newAddress) { super(sourceAdapter, newAddress); // initialize the memory banks initMem(); } /** * Creates a container with a provided adapter object * and the address of the 1-Wire device. * * @param sourceAdapter adapter required to communicate with * this device * @param newAddress address of this 1-Wire device */ public OneWireContainer20 (DSPortAdapter sourceAdapter, String newAddress) { super(sourceAdapter, newAddress); // initialize the memory banks initMem(); } //-------- //-------- Methods //-------- /** * Gets the name of this 1-Wire device. * * @return representation of this 1-Wire device's name */ public String getName () { return "DS2450"; } /** * Gets any other possible names for this 1-Wire device. * * @return representation of this 1-Wire device's other names */ public String getAlternateNames () { return "1-Wire Quad A/D Converter"; } /** * Gets a brief description of the functionality * of this 1-Wire device. * * @return description of this 1-Wire device's functionality */ public String getDescription () { return "Four high-impedance inputs for measurement of analog " + "voltages. User programable input range. Very low " + "power. Built-in multidrop controller. Channels " + "not used as input can be configured as outputs " + "through the use of open drain digital outputs. " + "Capable of use of Overdrive for fast data transfer. " + "Uses on-chip 16-bit CRC-generator to guarantee good data."; } /** * Gets the maximum speed this 1-Wire device can communicate at. * * @return maximum speed of this One-Wire device */ public int getMaxSpeed () { return DSPortAdapter.SPEED_OVERDRIVE; } /** * Gets an enumeration of memory banks. * * @return enumeration of memory banks * * @see com.dalsemi.onewire.container.MemoryBank * @see com.dalsemi.onewire.container.PagedMemoryBank * @see com.dalsemi.onewire.container.OTPMemoryBank */ public Enumeration getMemoryBanks () { Vector bank_vector = new Vector(4); // readout bank_vector.addElement(readout); // control/alarms/calibration for (int i = 0; i < 3; i++) bank_vector.addElement(regs.elementAt(i)); return bank_vector.elements(); } //-------- //-------- A/D Feature methods //-------- /** * Queries to get the number of channels supported by this A/D. * Channel specific methods will use a channel number specified * by an integer from <CODE>[0 to (getNumberChannels() - 1)]</CODE>. * * @return the number of channels */ public int getNumberADChannels () { return NUM_CHANNELS; } /** * Queries to see if this A/D measuring device has high/low * alarms. * * @return <CODE>true</CODE> if it has high/low trips */ public boolean hasADAlarms () { return true; } /** * Queries to get an array of available ranges for the specified * A/D channel. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * * @return available ranges starting * from the largest range to the smallest range */ public double[] getADRanges (int channel) { double[] ranges = new double [2]; ranges [0] = 5.12; ranges [1] = 2.56; return ranges; } /** * Queries to get an array of available resolutions based * on the specified range on the specified A/D channel. * * @param channel channel in the range * <CODE>[0 to (getNumberChannels() - 1)]</CODE> * @param range specified range * * @return available resolutions */ public double[] getADResolutions (int channel, double range) { double[] res = new double [16]; for (int i = 0; i < 16; i++) res [i] = range / ( double ) (1 << (i + 1)); return res; } /** * Queries to see if this A/D supports doing multiple voltage * conversions at the same time. * * @return <CODE>true</CODE> if can do multi-channel voltage reads */ public boolean canADMultiChannelRead () { return true; } //-------- //-------- A/D IO Methods //-------- /** * Retrieves the entire A/D control/status and alarm pages. * It reads this and verifies the data with the onboard CRC generator. * Use the byte array returned from this method with static * utility methods to extract the status, alarm and other register values. * Appended to the data is 2 bytes that represent a bitmap * of changed bytes. These bytes are used in the <CODE>writeADRegisters()</CODE> * in conjuction with the 'set' methods to only write back the changed * register bytes. * * @return register page contents verified * with onboard CRC * * @throws OneWireIOException Data was not read correctly * @throws OneWireException Could not find part */ public byte[] readDevice () throws OneWireIOException, OneWireException { byte[] read_buf = new byte [27]; MemoryBankAD mb; // read the banks, control/alarm/calibration for (int i = 0; i < 3; i++) { mb = ( MemoryBankAD ) regs.elementAt(i); mb.readPageCRC(0, (i != 0), read_buf, i * 8); } // zero out the bitmap read_buf [24] = 0; read_buf [25] = 0; read_buf [26] = 0; return read_buf; } /** * Writes the bytes in the provided A/D register pages that * have been changed by the 'set' methods. It knows which state has * changed by looking at the bitmap fields appended to the * register data. Any alarm flags will be automatically * cleared. Only VCC powered indicator byte in physical location 0x1C * can be written in the calibration memory bank. * * @param state register pages * * @throws OneWireIOException Data was not written correctly * @throws OneWireException Could not find part */ public void writeDevice (byte[] state) throws OneWireIOException, OneWireException { int start_offset, len, i, bank, index; boolean got_block; MemoryBankAD mb; // Force a clear of the alarm flags for (i = 0; i < 4; i++) { // check if POR or alarm high/low flag present index = i * 2 + 1; if ((state [index] & ( byte ) 0xB0) != 0) { // clear the bits state [index] &= ( byte ) 0x0F; // set to write in bitmap
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -