unsigned char DS2482WriteConfig(unsigned char config) { unsigned char read_config; I2CStart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Transmitter); I2CWrite(DS2482_CMD_WCFG); I2CWrite(config | (~config << 4)); I2CRestart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Receiver); I2CNotAck(); I2CStop(); read_config = I2CRead(); // check for failure due to incorrect read back if (config != read_config) { DS2482Reset(); return false; } return true; }
void OneWireWriteByte(unsigned char sendbyte) { unsigned char status; int poll_count = 0; I2CStart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Transmitter); I2CWrite(DS2482_CMD_1WWB); I2CWrite(sendbyte); I2CRestart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Receiver); I2CAck(); do { status = I2CRead(); } while ((status & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT)); I2CNotAck(); I2CStop(); status = I2CRead(); if (poll_count >= POLL_LIMIT) DS2482Reset(); }
unsigned char DS2482ChannelSelect(unsigned char channel) { unsigned char ch, ch_read, read_channel; switch (channel) { default: case 0: ch = DS2482_CH_IO0; ch_read = DS2482_RCH_IO0; break; case 1: ch = DS2482_CH_IO1; ch_read = DS2482_RCH_IO1; break; case 2: ch = DS2482_CH_IO2; ch_read = DS2482_RCH_IO2; break; case 3: ch = DS2482_CH_IO3; ch_read = DS2482_RCH_IO3; break; case 4: ch = DS2482_CH_IO4; ch_read = DS2482_RCH_IO4; break; case 5: ch = DS2482_CH_IO5; ch_read = DS2482_RCH_IO5; break; case 6: ch = DS2482_CH_IO6; ch_read = DS2482_RCH_IO6; break; case 7: ch = DS2482_CH_IO7; ch_read = DS2482_RCH_IO7; break; }; I2CStart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Transmitter); I2CWrite(DS2482_CMD_CHSL); I2CWrite(ch); I2CRestart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Receiver); I2CNotAck(); I2CStop(); read_channel = I2CRead(); return (read_channel == ch_read); }
void main() { /* Buffer where we will read/write our data */ unsigned char I2CData[] = {0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x09, 0x00}; unsigned char i; /* Initialize I2C Port */ I2CInit(); /* Send Start condition */ I2CStart(); /* Send DS1307 slave address with write operation */ I2CSend(0xD0); /* Send subaddress 0x00, we are writing to this location */ I2CSend(0x00); /* Loop to write 8 bytes */ for (i = 0; i < 8; i++) { /* send I2C data one by one */ //I2CSend(I2CInitval[i]); I2CSend(I2CData[i]); } /* Send a stop condition - as transfer finishes */ I2CStop(); /* We will now read data from DS1307 */ /* Reading for a memory based device always starts with a dummy write */ /* Send a start condition */ I2CStart(); /* Send slave address with write */ I2CSend(0xD0); /* Send address for dummy write operation */ /* this address is actually where we are going to read from */ I2CSend(0x00); /* Send a repeated start, after a dummy write to start reading */ I2CRestart(); /* send slave address with read bit set */ I2CSend(0xD1); /* Loop to read 8 bytes from I2C slave */ for (i = 8; i > 0; i--) { /* read a byte */ I2CData[i] = I2CRead(); /* ACK if its not the last byte to read */ /* if its the last byte then send a NAK */ if (i - 1) I2CAck(); else I2CNak(); } /* Send stop */ I2CStop(); /* end of program */ while(1); }
unsigned char OneWireReadByte(void) { unsigned char data, status; int poll_count = 0; I2CStart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Transmitter); I2CWrite(DS2482_CMD_1WRB); I2CRestart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Receiver); I2CAck(); do { status = I2CRead(); } while ((status & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT)); I2CNotAck(); I2CStop(); I2CRead(); if (poll_count >= POLL_LIMIT) { DS2482Reset(); return false; } I2CStart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Transmitter); I2CWrite(DS2482_CMD_SRP); I2CWrite(DS2482_READPTR_RDR); I2CRestart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Receiver); I2CNotAck(); I2CStop(); data = I2CRead(); return data; }
BYTE read_register(BYTE address,BYTE regist) { I2CStart(); I2CWrite(address | 0); I2CWrite(regist); I2CRestart(); I2CWrite(address | 1); BYTE data = I2CRead(1); I2CStop(); I2CStop(); I2CStop(); return data; }
void I2CDataCom(unsigned char read_write,unsigned char chipAdr,unsigned char* regAdr,unsigned char* data,unsigned char nbData,unsigned char nbReg) { /*Standard polling I2C function for read and write*/ unsigned char i; I2CStart(); //Send adress of MPU shifted to 1 to the left in order to send Write command if(I2CTransfer(chipAdr)) { I2CStop(); return; } //send address register for(i=0;i<nbReg;i++) { if(I2CTransfer(regAdr[i])) { I2CStop(); return; } } if(read_write) { //If a read request is needed //send restart command I2CRestart(); //resend the address of MPU and add READ command if(I2CTransfer(chipAdr|I2C_READ)) { I2CStop(); return; } for(i=0;i<nbData;i++) { data[i] = I2CReceive(); if(i == nbData-1) { //Generate ACK for all datas exept the last I2CACKDis(); //send a nack I2CAckGen(); //generate ACK procedure } else { //Generate NACK for the last value I2CACKEn(); I2CAckGen(); } } } else { for(i=0;i<nbData;i++) //send datas if(I2CTransfer(data[i])) break; //testing if acknowledge has been received } //send STOP I2CStop(); }
unsigned char DS2482Reset(void) { unsigned char status; I2CStart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Transmitter); I2CWrite(DS2482_CMD_DRST); I2CRestart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Receiver); I2CNotAck(); I2CStop(); status = I2CRead(); // check for failure due to incorrect read back of status return ((status & 0xf7) == 0x10); }
/** * 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. * * @param search_direction * @return The DS2482 status byte result from the triplet command */ unsigned char DS2482SearchTriplet(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 I2CStart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Transmitter); I2CWrite(DS2482_CMD_1WT); I2CWrite(search_direction ? 0x80 : 0x00); I2CRestart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Receiver); I2CAck(); do { status = I2CRead(); } while ((status & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT)); I2CNotAck(); I2CStop(); I2CRead(); // check for failure due to poll limit reached if (poll_count >= POLL_LIMIT) { DS2482Reset(); return false; } // return status byte return status; }
unsigned char OneWireTouchBit(unsigned char sendbit) { unsigned char status; int poll_count = 0; I2CStart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Transmitter); I2CWrite(DS2482_CMD_1WSB); I2CWrite(sendbit ? 0x80 : 0x00); I2CRestart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Receiver); I2CAck(); do { status = I2CRead(); } while ((status & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT)); I2CNotAck(); I2CStop(); status = I2CRead(); if (poll_count >= POLL_LIMIT) { DS2482Reset(); return false; } // check for single bit result if (status & DS2482_STATUS_SBR) return true; else return false; }
unsigned char OneWireReset(void) { unsigned char status = 0; int poll_count = 0; I2CStart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Transmitter); I2CWrite(DS2482_CMD_1WRS); I2CRestart(); I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Receiver); I2CAck(); do { status = I2CRead(); } while ((status & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT)); I2CNotAck(); I2CStop(); status = I2CRead(); if (poll_count >= POLL_LIMIT) { DS2482Reset(); return false; } // check for presence detect if (status & DS2482_STATUS_PPD) return true; else return false; }