Abstract: DS2482 is a bridge device from I²C to 1-Wire. DS2482 can make any host with I²C communication function generate correct timing and 1-Wire waveform with slew rate control. This application note is a user's guide for the DS2482 I²C to 1-Wire line driver, which details the communication process for the operation of a general-purpose 1-Wire master controller.
Introduction The 1-Wire network includes a master and one or more slave devices. The 1-Wire master can be composed of an I / O pin of the microprocessor and manually generate timing pulses. The DS2482 I²C to 1-Wire network bridge device can generate detailed 1-Wire communication timing without the need for engineers to participate in the design. Figure 1 shows a simplified block diagram of the DS2482 configuration. This article introduces an effective method for implementing application program interface (API) using DS2482, supporting basic and extended 1-Wire operations. The I²C communication corresponding to 1-Wire operation is described in detail. These operations lay a comprehensive foundation for performing all functions of current and future 1-Wire devices, except for device programming based on DS250x series EPROM. This outline of 1-Wire operation is suitable for 1-Wire applications that do not rely on a 1-Wire master.
This article is only a supplement to the DS2482 data sheet, and cannot replace the data sheet. The DS2482 is available in two configurations, a single-channel 1-Wire master controller (DS2482-100) and a 1-Wire master controller with low-power sleep mode (DS2482-101), and an eight-channel 1-Wire master controller (DS2482-800).
Figure 1. Functional diagram of the DS2482 bridge that implements I²C and 1-Wire network communication
The 1-Wire interface below gives a few basic 1-Wire functions, called original functions, that is, the functions that must be available in the application in order to perform all 1-Wire operations. The first function (OWReset) is to reset all 1-Wire slave devices on the network in preparation for receiving commands from the 1-Wire master controller. The second function (OWWriteBit) completes the operation of the 1-Wire master writing a bit to the slave device, and the third function (OWReadBit) completes the operation of reading a bit from the 1-Wire slave device. Since all 1-Wire bit communications must be initiated by the 1-Wire master, "reading" is actually the result of sampling after "writing" a bit. Almost all other 1-Wire operations can be composed of these three operations. For example, writing a byte to a 1-Wire network is equivalent to writing one bit 8 times.
The 1-Wire search algorithm can also be constructed using the same three original functions. With the three 1-Wire commands, the DS2482 can perform search functions, greatly reducing the amount of communication required for search operations. Similarly, 1-byte 1-Wire communication commands are more efficient than eight bit-by-bit operations.
Table 1 shows three basic primitive functions (OWReset, OWWriteBit / OWReadBit and OWWriteByte / OWReadByte) and three other very useful functions (OWBlock, OWSearch and msDelay), which constitute a series of main basic 1-Wire operations. These operation names will be used below.
Table 1. Basic 1-Wire operation
Many 1-Wire slave devices can operate at two different communication rates: standard rate and high-speed mode. All devices support standard rate mode. The high-speed rate is about 10 times the standard rate. DS2482 supports both of these 1-Wire rates.
1-Wire devices usually draw some or all of the operating power from the 1-Wire bus. However, some devices require additional power for certain operations of the protocol. For example, a device may need to perform temperature conversion or perform a SHA-1 hash algorithm. Power for this operation is provided by enabling a strong pull-up on the 1-Wire bus. In this power supply mode, normal communication cannot be performed. The DS2482 is powered by setting the strong pull-up flag (SPU) bit, which will initiate a strong pull-up after the next byte / bit communication of 1-Wire is complete. The DS2482-100 and DS2482-101 also have an external pin (PCTLZ) that can control high current strong pull-ups.
Table 2 lists the extended 1-Wire operation for 1-Wire rate setting, power supply, and programming pulses.
Table 2. Extended 1-Wire operation
Host Configuration The DS2482 host should have an I²C communication port. This article does not describe the configuration of the host. However, the host must provide standard I²C interface operation. It should be noted that the host interface has advanced functions that include some I²C interface operations. Refer to Table 3 for required operations.
Table 3. Required I²C host operations
Before configuring DS2482 to attempt 1-Wire operation, the host must set up and synchronize the I²C to 1-Wire line driver DS2482. To communicate with the DS2482, the host must know its slave address. Figure 2 shows the slave addresses of DS2482-100, DS2482-101, and DS2482-800.
Figure 2. I²C slave address of DS2482
DS2482's I²C command The following symbol descriptions come from DS2482 data materials, which are represented by abbreviated symbols and are used to explain the I²C communication process of the device. Next, we will repeat these communication processes and provide additional comments and C language routines for basic and extended 1-Wire operations.
I²C communication process-symbol description
Data direction notation
The data direction representation method in many figures in this article indicates whether the communication direction is master to slave (grey) or slave to master (white).
DS2482 configuration operation The following operations are used to set up and configure the DS2482. Some operations require calling 1-Wire operation subroutines.
The C program shown in DS2482 detection example 1 provides the detection and configuration process. The default values ​​written to DS2482 include 1-Wire rate (standard), strong pull-up (shutdown), online pulse mask (shutdown), and active pull-up through). This state is maintained as a general variable and can be restored if the device needs to be reset to this default state. Different default values ​​can be selected for different applications.
// DS2482 state unsigned char I2C_address; int c1WS, cSPU, cPPM, cAPU; int short_detected; // ---------------------------- ---------------------------------------------- // DS2428 Detect routine that sets the I2C address and then performs a // device reset followed by writing the configuration byte to default values: // 1-Wire speed (c1WS) = standard (0) // Strong pullup (cSPU) = off (0) // Presence pulse masking (cPPM) = off (0) // Active pullup (cAPU) = on (CONFIG_APU = 0x01) // // Returns: TRUE if device was detected and written // FALSE device not detected or failure to write configuration byte // int DS2482_detect (unsigned char addr) {// set global address I2C_address = addr; // reset the DS2482 ON selected address if (! DS2482_reset ()) return FALSE; // default configuration c1WS = FALSE; cSPU = FALSE ; cPPM = FALSE; cAPU = CONFIG_APU; // write the default configuration setup if (! DS2482_write_config (c1WS | cSPU | cPPM | cAPU)) return FALSE; return TRUE;} Example 1. DS2482 test
DS2482 device reset Figure 3 is the flow of DS2482 device reset I²C communication. Example 2 shows the C program of the DS2482 reset command, which can completely reset the device state machine logic and terminate all ongoing 1-Wire communications. The device reset command code is F0h.
Figure 3. Device reset after power on. This example includes optional read access to verify that the command was successfully executed.
// ------------------------------------------------ -------------------------- // Perform a device reset on the DS2482 // // Returns: TRUE if device was reset // FALSE device not detected or failure to perform reset // int DS2482_reset () {unsigned char status; // Device Reset // S AD, 0 [A] DRST [A] Sr AD, 1 [A] [SS] A \ P // [] indicates from slave // ​​SS status byte to read to verify state I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_DRST, EXPECT_ACK); I2C_rep_start (); I2C_write (I2C_address | I2C_READ, EXPECT_ACK) I2C_read (NACK); I2C_stop (); // check for failure due to incorrect read back of status return ((status & 0xF7) == 0x10);} Example 2. Reset device code
DS2482 write configuration Figure 4 shows the I²C communication routine for the DS2482 write configuration; Example 3 shows the C program that implements the DS2482 write configuration command timing. The write configuration command code is D2h.
Figure 4. The write configuration register, which includes an optional read operation to verify the successful execution of the command.
// ------------------------------------------------ -------------------------- // Write the configuration register in the DS2482. The configuration // options are provided in the lower nibble of the provided config byte. // The uppper nibble in bitwise inverted when written to the DS2482. // // Returns: TRUE: config written and response correct // FALSE: response incorrect // int DS2482_write_config (unsigned char config) {unsigned char read_config; // Write configuration (Case A) // S AD, 0 [A] WCFG [A] CF [A] Sr AD, 1 [A] [CF] A \ P // [] indicates from slave // ​​CF configuration byte to write I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_WCFG, EXPECT_ACK); I2C_write (config | (~ config << 4), EXPECT_ACK); I2C_rep_start (); I2C_write (I2C_address | 2 | read_config = I2C_read (NACK); I2C_stop (); // check for failure due to incorrect read back if (config! = read_config) {// handle error // ... DS2482_reset (); return FALSE;} return TRUE;} Example 3. DS2 482 write configuration
DS2482 channel selection Figure 5 shows an example of I²C communication that implements DS2482-800 channel selection. The effective channels are 0 to 7. Note that this operation is not suitable for DS2482-100 or DS2482-101. Example 4 shows the C program that executes the DS2482-800 channel selection command. The channel selection command code is C3h. After selecting the channel, the selected channel can perform all 1-Wire operations.
Figure 5. Write channel selection register. This example includes an optional read operation to verify the successful execution of the command.
// ------------------------------------------------ -------------------------- // Select the 1-Wire channel on a DS2482-800. // // Returns: TRUE if channel selected // FALSE device not detected or failure to perform select // int DS2482_channel_select (int channel) {unsigned char ch, ch_read, check; // Channel Select (Case A) // S AD, 0 [A] CHSL [A] CC [A] Sr AD, 1 [A] [RR] A \ P // [] indicates from slave // ​​CC channel value // RR channel read back I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_CHSL , EXPECT_ACK); switch (channel) {default: case 0: ch = 0xF0; ch_read = 0xB8; break; case 1: ch = 0xE1; ch_read = 0xB1; break; case 2: ch = 0xD2; ch_read = 0xAA; break; case 3: ch = 0xC3; ch_read = 0xA3; break; case 4: ch = 0xB4; ch_read = 0x9C; break; case 5: ch = 0xA5; ch_read = 0x95; break; case 6: ch = 0x96; ch_read = 0x8E; break; case 7: ch = 0x87; ch_read = 0x87; break;}; I2C_write (ch, EXPECT_ACK); I2C_rep_start (); I2C_write (I2C_address | I2C_READ, EXPECT_ACK); check = I2C_read (NACK); I2C_stop (); // check for failure due to incorrect read back of channel return (check == ch_read);} Example 4. DS2482-800 channel selection
The DS2482's 1-Wire operation OWReset1-Wire Reset command (B4h) performs 1-Wire reset and 1-Wire device online response pulse detection on the 1-Wire bus. The status of the 1-Wire bus can be sampled and reported through the online response pulse detection (PPD) and short circuit detection (SD) fields in the status register. Figure 6 shows the I²C communication flow of the 1-Wire reset command. Example 5 is a C program that sends a command. It will check the status register to determine the status of the online response pulse.
Figure 6. 1-Wire reset. Start or terminate 1-Wire communication. Continuously detect 1-Wire idle (1WB = 0) and busy status until the 1-Wire command is completed, wait until the 1-Wire command is completed, and then read the result.
// ------------------------------------------------ -------------------------- // Reset all of the devices on the 1-Wire Net and return the result. // // Returns: TRUE (1): presence pulse (s) detected, device (s) reset // FALSE (0): no presence pulses detected // int OWReset (void) {unsigned char status; int poll_count = 0; // 1-Wire reset (Case B) // S AD, 0 [A] 1WRS [A] Sr AD, 1 [A] [Status] A [Status] A \ P // \ -------- / // Repeat until 1WB bit has changed to 0 // [] indicates from slave I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_1WRS, EXPECT_ACK); I2C_rep_start (); I2C_write (I2C_address | I2C_READ, EXPECT_ACK) checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached status = I2C_read (ACK); do {status = I2C_read (status & STATUS_1WB);} while ((status & STATUS_1WB) && (poll_count ++ <POLL_LIMIT)) ; I2C_stop (); // check for failure due to poll limit reached if (poll_count> = POLL_LIMIT) {// handle error // ... DS2482_reset (); r eturn FALSE;} // check for short condition if (status & STATUS_SD) short_detected = TRUE; else short_detected = FALSE; // check for presence detect if (status & STATUS_PPD) return TRUE; else return FALSE;} Example 5. OWReset code
The OWWriteBit / OWReadBit1-Wire bit command (0x87) generates a 1-Wire bit time slot. Figure 7 shows the I²C communication C program for 1-Wire bit commands. In Fig. 8, the bit allocation of the byte, V is the transmission bit. Example 6 shows the OWriteBit, OWReadBit and OWTouchBit programs.
Figure 7. The 1-Wire bit generates a time slot on the 1-Wire bus. When 1WB changes from 1 to 0, the status register holds the valid result of the 1-Wire bit command.
Figure 8. One data byte of 1-Wire
// ------------------------------------------------ -------------------------- // Send 1 bit of communication to the 1-Wire Net. // The parameter 'sendbit' least significant bit is used. // // 'sendbit'-1 bit to send (least significant byte) // void OWWriteBit (unsigned char sendbit) {OWTouchBit (sendbit);} // ------------ -------------------------------------------------- ------------ // Reads 1 bit of communication from the 1-Wire Net and returns the // result // // Returns: 1 bit read from 1-Wire Net // unsigned char OWReadBit (void) {return OWTouchBit (0x01);} // ------------------------------------- ------------------------------------- // Send 1 bit of communication to the 1-Wire Net and return the // result 1 bit read from the 1-Wire Net. The parameter 'sendbit' // least significant bit is used and the least significant bit // of the result is the return bit. // // 'sendbit' -the least significant bit is the bit to send // // Returns: 0: 0 bit read from sendbit // 1: 1 bit read from sendbit // unsigned char OWTouchBit (unsigned char sendbit) {unsigned char status; int poll_count = 0; // 1-Wire bit (Case B) // S AD, 0 [A] 1WSB [A] BB [A] Sr AD , 1 [A] [Status] A [Status] A \ P // \ -------- / // Repeat until 1WB bit has changed to 0 // [] indicates from slave // ​​BB indicates byte containing bit value in msbit I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_1WSB, EXPECT_ACK); I2C_write (sendbit? 0x80: 0x00, EXPECT_ACK); I2C_rep_start (); I2C_address (2) loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached status = I2C_read (ACK); do {status = I2C_read (status & STATUS_1WB);} while ((status & STATUS_1WB) && (poll_count ++ <POLL_LIMIT) ); I2C_stop (); // check for failure due to poll limit reached if (poll_count> = POLL_LIMIT) {// handle error // ... DS2482_reset (); return 0;} // return bit state if (status & STATUS_SBR) return 1; else return 0;} Example 6. 1-Wire bit command
The OWWriteByte1-Wire write byte command (A5h) writes a single data byte to the 1-Wire bus. Before DS2482 executes this command, it must end the working state of 1-Wire. Figure 9 shows the case of writing 1-Wire bytes via I²C. The program in Example 7 checks whether 1-Wire has completed a valid operation before returning from this operation.
Figure 9. 1-Wire write byte. Send the command code to the 1-Wire bus. When 1WB changes from 1 to 0, the 1-Wire write byte command is completed.
// ------------------------------------------------ -------------------------- // Send 8 bits of communication to the 1-Wire Net and verify that the // 8 bits read from the 1-Wire Net are the same (write operation). // The parameter 'sendbyte' least significant 8 bits are used. // // 'sendbyte'-8 bits to send (least significant byte) // // Returns: TRUE : bytes written and echo was the same // FALSE: echo was not the same // void OWWriteByte (unsigned char sendbyte) {unsigned char status; int poll_count = 0; // 1-Wire Write Byte (Case B) // S AD, 0 [A] 1WWB [A] DD [A] Sr AD, 1 [A] [Status] A [Status] A \ P // \ -------- / // Repeat until 1WB bit has changed to 0 // [] indicates from slave // ​​DD data to write I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_1WWB, EXPECT_ACK); I2C_write (sendbyte, EXPECT_ACK); I2C_rep_start (); I2C_write (I | I2C_READ, EXPECT_ACK); // loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached status = I2C_read (ACK); do {status = I2C_read (status & STATUS_1WB);} while ((status & STATUS_1WB) && (poll_count ++ <POLL_LIMIT)); I2C_stop (); // check for failure due to poll limit reached if (poll_count > = POLL_LIMIT) {// handle error // ... DS2482_reset ();}} Example 7. OWWriteByte program
The OWReadByte1-Wire read byte command (96h) reads a single data byte from the 1-Wire network. Before the DS2482 executes this command, it must wait for the 1-Wire working state to end. Figure 10 shows the I²C communication flow. The 1-Wire read byte command procedure is shown in Example 8. Before sending the read byte command, you must check whether the working status of 1-Wire is over.
Figure 10. 1-Wire read byte. Read a byte from the 1-Wire bus. Continuously detect the status register until the 1WB bit changes from 1 to 0. Then set the read pointer to point to the read data register (code E1h), and access the device again to read the data bytes from the 1-Wire bus. // ------------------------------------------------ -------------------------- // Send 8 bits of read communication to the 1-Wire Net and return the // result 8 bits read from the 1-Wire Net. // // Returns: 8 bits read from 1-Wire Net // unsigned char OWReadByte (void) {unsigned char data, status; int poll_count = 0; // 1-Wire Read Bytes (Case C ) // S AD, 0 [A] 1WRB [A] Sr AD, 1 [A] [Status] A [Status] A \ // \ -------- / // Repeat until 1WB bit has changed to 0 // Sr AD, 0 [A] SRP [A] E1 [A] Sr AD, 1 [A] DD A \ P // // [] indicates from slave // ​​DD data read I2C_start (); I2C_write ( I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_1WRB, EXPECT_ACK); I2C_rep_start (); I2C_write (I2C_address | I2C_READ, EXPECT_ACK); // loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached status = I2C_read (ACK); do {status = I2C_read (status & STATUS_1WB);} while ((status & STATUS_1WB) && (poll_count ++ <POLL_LIMIT)); // check for failure due to poll limit reached if (poll_count > = POLL_LIMIT) {// handle error // ... DS2482_reset (); return 0;} I2C_rep_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_SRP, EXPECT_ACK); I2C_write (0xE1, EXPECT_ACK); (); I2C_write (I2C_address | I2C_READ, EXPECT_ACK); data = I2C_read (NACK); I2C_stop (); return data;} Example 8. OWReadByte program
OWBlockOWBlock is used to perform a set of 1-Wire byte operations, which is very useful for data block transmission on 1-Wire networks. Example 9 shows the OWBlock routine.
// ------------------------------------------------ -------------------------- // The 'OWBlock' transfers a block of data to and from the // 1-Wire Net. The result is returned in the same buffer. // // 'tran_buf'-pointer to a block of unsigned // chars of length 'tran_len' that will be sent // to the 1-Wire Net // 'tran_len'-length in bytes to transfer // void OWBlock (unsigned char * tran_buf, int tran_len) {int i; for (i = 0; i <tran_len; i ++) tran_buf [i] = OWTouchByte (tran_buf [i]);} // --- -------------------------------------------------- --------------------- // Send 8 bits of communication to the 1-Wire Net and return the // result 8 bits read from the 1-Wire Net. The parameter 'sendbyte' // least significant 8 bits are used and the least significant 8 bits // of the result are the return byte. // // 'sendbyte'-8 bits to send (least significant byte) // // Returns: 8 bits read from sendbyte // unsigned char OWTouchByte (unsigned char sendbyte) {if (sendbyte == 0xFF) return OWRe adByte (); else {OWWriteByte (sendbyte); return sendbyte;}} Example 9. OWBlock program
The OWSearch / 1-Wire 3-in-1 command 1-Wire search command is used to search for a unique 64-bit registration code for each device in the 1-Wire network. This unique registration code is usually represented by ROM code in the data, because it Store in read-only memory. The search starts with a 1-Wire reset, followed by a search command. The search command answered by all 1-Wire devices is F0h. After the search command, the 1-Wire master will perform a binary tree search to find a device. Binary tree search judges each bit of 64 bits by the following operations: first read a bit, read the complement of the bit, and then write a bit to determine the device left in the search. These three individual bit operation timings are called three-in-one operations. The DS2482 comes with a short command that can be used to perform 1-Wire searches more efficiently.
The three-in-one command (78h) can generate three time slots on the 1-Wire bus, including two read slots and one write slot. The direction byte (DIR) in the status register determines the type of write time slot (Figure 11). Example 10 describes the complete 1-Wire search process, using the 1-Wire three-in-one command. Call OWFirst, and then call OWNext repeatedly to find all devices on the 1-Wire network. For a detailed introduction to the 1-Wire search algorithm, please refer to Application Note 187, "1-Wire Search Algorithm".
Figure 11. The 1-Wire three-in-one command implements the ROM search function on the 1-Wire bus. Idle time is required to complete this 1-Wire function. Then access the device in read mode and get the result through the 1-Wire 3-in-1 command.
// Search state unsigned char ROM_NO [8]; int LastDiscrepancy; int LastFamilyDiscrepancy; int LastDeviceFlag; unsigned char crc8; // ------------------------ -------------------------------------------------- // Find the 'first' devices on the 1-Wire network // Return TRUE: device found, ROM number in ROM_NO buffer // FALSE: no device present // int OWFirst () {// reset the search state LastDiscrepancy = 0 ; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; return OWSearch ();} // --------------------------------- ----------------------------------------- // Find the 'next' devices on the 1-Wire network // Return TRUE: device found, ROM number in ROM_NO buffer // FALSE: device not found, end of search // int OWNext () {// leave the search state alone return OWSearch ();} / / ------------------------------------------------- ------------------------- // The 'OWSearch' function does a general search. This function // continues from the previous search state. The search state // can be reset by using the 'OWFirst' function. // This function contains one parameter 'alarm_only'. // When 'alarm_only' is TRUE (1) the find alarm command // 0xEC is sent instead of the normal search command 0xF0. // Using the find alarm command 0xEC will limit the search to only // 1-Wire devices that are in an 'alarm' state. // // Returns: TRUE (1): when a 1-Wire device was found and its // Serial Number placed in the global ROM // FALSE (0): when no new device was found. Either the // last search was the last device or there // are no devices on the 1-Wire Net. // int OWSearch () {int id_bit_number; int last_zero, rom_byte_number, search_result; int id_bit, cmp_id_bit; unsigned char rom_byte_mask, search_direction, status; // initialize for search id_bit_number = 1; last_zero = 0; rom_byte_number = 0; rom_byte_mask = 1; search_result = 0; // if the last call was not the last one if (! LastDeviceFlag) {// 1-Wire reset if (! OWReset ()) {// reset the search LastDiscrepancy = 0; Last DeviceFlag = FALSE; LastFamilyDiscrepancy = 0; return FALSE;} // issue the search command OWWriteByte (0xF0); // loop to do the search do {// if this discrepancy if before the Last Discrepancy // on a previous next then pick the same as last time if (id_bit_number <LastDiscrepancy) {if ((ROM_NO [rom_byte_number] & rom_byte_mask)> 0) search_direction = 1; else search_direction = 0;} else {// if equal to last pick 1, if not then pick 0 if (id_bit_number == LastDiscrepancy) search_direction = 1; else search_direction = 0;} // Perform a triple operation on the DS2482 which will perform // 2 read bits and 1 write bit status = DS2482_search_triplet (search_direction); // check bit results in status byte id_bit = ((status & STATUS_SBR) == STATUS_SBR); cmp_id_bit = ((status & STATUS_TSB) == STATUS_TSB); search_direction = ((status & STATUS_DIR) == STATUS_DIR)? (byte) 1: (byte) ) 0; // check for no devices on 1-Wire if ((id_bit) && (cmp_id_bit)) break; else {if ((! Id_bit) && (! Cmp_id_bit) & & (search_direction == 0)) {last_zero = id_bit_number; // check for Last discrepancy in family if (last_zero <9) LastFamilyDiscrepancy = last_zero;} // set or clear the bit in the ROM byte rom_byte_number // with mask rom_byte_mask if (search_direction == 1) ROM_NO [rom_byte_number] | = rom_byte_mask; else ROM_NO [rom_byte_number] & = (byte) ~ rom_byte_mask; // increment the byte counter id_bit_number // and shift the mask rom_byte_mask id_bit_number ++; rom_byte_mask << = 1; / / if the mask is 0 then go to new SerialNum byte rom_byte_number // and reset mask if (rom_byte_mask == 0) {calc_crc8 (ROM_NO [rom_byte_number]); // accumulate the CRC rom_byte_number ++; rom_byte_mask = 1;}}} while ( rom_byte_number <8); // loop until through all ROM bytes 0-7 // if the search was successful then if (! ((id_bit_number <65) || (crc8! = 0))) {// search successful so set LastDiscrepancy, LastDeviceFlag // search_result LastDiscrepancy = last_zero; // check for last device if (LastDiscrepancy == 0) LastD eviceFlag = TRUE; search_result = TRUE;}} // if no device found then reset counters so next // 'search' will be like a first if (! search_result || (ROM_NO [0] == 0)) {LastDiscrepancy = 0; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; search_result = FALSE;} return search_result;} // ----------------------------- --------------------------------------------- // Use the DS2482 help command '1-Wire triplet' to perform one bit of a // 1-Wire search. // This command does two read bits and one write bit. The write bit // is either the default direction (all device have same bit ) or in case of // a discrepancy, the 'search_direction' parameter is used. // // Returns – The DS2482 status byte result from the triplet command // unsigned char DS2482_search_triplet (int search_direction) {unsigned char status; int poll_count = 0; // 1-Wire Triplet (Case B) // S AD, 0 [A] 1WT [A] SS [A] Sr AD, 1 [A] [Status] A [Status] A \ P // \- ------- / // Repeat until 1WB bit has changed to 0 // [] indicates from slave // ​​SS indicates byte containing search direction bit value in msbit I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_1WT, EXPECT_ACK); I2C_write (search_direction? 0x80: 0x00, EXPECT_ACK); I2C_rep2start (; (I2C_address | I2C_READ, EXPECT_ACK); // loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached status = I2C_read (ACK); do {status = I2C_read (status & STATUS_1WB);} while (( status & STATUS_1WB) && (poll_count ++ <POLL_LIMIT)); I2C_stop (); // check for failure due to poll limit reached if (poll_count> = POLL_LIMIT) {// handle error // ... DS2482_reset (); return 0; } // return status byte return status;} Example 10. OWSearch program
Extended 1-Wire operating mode OWSpeed ​​Example 11 shows how to use DS2482 to change the 1-Wire bus rate routine. All 1-Wire devices work by default at the standard communication rate. The Skip-ROM command shifts to the high-speed operation mode. Once the device is operating in high-speed mode, all 1-Wire communications will use high-speed timing. The standard rate 1-Wire reset command will return all devices to the standard rate.
// ------------------------------------------------ -------------------------- // Set the 1-Wire Net communication speed. // // 'new_speed'-new speed defined as / / MODE_STANDARD 0x00 // MODE_OVERDRIVE 0x01 // // Returns: current 1-Wire Net speed // int OWSpeed ​​(int new_speed) {// set the speed if (new_speed == MODE_OVERDRIVE) c1WS = CONFIG_1WS; else c1WS = FALSE; / / write the new config DS2482_write_config (c1WS | cSPU | cPPM | cAPU); return new_speed;} Example 11. OWSpeed ​​program
Example 12 of OWLevel shows how to use DS2482 to change the 1-Wire bus level. DS2482 can enable strong pull-up after performing a bit or byte communication. Subsequently, the OWLevel program returns the 1-Wire network to the standard pull-up, using OOWriteBytePower or OWReadBitPower operations to enable strong pull-ups.
// ------------------------------------------------ -------------------------- // Set the 1-Wire Net line level pullup to normal. The DS2482 only // allows enabling strong pullup on a bit or byte event. consequent this // function only allows the MODE_STANDARD argument. To enable strong pullup // use OWWriteBytePower or OWReadBitPower. // // 'new_level'-new level defined as // MODE_STANDARD 0x00 // // Returns: current 1-Wire Net level // int OWLevel(int new_level) { // function only will turn back to non-strong pullup if (new_level != MODE_STANDARD) return MODE_STRONG; // clear the strong pullup bit in the global config state cSPU = FALSE; // write the new config DS2482_write_config(c1WS | cSPU | cPPM | cAPU); return MODE_STANDARD; }例12. OWLevel程åº
OWReadBitPower例13是OWReadBitPower所使用的程åºï¼Œç”¨äºŽè¯»å–一个1-Wireä½å¹¶æ‰§è¡Œä¾›ç”µåŠŸèƒ½ã€‚当é…置寄å˜å™¨ä¸çš„强上拉(SPU)ä½ä½¿èƒ½æ—¶ï¼Œåœ¨ä¸‹ä¸€ä½æˆ–å—节通信完æˆåŽï¼ŒDS2482将有æºæ‹‰é«˜1-Wire总线。该æ“作å¯é€šè¿‡æ˜¯å¦èŽ·å¾—相应的å“应æ¥éªŒè¯è¯»ä½çš„æ£ç¡®æ€§ï¼Œå¦‚æžœå“应ä¸æ£ç¡®ï¼Œ1-Wireç”µå¹³å°†è¿”å›žåˆ°æ ‡å‡†çš„ä¸Šæ‹‰çŠ¶æ€ã€‚
//-------------------------------------------------------------------------- // Send 1 bit of communication to the 1-Wire Net and verify that the // response matches the 'applyPowerResponse' bit and apply power delivery // to the 1-Wire net. Note that some implementations may apply the power // first and then turn it off if the response is incorrect. // // 'applyPowerResponse' - 1 bit response to check, if correct then start // power delivery // // Returns: TRUE: bit written and response correct, strong pullup now on // FALSE: response incorrect // int OWReadBitPower(int applyPowerResponse) { unsigned char rdbit; // set strong pullup enable cSPU = CONFIG_SPU; // write the new config if (!DS2482_write_config(c1WS | cSPU | cPPM | cAPU)) return FALSE; // perform read bit rdbit = OWReadBit(); // check if response was correct, if not then turn off strong pullup if (rdbit != applyPowerResponse) { OWLevel(MODE_STANDARD); return FALSE; } return TRUE; }例13. OWReadBitPower程åº
OWWriteBytePower例14为OWWriteBytePower所采用的程åºï¼Œç”¨äºŽå†™å…¥1-Wireå—节并执行强上拉供电功能。当é…置寄å˜å™¨ä¸çš„强上拉(SPU)ä½ä½¿èƒ½æ—¶ï¼Œåœ¨ä¸‹ä¸€ä½æˆ–å—节通信完æˆåŽï¼ŒDS2482将由æºæ‹‰é«˜1-Wire总线。
//-------------------------------------------------------------------------- // Send 8 bits of communication to the 1-Wire Net and verify that the // 8 bits read from the 1-Wire Net are the same (write operation). // The parameter 'sendbyte' least significant 8 bits are used. After the // 8 bits are sent change the level of the 1-Wire net. // // 'sendbyte' - 8 bits to send (least significant bit) // // Returns: TRUE: bytes written and echo was the same, strong pullup now on // FALSE: echo was not the same // int OWWriteBytePower(int sendbyte) { // set strong pullup enable cSPU = CONFIG_SU; // write the new config if (!DS2482_write_config(c1WS | cSPU | cPPM | cAPU)) return FALSE; // perform write byte OWWriteByte(sendbyte); return TRUE; }例14. OWWriteBytePower程åº
结论DS2482å·²ç»è¿‡æµ‹è¯•ï¼Œå¯æˆåŠŸå®ŒæˆI²C接å£è‡³1-Wire网络的转æ¢ã€‚本文给出了基于DS2482 I²C 1-Wire线驱动器的完整的1-Wire接å£æ–¹æ¡ˆï¼Œè¯¥æŽ¥å£é€‚用于所有1-Wire器件,所æ供的例程å¯ä»¥åœ¨ä»»ä½•ä¸€ä¸ªå…·æœ‰I²C通信å£çš„主机上轻æ¾å®žçŽ°ã€‚å¦å¤–还æä¾›å¯ä¾›ä¸‹è½½çš„完整的Cè¯è¨€æ‰§è¡Œç¨‹åºã€‚测试平å°æ˜¯CMAXQUSB评估套件。
Introduction The 1-Wire network includes a master and one or more slave devices. The 1-Wire master can be composed of an I / O pin of the microprocessor and manually generate timing pulses. The DS2482 I²C to 1-Wire network bridge device can generate detailed 1-Wire communication timing without the need for engineers to participate in the design. Figure 1 shows a simplified block diagram of the DS2482 configuration. This article introduces an effective method for implementing application program interface (API) using DS2482, supporting basic and extended 1-Wire operations. The I²C communication corresponding to 1-Wire operation is described in detail. These operations lay a comprehensive foundation for performing all functions of current and future 1-Wire devices, except for device programming based on DS250x series EPROM. This outline of 1-Wire operation is suitable for 1-Wire applications that do not rely on a 1-Wire master.
This article is only a supplement to the DS2482 data sheet, and cannot replace the data sheet. The DS2482 is available in two configurations, a single-channel 1-Wire master controller (DS2482-100) and a 1-Wire master controller with low-power sleep mode (DS2482-101), and an eight-channel 1-Wire master controller (DS2482-800).
Figure 1. Functional diagram of the DS2482 bridge that implements I²C and 1-Wire network communication
The 1-Wire interface below gives a few basic 1-Wire functions, called original functions, that is, the functions that must be available in the application in order to perform all 1-Wire operations. The first function (OWReset) is to reset all 1-Wire slave devices on the network in preparation for receiving commands from the 1-Wire master controller. The second function (OWWriteBit) completes the operation of the 1-Wire master writing a bit to the slave device, and the third function (OWReadBit) completes the operation of reading a bit from the 1-Wire slave device. Since all 1-Wire bit communications must be initiated by the 1-Wire master, "reading" is actually the result of sampling after "writing" a bit. Almost all other 1-Wire operations can be composed of these three operations. For example, writing a byte to a 1-Wire network is equivalent to writing one bit 8 times.
The 1-Wire search algorithm can also be constructed using the same three original functions. With the three 1-Wire commands, the DS2482 can perform search functions, greatly reducing the amount of communication required for search operations. Similarly, 1-byte 1-Wire communication commands are more efficient than eight bit-by-bit operations.
Table 1 shows three basic primitive functions (OWReset, OWWriteBit / OWReadBit and OWWriteByte / OWReadByte) and three other very useful functions (OWBlock, OWSearch and msDelay), which constitute a series of main basic 1-Wire operations. These operation names will be used below.
Table 1. Basic 1-Wire operation
OperaTIon | DescripTIon |
OWReset | Sends the 1-Wire reset sTImulus and check for the presence pulse of 1-Wire slave devices. |
OWWriteBit / OWReadBit | Sends to or receives from the 1-Wire network a single bit of data. |
OWWriteByte / OWReadByte | Sends to or receives from the 1-Wire network a single byte of data. |
OWBlock | Sends to and receives from the 1-Wire network mulTIple bytes of data. |
OWSearch | Performs the 1-Wire Search Algorithm (see application note 187). |
msDelay | Delays at least the specified number of milliseconds. Used for timing strong pullup operations. |
Many 1-Wire slave devices can operate at two different communication rates: standard rate and high-speed mode. All devices support standard rate mode. The high-speed rate is about 10 times the standard rate. DS2482 supports both of these 1-Wire rates.
1-Wire devices usually draw some or all of the operating power from the 1-Wire bus. However, some devices require additional power for certain operations of the protocol. For example, a device may need to perform temperature conversion or perform a SHA-1 hash algorithm. Power for this operation is provided by enabling a strong pull-up on the 1-Wire bus. In this power supply mode, normal communication cannot be performed. The DS2482 is powered by setting the strong pull-up flag (SPU) bit, which will initiate a strong pull-up after the next byte / bit communication of 1-Wire is complete. The DS2482-100 and DS2482-101 also have an external pin (PCTLZ) that can control high current strong pull-ups.
Table 2 lists the extended 1-Wire operation for 1-Wire rate setting, power supply, and programming pulses.
Table 2. Extended 1-Wire operation
Operation | Description |
OWSpeed | Sets the 1-Wire communication speed, either standard or overdrive. Note that this only changes the communication speed of the 1-Wire master; the 1-Wire slave device must be instructed to make the switch when going from normal to overdrive. The 1 -Wire slave will always revert to standard speed when it encounters a standard-speed 1-Wire reset. |
OWLevel | Sets the 1-Wire power level (normal or power delivery). |
OWReadBitPower | Reads a single bit of data from the 1-Wire network and optionally applies power delivery immediately after the bit is complete. |
OWWriteBytePower | Sends a single byte of data to the 1-Wire network and applies power delivery immediately after the byte is complete. |
Host Configuration The DS2482 host should have an I²C communication port. This article does not describe the configuration of the host. However, the host must provide standard I²C interface operation. It should be noted that the host interface has advanced functions that include some I²C interface operations. Refer to Table 3 for required operations.
Table 3. Required I²C host operations
Operation | Description |
I2C_start | I²C start command. |
I2C_rep_start | I²C repeated start command. |
I2C_stop | I²C stop command. |
I2C_write | Writes a byte to the I²C bus. The byte to write is passed to the function. |
I2C_read | Reads a byte from the I²C bus. The byte read is returned from the function. |
Before configuring DS2482 to attempt 1-Wire operation, the host must set up and synchronize the I²C to 1-Wire line driver DS2482. To communicate with the DS2482, the host must know its slave address. Figure 2 shows the slave addresses of DS2482-100, DS2482-101, and DS2482-800.
Figure 2. I²C slave address of DS2482
DS2482's I²C command The following symbol descriptions come from DS2482 data materials, which are represented by abbreviated symbols and are used to explain the I²C communication process of the device. Next, we will repeat these communication processes and provide additional comments and C language routines for basic and extended 1-Wire operations.
I²C communication process-symbol description
Symbol | Description |
S | START Condition |
AD, 0 | Select DS2482 for Write Access |
AD, 1 | Select DS2482 for Read Access |
Sr | Repeated START Condition |
P | STOP Condition |
A | Acknowledged |
A \ | Not acknowledged |
(Idle) | Bus not busy |
Transfer of one byte | |
DRST | Command 'Device Reset', F0h |
WCFG | Command 'Write Configuration', D2h |
CHSL | Command 'Channel Select', C3h (DS2482-800 only) |
SRP | Command 'Set Read Pointer', E1h |
1WRS | Command '1-Wire Reset', B4h |
1WWB | Command '1-Wire Write Byte', A5h |
1WRB | Command '1-Wire Read Byte', 96h |
1WSB | Command '1-Wire Single Bit', 87h |
1WT | Command '1-Wire Triplet', 78h |
Data direction notation
Master-to-Slave | Slave-to-Master |
The data direction representation method in many figures in this article indicates whether the communication direction is master to slave (grey) or slave to master (white).
DS2482 configuration operation The following operations are used to set up and configure the DS2482. Some operations require calling 1-Wire operation subroutines.
The C program shown in DS2482 detection example 1 provides the detection and configuration process. The default values ​​written to DS2482 include 1-Wire rate (standard), strong pull-up (shutdown), online pulse mask (shutdown), and active pull-up through). This state is maintained as a general variable and can be restored if the device needs to be reset to this default state. Different default values ​​can be selected for different applications.
// DS2482 state unsigned char I2C_address; int c1WS, cSPU, cPPM, cAPU; int short_detected; // ---------------------------- ---------------------------------------------- // DS2428 Detect routine that sets the I2C address and then performs a // device reset followed by writing the configuration byte to default values: // 1-Wire speed (c1WS) = standard (0) // Strong pullup (cSPU) = off (0) // Presence pulse masking (cPPM) = off (0) // Active pullup (cAPU) = on (CONFIG_APU = 0x01) // // Returns: TRUE if device was detected and written // FALSE device not detected or failure to write configuration byte // int DS2482_detect (unsigned char addr) {// set global address I2C_address = addr; // reset the DS2482 ON selected address if (! DS2482_reset ()) return FALSE; // default configuration c1WS = FALSE; cSPU = FALSE ; cPPM = FALSE; cAPU = CONFIG_APU; // write the default configuration setup if (! DS2482_write_config (c1WS | cSPU | cPPM | cAPU)) return FALSE; return TRUE;} Example 1. DS2482 test
DS2482 device reset Figure 3 is the flow of DS2482 device reset I²C communication. Example 2 shows the C program of the DS2482 reset command, which can completely reset the device state machine logic and terminate all ongoing 1-Wire communications. The device reset command code is F0h.
Figure 3. Device reset after power on. This example includes optional read access to verify that the command was successfully executed.
// ------------------------------------------------ -------------------------- // Perform a device reset on the DS2482 // // Returns: TRUE if device was reset // FALSE device not detected or failure to perform reset // int DS2482_reset () {unsigned char status; // Device Reset // S AD, 0 [A] DRST [A] Sr AD, 1 [A] [SS] A \ P // [] indicates from slave // ​​SS status byte to read to verify state I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_DRST, EXPECT_ACK); I2C_rep_start (); I2C_write (I2C_address | I2C_READ, EXPECT_ACK) I2C_read (NACK); I2C_stop (); // check for failure due to incorrect read back of status return ((status & 0xF7) == 0x10);} Example 2. Reset device code
DS2482 write configuration Figure 4 shows the I²C communication routine for the DS2482 write configuration; Example 3 shows the C program that implements the DS2482 write configuration command timing. The write configuration command code is D2h.
Figure 4. The write configuration register, which includes an optional read operation to verify the successful execution of the command.
// ------------------------------------------------ -------------------------- // Write the configuration register in the DS2482. The configuration // options are provided in the lower nibble of the provided config byte. // The uppper nibble in bitwise inverted when written to the DS2482. // // Returns: TRUE: config written and response correct // FALSE: response incorrect // int DS2482_write_config (unsigned char config) {unsigned char read_config; // Write configuration (Case A) // S AD, 0 [A] WCFG [A] CF [A] Sr AD, 1 [A] [CF] A \ P // [] indicates from slave // ​​CF configuration byte to write I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_WCFG, EXPECT_ACK); I2C_write (config | (~ config << 4), EXPECT_ACK); I2C_rep_start (); I2C_write (I2C_address | 2 | read_config = I2C_read (NACK); I2C_stop (); // check for failure due to incorrect read back if (config! = read_config) {// handle error // ... DS2482_reset (); return FALSE;} return TRUE;} Example 3. DS2 482 write configuration
DS2482 channel selection Figure 5 shows an example of I²C communication that implements DS2482-800 channel selection. The effective channels are 0 to 7. Note that this operation is not suitable for DS2482-100 or DS2482-101. Example 4 shows the C program that executes the DS2482-800 channel selection command. The channel selection command code is C3h. After selecting the channel, the selected channel can perform all 1-Wire operations.
Figure 5. Write channel selection register. This example includes an optional read operation to verify the successful execution of the command.
// ------------------------------------------------ -------------------------- // Select the 1-Wire channel on a DS2482-800. // // Returns: TRUE if channel selected // FALSE device not detected or failure to perform select // int DS2482_channel_select (int channel) {unsigned char ch, ch_read, check; // Channel Select (Case A) // S AD, 0 [A] CHSL [A] CC [A] Sr AD, 1 [A] [RR] A \ P // [] indicates from slave // ​​CC channel value // RR channel read back I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_CHSL , EXPECT_ACK); switch (channel) {default: case 0: ch = 0xF0; ch_read = 0xB8; break; case 1: ch = 0xE1; ch_read = 0xB1; break; case 2: ch = 0xD2; ch_read = 0xAA; break; case 3: ch = 0xC3; ch_read = 0xA3; break; case 4: ch = 0xB4; ch_read = 0x9C; break; case 5: ch = 0xA5; ch_read = 0x95; break; case 6: ch = 0x96; ch_read = 0x8E; break; case 7: ch = 0x87; ch_read = 0x87; break;}; I2C_write (ch, EXPECT_ACK); I2C_rep_start (); I2C_write (I2C_address | I2C_READ, EXPECT_ACK); check = I2C_read (NACK); I2C_stop (); // check for failure due to incorrect read back of channel return (check == ch_read);} Example 4. DS2482-800 channel selection
The DS2482's 1-Wire operation OWReset1-Wire Reset command (B4h) performs 1-Wire reset and 1-Wire device online response pulse detection on the 1-Wire bus. The status of the 1-Wire bus can be sampled and reported through the online response pulse detection (PPD) and short circuit detection (SD) fields in the status register. Figure 6 shows the I²C communication flow of the 1-Wire reset command. Example 5 is a C program that sends a command. It will check the status register to determine the status of the online response pulse.
Figure 6. 1-Wire reset. Start or terminate 1-Wire communication. Continuously detect 1-Wire idle (1WB = 0) and busy status until the 1-Wire command is completed, wait until the 1-Wire command is completed, and then read the result.
// ------------------------------------------------ -------------------------- // Reset all of the devices on the 1-Wire Net and return the result. // // Returns: TRUE (1): presence pulse (s) detected, device (s) reset // FALSE (0): no presence pulses detected // int OWReset (void) {unsigned char status; int poll_count = 0; // 1-Wire reset (Case B) // S AD, 0 [A] 1WRS [A] Sr AD, 1 [A] [Status] A [Status] A \ P // \ -------- / // Repeat until 1WB bit has changed to 0 // [] indicates from slave I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_1WRS, EXPECT_ACK); I2C_rep_start (); I2C_write (I2C_address | I2C_READ, EXPECT_ACK) checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached status = I2C_read (ACK); do {status = I2C_read (status & STATUS_1WB);} while ((status & STATUS_1WB) && (poll_count ++ <POLL_LIMIT)) ; I2C_stop (); // check for failure due to poll limit reached if (poll_count> = POLL_LIMIT) {// handle error // ... DS2482_reset (); r eturn FALSE;} // check for short condition if (status & STATUS_SD) short_detected = TRUE; else short_detected = FALSE; // check for presence detect if (status & STATUS_PPD) return TRUE; else return FALSE;} Example 5. OWReset code
The OWWriteBit / OWReadBit1-Wire bit command (0x87) generates a 1-Wire bit time slot. Figure 7 shows the I²C communication C program for 1-Wire bit commands. In Fig. 8, the bit allocation of the byte, V is the transmission bit. Example 6 shows the OWriteBit, OWReadBit and OWTouchBit programs.
Figure 7. The 1-Wire bit generates a time slot on the 1-Wire bus. When 1WB changes from 1 to 0, the status register holds the valid result of the 1-Wire bit command.
Figure 8. One data byte of 1-Wire
// ------------------------------------------------ -------------------------- // Send 1 bit of communication to the 1-Wire Net. // The parameter 'sendbit' least significant bit is used. // // 'sendbit'-1 bit to send (least significant byte) // void OWWriteBit (unsigned char sendbit) {OWTouchBit (sendbit);} // ------------ -------------------------------------------------- ------------ // Reads 1 bit of communication from the 1-Wire Net and returns the // result // // Returns: 1 bit read from 1-Wire Net // unsigned char OWReadBit (void) {return OWTouchBit (0x01);} // ------------------------------------- ------------------------------------- // Send 1 bit of communication to the 1-Wire Net and return the // result 1 bit read from the 1-Wire Net. The parameter 'sendbit' // least significant bit is used and the least significant bit // of the result is the return bit. // // 'sendbit' -the least significant bit is the bit to send // // Returns: 0: 0 bit read from sendbit // 1: 1 bit read from sendbit // unsigned char OWTouchBit (unsigned char sendbit) {unsigned char status; int poll_count = 0; // 1-Wire bit (Case B) // S AD, 0 [A] 1WSB [A] BB [A] Sr AD , 1 [A] [Status] A [Status] A \ P // \ -------- / // Repeat until 1WB bit has changed to 0 // [] indicates from slave // ​​BB indicates byte containing bit value in msbit I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_1WSB, EXPECT_ACK); I2C_write (sendbit? 0x80: 0x00, EXPECT_ACK); I2C_rep_start (); I2C_address (2) loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached status = I2C_read (ACK); do {status = I2C_read (status & STATUS_1WB);} while ((status & STATUS_1WB) && (poll_count ++ <POLL_LIMIT) ); I2C_stop (); // check for failure due to poll limit reached if (poll_count> = POLL_LIMIT) {// handle error // ... DS2482_reset (); return 0;} // return bit state if (status & STATUS_SBR) return 1; else return 0;} Example 6. 1-Wire bit command
The OWWriteByte1-Wire write byte command (A5h) writes a single data byte to the 1-Wire bus. Before DS2482 executes this command, it must end the working state of 1-Wire. Figure 9 shows the case of writing 1-Wire bytes via I²C. The program in Example 7 checks whether 1-Wire has completed a valid operation before returning from this operation.
Figure 9. 1-Wire write byte. Send the command code to the 1-Wire bus. When 1WB changes from 1 to 0, the 1-Wire write byte command is completed.
// ------------------------------------------------ -------------------------- // Send 8 bits of communication to the 1-Wire Net and verify that the // 8 bits read from the 1-Wire Net are the same (write operation). // The parameter 'sendbyte' least significant 8 bits are used. // // 'sendbyte'-8 bits to send (least significant byte) // // Returns: TRUE : bytes written and echo was the same // FALSE: echo was not the same // void OWWriteByte (unsigned char sendbyte) {unsigned char status; int poll_count = 0; // 1-Wire Write Byte (Case B) // S AD, 0 [A] 1WWB [A] DD [A] Sr AD, 1 [A] [Status] A [Status] A \ P // \ -------- / // Repeat until 1WB bit has changed to 0 // [] indicates from slave // ​​DD data to write I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_1WWB, EXPECT_ACK); I2C_write (sendbyte, EXPECT_ACK); I2C_rep_start (); I2C_write (I | I2C_READ, EXPECT_ACK); // loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached status = I2C_read (ACK); do {status = I2C_read (status & STATUS_1WB);} while ((status & STATUS_1WB) && (poll_count ++ <POLL_LIMIT)); I2C_stop (); // check for failure due to poll limit reached if (poll_count > = POLL_LIMIT) {// handle error // ... DS2482_reset ();}} Example 7. OWWriteByte program
The OWReadByte1-Wire read byte command (96h) reads a single data byte from the 1-Wire network. Before the DS2482 executes this command, it must wait for the 1-Wire working state to end. Figure 10 shows the I²C communication flow. The 1-Wire read byte command procedure is shown in Example 8. Before sending the read byte command, you must check whether the working status of 1-Wire is over.
Figure 10. 1-Wire read byte. Read a byte from the 1-Wire bus. Continuously detect the status register until the 1WB bit changes from 1 to 0. Then set the read pointer to point to the read data register (code E1h), and access the device again to read the data bytes from the 1-Wire bus. // ------------------------------------------------ -------------------------- // Send 8 bits of read communication to the 1-Wire Net and return the // result 8 bits read from the 1-Wire Net. // // Returns: 8 bits read from 1-Wire Net // unsigned char OWReadByte (void) {unsigned char data, status; int poll_count = 0; // 1-Wire Read Bytes (Case C ) // S AD, 0 [A] 1WRB [A] Sr AD, 1 [A] [Status] A [Status] A \ // \ -------- / // Repeat until 1WB bit has changed to 0 // Sr AD, 0 [A] SRP [A] E1 [A] Sr AD, 1 [A] DD A \ P // // [] indicates from slave // ​​DD data read I2C_start (); I2C_write ( I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_1WRB, EXPECT_ACK); I2C_rep_start (); I2C_write (I2C_address | I2C_READ, EXPECT_ACK); // loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached status = I2C_read (ACK); do {status = I2C_read (status & STATUS_1WB);} while ((status & STATUS_1WB) && (poll_count ++ <POLL_LIMIT)); // check for failure due to poll limit reached if (poll_count > = POLL_LIMIT) {// handle error // ... DS2482_reset (); return 0;} I2C_rep_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_SRP, EXPECT_ACK); I2C_write (0xE1, EXPECT_ACK); (); I2C_write (I2C_address | I2C_READ, EXPECT_ACK); data = I2C_read (NACK); I2C_stop (); return data;} Example 8. OWReadByte program
OWBlockOWBlock is used to perform a set of 1-Wire byte operations, which is very useful for data block transmission on 1-Wire networks. Example 9 shows the OWBlock routine.
// ------------------------------------------------ -------------------------- // The 'OWBlock' transfers a block of data to and from the // 1-Wire Net. The result is returned in the same buffer. // // 'tran_buf'-pointer to a block of unsigned // chars of length 'tran_len' that will be sent // to the 1-Wire Net // 'tran_len'-length in bytes to transfer // void OWBlock (unsigned char * tran_buf, int tran_len) {int i; for (i = 0; i <tran_len; i ++) tran_buf [i] = OWTouchByte (tran_buf [i]);} // --- -------------------------------------------------- --------------------- // Send 8 bits of communication to the 1-Wire Net and return the // result 8 bits read from the 1-Wire Net. The parameter 'sendbyte' // least significant 8 bits are used and the least significant 8 bits // of the result are the return byte. // // 'sendbyte'-8 bits to send (least significant byte) // // Returns: 8 bits read from sendbyte // unsigned char OWTouchByte (unsigned char sendbyte) {if (sendbyte == 0xFF) return OWRe adByte (); else {OWWriteByte (sendbyte); return sendbyte;}} Example 9. OWBlock program
The OWSearch / 1-Wire 3-in-1 command 1-Wire search command is used to search for a unique 64-bit registration code for each device in the 1-Wire network. This unique registration code is usually represented by ROM code in the data, because it Store in read-only memory. The search starts with a 1-Wire reset, followed by a search command. The search command answered by all 1-Wire devices is F0h. After the search command, the 1-Wire master will perform a binary tree search to find a device. Binary tree search judges each bit of 64 bits by the following operations: first read a bit, read the complement of the bit, and then write a bit to determine the device left in the search. These three individual bit operation timings are called three-in-one operations. The DS2482 comes with a short command that can be used to perform 1-Wire searches more efficiently.
The three-in-one command (78h) can generate three time slots on the 1-Wire bus, including two read slots and one write slot. The direction byte (DIR) in the status register determines the type of write time slot (Figure 11). Example 10 describes the complete 1-Wire search process, using the 1-Wire three-in-one command. Call OWFirst, and then call OWNext repeatedly to find all devices on the 1-Wire network. For a detailed introduction to the 1-Wire search algorithm, please refer to Application Note 187, "1-Wire Search Algorithm".
Figure 11. The 1-Wire three-in-one command implements the ROM search function on the 1-Wire bus. Idle time is required to complete this 1-Wire function. Then access the device in read mode and get the result through the 1-Wire 3-in-1 command.
// Search state unsigned char ROM_NO [8]; int LastDiscrepancy; int LastFamilyDiscrepancy; int LastDeviceFlag; unsigned char crc8; // ------------------------ -------------------------------------------------- // Find the 'first' devices on the 1-Wire network // Return TRUE: device found, ROM number in ROM_NO buffer // FALSE: no device present // int OWFirst () {// reset the search state LastDiscrepancy = 0 ; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; return OWSearch ();} // --------------------------------- ----------------------------------------- // Find the 'next' devices on the 1-Wire network // Return TRUE: device found, ROM number in ROM_NO buffer // FALSE: device not found, end of search // int OWNext () {// leave the search state alone return OWSearch ();} / / ------------------------------------------------- ------------------------- // The 'OWSearch' function does a general search. This function // continues from the previous search state. The search state // can be reset by using the 'OWFirst' function. // This function contains one parameter 'alarm_only'. // When 'alarm_only' is TRUE (1) the find alarm command // 0xEC is sent instead of the normal search command 0xF0. // Using the find alarm command 0xEC will limit the search to only // 1-Wire devices that are in an 'alarm' state. // // Returns: TRUE (1): when a 1-Wire device was found and its // Serial Number placed in the global ROM // FALSE (0): when no new device was found. Either the // last search was the last device or there // are no devices on the 1-Wire Net. // int OWSearch () {int id_bit_number; int last_zero, rom_byte_number, search_result; int id_bit, cmp_id_bit; unsigned char rom_byte_mask, search_direction, status; // initialize for search id_bit_number = 1; last_zero = 0; rom_byte_number = 0; rom_byte_mask = 1; search_result = 0; // if the last call was not the last one if (! LastDeviceFlag) {// 1-Wire reset if (! OWReset ()) {// reset the search LastDiscrepancy = 0; Last DeviceFlag = FALSE; LastFamilyDiscrepancy = 0; return FALSE;} // issue the search command OWWriteByte (0xF0); // loop to do the search do {// if this discrepancy if before the Last Discrepancy // on a previous next then pick the same as last time if (id_bit_number <LastDiscrepancy) {if ((ROM_NO [rom_byte_number] & rom_byte_mask)> 0) search_direction = 1; else search_direction = 0;} else {// if equal to last pick 1, if not then pick 0 if (id_bit_number == LastDiscrepancy) search_direction = 1; else search_direction = 0;} // Perform a triple operation on the DS2482 which will perform // 2 read bits and 1 write bit status = DS2482_search_triplet (search_direction); // check bit results in status byte id_bit = ((status & STATUS_SBR) == STATUS_SBR); cmp_id_bit = ((status & STATUS_TSB) == STATUS_TSB); search_direction = ((status & STATUS_DIR) == STATUS_DIR)? (byte) 1: (byte) ) 0; // check for no devices on 1-Wire if ((id_bit) && (cmp_id_bit)) break; else {if ((! Id_bit) && (! Cmp_id_bit) & & (search_direction == 0)) {last_zero = id_bit_number; // check for Last discrepancy in family if (last_zero <9) LastFamilyDiscrepancy = last_zero;} // set or clear the bit in the ROM byte rom_byte_number // with mask rom_byte_mask if (search_direction == 1) ROM_NO [rom_byte_number] | = rom_byte_mask; else ROM_NO [rom_byte_number] & = (byte) ~ rom_byte_mask; // increment the byte counter id_bit_number // and shift the mask rom_byte_mask id_bit_number ++; rom_byte_mask << = 1; / / if the mask is 0 then go to new SerialNum byte rom_byte_number // and reset mask if (rom_byte_mask == 0) {calc_crc8 (ROM_NO [rom_byte_number]); // accumulate the CRC rom_byte_number ++; rom_byte_mask = 1;}}} while ( rom_byte_number <8); // loop until through all ROM bytes 0-7 // if the search was successful then if (! ((id_bit_number <65) || (crc8! = 0))) {// search successful so set LastDiscrepancy, LastDeviceFlag // search_result LastDiscrepancy = last_zero; // check for last device if (LastDiscrepancy == 0) LastD eviceFlag = TRUE; search_result = TRUE;}} // if no device found then reset counters so next // 'search' will be like a first if (! search_result || (ROM_NO [0] == 0)) {LastDiscrepancy = 0; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; search_result = FALSE;} return search_result;} // ----------------------------- --------------------------------------------- // Use the DS2482 help command '1-Wire triplet' to perform one bit of a // 1-Wire search. // This command does two read bits and one write bit. The write bit // is either the default direction (all device have same bit ) or in case of // a discrepancy, the 'search_direction' parameter is used. // // Returns – The DS2482 status byte result from the triplet command // unsigned char DS2482_search_triplet (int search_direction) {unsigned char status; int poll_count = 0; // 1-Wire Triplet (Case B) // S AD, 0 [A] 1WT [A] SS [A] Sr AD, 1 [A] [Status] A [Status] A \ P // \- ------- / // Repeat until 1WB bit has changed to 0 // [] indicates from slave // ​​SS indicates byte containing search direction bit value in msbit I2C_start (); I2C_write (I2C_address | I2C_WRITE, EXPECT_ACK); I2C_write (CMD_1WT, EXPECT_ACK); I2C_write (search_direction? 0x80: 0x00, EXPECT_ACK); I2C_rep2start (; (I2C_address | I2C_READ, EXPECT_ACK); // loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached status = I2C_read (ACK); do {status = I2C_read (status & STATUS_1WB);} while (( status & STATUS_1WB) && (poll_count ++ <POLL_LIMIT)); I2C_stop (); // check for failure due to poll limit reached if (poll_count> = POLL_LIMIT) {// handle error // ... DS2482_reset (); return 0; } // return status byte return status;} Example 10. OWSearch program
Extended 1-Wire operating mode OWSpeed ​​Example 11 shows how to use DS2482 to change the 1-Wire bus rate routine. All 1-Wire devices work by default at the standard communication rate. The Skip-ROM command shifts to the high-speed operation mode. Once the device is operating in high-speed mode, all 1-Wire communications will use high-speed timing. The standard rate 1-Wire reset command will return all devices to the standard rate.
// ------------------------------------------------ -------------------------- // Set the 1-Wire Net communication speed. // // 'new_speed'-new speed defined as / / MODE_STANDARD 0x00 // MODE_OVERDRIVE 0x01 // // Returns: current 1-Wire Net speed // int OWSpeed ​​(int new_speed) {// set the speed if (new_speed == MODE_OVERDRIVE) c1WS = CONFIG_1WS; else c1WS = FALSE; / / write the new config DS2482_write_config (c1WS | cSPU | cPPM | cAPU); return new_speed;} Example 11. OWSpeed ​​program
Example 12 of OWLevel shows how to use DS2482 to change the 1-Wire bus level. DS2482 can enable strong pull-up after performing a bit or byte communication. Subsequently, the OWLevel program returns the 1-Wire network to the standard pull-up, using OOWriteBytePower or OWReadBitPower operations to enable strong pull-ups.
// ------------------------------------------------ -------------------------- // Set the 1-Wire Net line level pullup to normal. The DS2482 only // allows enabling strong pullup on a bit or byte event. consequent this // function only allows the MODE_STANDARD argument. To enable strong pullup // use OWWriteBytePower or OWReadBitPower. // // 'new_level'-new level defined as // MODE_STANDARD 0x00 // // Returns: current 1-Wire Net level // int OWLevel(int new_level) { // function only will turn back to non-strong pullup if (new_level != MODE_STANDARD) return MODE_STRONG; // clear the strong pullup bit in the global config state cSPU = FALSE; // write the new config DS2482_write_config(c1WS | cSPU | cPPM | cAPU); return MODE_STANDARD; }例12. OWLevel程åº
OWReadBitPower例13是OWReadBitPower所使用的程åºï¼Œç”¨äºŽè¯»å–一个1-Wireä½å¹¶æ‰§è¡Œä¾›ç”µåŠŸèƒ½ã€‚当é…置寄å˜å™¨ä¸çš„强上拉(SPU)ä½ä½¿èƒ½æ—¶ï¼Œåœ¨ä¸‹ä¸€ä½æˆ–å—节通信完æˆåŽï¼ŒDS2482将有æºæ‹‰é«˜1-Wire总线。该æ“作å¯é€šè¿‡æ˜¯å¦èŽ·å¾—相应的å“应æ¥éªŒè¯è¯»ä½çš„æ£ç¡®æ€§ï¼Œå¦‚æžœå“应ä¸æ£ç¡®ï¼Œ1-Wireç”µå¹³å°†è¿”å›žåˆ°æ ‡å‡†çš„ä¸Šæ‹‰çŠ¶æ€ã€‚
//-------------------------------------------------------------------------- // Send 1 bit of communication to the 1-Wire Net and verify that the // response matches the 'applyPowerResponse' bit and apply power delivery // to the 1-Wire net. Note that some implementations may apply the power // first and then turn it off if the response is incorrect. // // 'applyPowerResponse' - 1 bit response to check, if correct then start // power delivery // // Returns: TRUE: bit written and response correct, strong pullup now on // FALSE: response incorrect // int OWReadBitPower(int applyPowerResponse) { unsigned char rdbit; // set strong pullup enable cSPU = CONFIG_SPU; // write the new config if (!DS2482_write_config(c1WS | cSPU | cPPM | cAPU)) return FALSE; // perform read bit rdbit = OWReadBit(); // check if response was correct, if not then turn off strong pullup if (rdbit != applyPowerResponse) { OWLevel(MODE_STANDARD); return FALSE; } return TRUE; }例13. OWReadBitPower程åº
OWWriteBytePower例14为OWWriteBytePower所采用的程åºï¼Œç”¨äºŽå†™å…¥1-Wireå—节并执行强上拉供电功能。当é…置寄å˜å™¨ä¸çš„强上拉(SPU)ä½ä½¿èƒ½æ—¶ï¼Œåœ¨ä¸‹ä¸€ä½æˆ–å—节通信完æˆåŽï¼ŒDS2482将由æºæ‹‰é«˜1-Wire总线。
//-------------------------------------------------------------------------- // Send 8 bits of communication to the 1-Wire Net and verify that the // 8 bits read from the 1-Wire Net are the same (write operation). // The parameter 'sendbyte' least significant 8 bits are used. After the // 8 bits are sent change the level of the 1-Wire net. // // 'sendbyte' - 8 bits to send (least significant bit) // // Returns: TRUE: bytes written and echo was the same, strong pullup now on // FALSE: echo was not the same // int OWWriteBytePower(int sendbyte) { // set strong pullup enable cSPU = CONFIG_SU; // write the new config if (!DS2482_write_config(c1WS | cSPU | cPPM | cAPU)) return FALSE; // perform write byte OWWriteByte(sendbyte); return TRUE; }例14. OWWriteBytePower程åº
结论DS2482å·²ç»è¿‡æµ‹è¯•ï¼Œå¯æˆåŠŸå®ŒæˆI²C接å£è‡³1-Wire网络的转æ¢ã€‚本文给出了基于DS2482 I²C 1-Wire线驱动器的完整的1-Wire接å£æ–¹æ¡ˆï¼Œè¯¥æŽ¥å£é€‚用于所有1-Wire器件,所æ供的例程å¯ä»¥åœ¨ä»»ä½•ä¸€ä¸ªå…·æœ‰I²C通信å£çš„主机上轻æ¾å®žçŽ°ã€‚å¦å¤–还æä¾›å¯ä¾›ä¸‹è½½çš„完整的Cè¯è¨€æ‰§è¡Œç¨‹åºã€‚测试平å°æ˜¯CMAXQUSB评估套件。
Solar Inverter,Efficiency Home Inverter,Solar System Inverter,Solar Pump Inverter
GuangZhou HanFong New Energy Technology Co. , Ltd. , https://www.hfsolarenergy.com