unsigned char oneWire_TouchBit(unsigned char sendbit){ unsigned char status; int poll_count = 0; i2c_start(slaves); i2c_write(slaves, DS2482_I2C_ADDR | I2C_FLAG_WRITE); i2c_write(slaves, DS2482_CMD_1WSB); i2c_write(slaves, sendbit ? 0x80:0x00); i2c_start(slaves); i2c_write(slaves, DS2482_I2C_ADDR | I2C_FLAG_READ); status = i2c_read(slaves, I2C_READ_ACK); do{ status = i2c_read(slaves, status & DS2482_STATUS_1WB); } while ((status & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT)); i2c_read(slaves, I2C_READ_NACK); i2c_stop(slaves); if(poll_count >= POLL_LIMIT){ DS2482_reset(); return false; } // check for single bit result if(status & DS2482_STATUS_SBR) return true; else return false; }
//-------------------------------------------------------------------------- // 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 pull-up (cSPU) = off (0) // Presence pulse masking (cPPM) = off (0) // Active pull-up (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<<1; // which we do not use! // avr-Xinu is using a single DS2482 and a constant address // TWI_SETUP_CMDBLOCK macro will not work with a variable address, // but it would be an easy matter to OR an address into CommandBlock[0] // for multiple DS2482s // reset the DS2482 ON selected address if (!DS2482_reset()) return FALSE; // default configuration c1WS = FALSE; cSPU = FALSE; cPPM = FALSE; cAPU = 0x01; // write the default configuration setup if (!DS2482_write_config(c1WS | cSPU | cPPM | cAPU)) return FALSE; return TRUE; }
int ds2482_write_config(int8 config){ int8 read_config; i2c_start(); i2c_write(DS2482_I2C_ADDR | I2C_FLAG_WRITE); i2c_write(DS2482_CMD_WCFG); I2C_write(config | (~config << 4)); //This makes the ones compliment in the top nibble ***Checked Working*** i2c_start(); i2c_write(DS2482_I2C_ADDR | I2C_FLAG_READ); read_config = i2c_read(0); i2c_stop(); // check for failure due to incorrect read back if (config != read_config){ DS2482_reset(); return false; } return true; }
//-------------------------------------------------------------------------- // 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; struct twi_Command cb1, cb2; uint8_t OWsb[2]; // 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_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 & DS2482_STATUS_1WB); // } // while ((status & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT)); // I2C_stop(); OWsb[0] = DS2482_CMD_1WSB; //1-Wire Single Bit command OWsb[1] = sendbit ? 0x80 : 0x00; cb1.slarw = DS2482_SLAVEADDRESS<<1|TW_WRITE; cb1.data = OWsb; cb1.dataLength = 2; cb1.chain = &cb2; cb2.slarw = DS2482_SLAVEADDRESS<<1|TW_READ; cb2.data = &status; cb2.dataLength = 1; cb2.chain = (struct twi_Command *)0; if ( twi_doCommand(&cb1) == SYSERR ) goto errorReturn; while ( twi_doCommand(&cb2) == OK && (status & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT)) { continue; } // check for failure due to poll limit reached if (poll_count >= POLL_LIMIT) { errorReturn: // handle error // ... DS2482_reset(); return 0; } // return bit state if (status & DS2482_STATUS_SBR) return 1; else return 0; }