1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
18 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
22 // Except as contained in this notice, the name of Dallas Semiconductor
23 // shall not be used except as stated in the Dallas Semiconductor
25 //---------------------------------------------------------------------------
27 // cnt1D.c - Module to read the DS2423 - counter.
35 //----------------------------------------------------------------------
36 // Read the counter on a specified page of a DS2423.
38 // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
39 // indicate the symbolic port number.
40 // 'SerialNum' - Serial Number of DS2423 that contains the counter
42 // 'CounterPage' - page number that the counter is associated with
43 // 'Count' - pointer to variable where that count will be returned
45 // Returns: TRUE(1) counter has been read and verified
46 // FALSE(0) could not read the counter, perhaps device is not
49 int ReadCounter(int portnum, uchar SerialNum[8], int CounterPage,
54 int send_cnt=0, address, i;
59 // set the device serial number to the counter device
60 owSerialNum(portnum,SerialNum,FALSE);
63 if (owAccess(portnum))
65 // create a block to send that reads the counter
66 // read memory and counter command
67 send_block[send_cnt++] = 0xA5;
68 docrc16(portnum,0xA5);
69 // address of last data byte before counter
70 address = (CounterPage << 5) + 31; // (1.02)
71 send_block[send_cnt++] = (uchar)(address & 0xFF);
72 docrc16(portnum,(ushort)(address & 0xFF));
73 send_block[send_cnt++] = (uchar)(address >> 8);
74 docrc16(portnum,(ushort)(address >> 8));
75 // now add the read bytes for data byte,counter,zero bits, crc16
76 for (i = 0; i < 11; i++)
77 send_block[send_cnt++] = 0xFF;
80 if (owBlock(portnum,FALSE,send_block,send_cnt))
82 // perform the CRC16 on the last 11 bytes of packet
83 for (i = send_cnt - 11; i < send_cnt; i++)
84 lastcrc16 = docrc16(portnum,send_block[i]);
86 // verify CRC16 is correct
87 if (lastcrc16 == 0xB001)
91 // extract the counter value
93 for (i = send_cnt - 7; i >= send_cnt - 10; i--)
96 *Count |= send_block[i];
102 // return the result flag rt