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(); }
/******读SD2200实时数据寄存器******/ void I2CReadDate(void) { uchar m,tmp; if(!I2CStart())return; I2CSendByte(0x65,1);//从年开始读取数据 if(!I2CWaitAck()){I2CStop();return;} for(m=0;m<7;m++) { timeBuf[m]=I2CReceiveByte(); if (m!=6) //最后一个数据不应答 { I2CAck(); } } I2CNoAck(); I2CStop(); /* for(m=0;m<SEND_TIME_LEN;m++) { //BCD处理 tmp=timeBuf[m+4]/16; sendTimeBuf[m]=timeBuf[m+4]%16; sendTimeBuf[m]=sendTimeBuf[m]+tmp*10; showTimeBuf[2*m]=timeBuf[m+4]/16; showTimeBuf[2*m+1]=timeBuf[m+4]%16; }*/ //展开处理 因小时需单独处理 12点以上的需减去40 tmp=timeBuf[4]/16; sendTimeBuf[0]=timeBuf[4]%16; sendTimeBuf[0]=sendTimeBuf[0]+tmp*10; if(sendTimeBuf[0]>=40){ sendTimeBuf[0]-=40; } showTimeBuf[0]=sendTimeBuf[0]/10; showTimeBuf[1]=sendTimeBuf[0]%10; tmp=timeBuf[5]/16; sendTimeBuf[1]=timeBuf[5]%16; sendTimeBuf[1]=sendTimeBuf[1]+tmp*10; showTimeBuf[2]=sendTimeBuf[1]/10; showTimeBuf[3]=sendTimeBuf[1]%10; tmp=timeBuf[6]/16; sendTimeBuf[2]=timeBuf[6]%16; sendTimeBuf[2]=sendTimeBuf[2]+tmp*10; showTimeBuf[4]=sendTimeBuf[2]/10; showTimeBuf[5]=sendTimeBuf[2]%10; //年月日 tmp=timeBuf[0]/16; sendTimeBuf[3]=timeBuf[0]%16; sendTimeBuf[3]=sendTimeBuf[3]+tmp*10; tmp=timeBuf[1]/16; sendTimeBuf[4]=timeBuf[1]%16; sendTimeBuf[4]=sendTimeBuf[4]+tmp*10; tmp=timeBuf[2]/16; sendTimeBuf[5]=timeBuf[2]%16; sendTimeBuf[5]=sendTimeBuf[5]+tmp*10; }
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); }
/** * 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 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; }
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; }