通信设计应用
Operation | Description | Implementation |
Write 1 bit | Send a '1' bit to the 1-Wire slaves (Write 1 time slot) | Drive bus low, delay A Release bus, delay B |
Write 0 bit | send a '0' bit to the 1-Wire slaves (Write 0 time slot) | Drive bus low, delay C Release bus, delay D |
Read bit | Read a bit from the 1-Wire slaves (Read time slot) | Drive bus low, delay A Release bus, delay E Sample bus to read bit from slave Delay F |
Reset | Reset the 1-Wire bus slave devices and ready them for a command | Delay G Drive bus low, delay H Release bus, delay I Sample bus, 0 = device(s) present, 1 = no device present Delay J |
Parameter | Speed | Min (µs) | Recommended (µs) | Max (µs) | Notes |
A | Standard | 5 | 6 | 15 | 1, 2 |
Overdrive | 1 | 1.5 | 1.85 | 1, 3 | |
B | Standard | 59 | 64 | N/A | 2, 4 |
Overdrive | 7.5 | 7.5 | N/A | 3, 4 | |
C | Standard | 60 | 60 | 120 | 2, 5 |
Overdrive | 7 | 7.5 | 14 | 3, 5 | |
D | Standard | 8 | 10 | N/A | 2, 6 |
Overdrive | 2.5 | 2.5 | N/A | 3, 6 | |
E | Standard | 5 | 9 | 12 | 2, 7, 8 |
Overdrive | 0.5 | 0.75 | 0.85 | 3, 7, 8 | |
F | Standard | 50 | 55 | N/A | 2, 9 |
Overdrive | 6.75 | 7 | N/A | 3, 9 | |
G | Standard | 0 | 0 | 0 | |
Overdrive | 2.5 | 2.5 | N/A | 3, 14 | |
H | Standard | 480 | 480 | 640 | 2, 10, 15 |
Overdrive | 68 | 70 | 80 | 3, 10 | |
I | Standard | 63 | 70 | 78 | 2, 11 |
Overdrive | 7.2 | 8.5 | 8.8 | 3, 11 | |
J | Standard | 410 | 410 | N/A | 2, 12, 13 |
Overdrive | 39.5 | 40 | N/A | 3, 12 |
// send 'databyte' to 'port' int outp(unsigned port, int databyte); // read byte from 'port' int inp(unsigned port);代码中的常量PORTADDRESS (图3)用来定义通信端口的地址。这里我们假定使用通信端口的第0位控制1-Wire总线。设定该位为1,将使1-Wire总线变为低电平;设定该位为0,1-Wire总线将被释放,此时1-Wire总线被电阻上拉,或被1-Wire从器件下拉。
// Pause for exactly 'tick' number of ticks = 0.25us void tickDelay(int tick); // Implementation is platform specific // 'tick' values int A,B,C,D,E,F,G,H,I,J; //----------------------------------------------------------------------------- // Set the 1-Wire timing to 'standard' (standard=1) or 'overdrive' (standard=0). // void SetSpeed(int standard) { // Adjust tick values depending on speed if (standard) { // Standard Speed A = 6 * 4; B = 64 * 4; C = 60 * 4; D = 10 * 4; E = 9 * 4; F = 55 * 4; G = 0; H = 480 * 4; I = 70 * 4; J = 410 * 4; } else { // Overdrive Speed A = 1.5 * 4; B = 7.5 * 4; C = 7.5 * 4; D = 2.5 * 4; E = 0.75 * 4; F = 7 * 4; G = 2.5 * 4; H = 70 * 4; I = 8.5 * 4; J = 40 * 4; } }1-Wire基本操作的代码程序如实例2所示。
//----------------------------------------------------------------------------- // Generate a 1-Wire reset, return 1 if no presence detect was found, // return 0 otherwise. // (NOTE: Does not handle alarm presence from DS2404/DS1994) // int OWTouchReset(void) { int result; tickDelay(G); outp(PORTADDRESS,0x00); // Drives DQ low tickDelay(H); outp(PORTADDRESS,0x01); // Releases the bus tickDelay(I); result = inp(PORTADDRESS) & 0x01; // Sample for presence pulse from slave tickDelay(J); // Complete the reset sequence recovery return result; // Return sample presence pulse result } //----------------------------------------------------------------------------- // Send a 1-Wire write bit. Provide 10us recovery time. // void OWWriteBit(int bit) { if (bit) { // Write '1' bit outp(PORTADDRESS,0x00); // Drives DQ low tickDelay(A); outp(PORTADDRESS,0x01); // Releases the bus tickDelay(B); // Complete the time slot and 10us recovery } else { // Write '0' bit outp(PORTADDRESS,0x00); // Drives DQ low tickDelay(C); outp(PORTADDRESS,0x01); // Releases the bus tickDelay(D); } } //----------------------------------------------------------------------------- // Read a bit from the 1-Wire bus and return it. Provide 10us recovery time. // int OWReadBit(void) { int result; outp(PORTADDRESS,0x00); // Drives DQ low tickDelay(A); outp(PORTADDRESS,0x01); // Releases the bus tickDelay(E); result = inp(PORTADDRESS) & 0x01; // Sample the bit value from the slave tickDelay(F); // Complete the time slot and 10us recovery return result; }该程序包括了1-Wire总线的所有位操作,通过调用该程序可以构成以字节为处理对象的函数,见实例3。
//----------------------------------------------------------------------------- // Write 1-Wire data byte // void OWWriteByte(int data) { int loop; // Loop to write each bit in the byte, LS-bit first for (loop = 0; loop < 8; loop++) { OWWriteBit(data & 0x01); // shift the data byte for the next bit data >>= 1; } } //----------------------------------------------------------------------------- // Read 1-Wire data byte and return it // int OWReadByte(void) { int loop, result=0; for (loop = 0; loop < 8; loop++) { // shift the result to get it ready for the next bit result >>= 1; // if result is one, then set MS bit if (OWReadBit()) result |= 0x80; } return result; } //----------------------------------------------------------------------------- // Write a 1-Wire data byte and return the sampled result. // int OWTouchByte(int data) { int loop, result=0; for (loop = 0; loop < 8; loop++) { // shift the result to get it ready for the next bit result >>= 1; // If sending a '1' then read a bit else write a '0' if (data & 0x01) { if (OWReadBit()) result |= 0x80; } else OWWriteBit(0); // shift the data byte for the next bit data >>= 1; } return result; } //----------------------------------------------------------------------------- // Write a block 1-Wire data bytes and return the sampled result in the same // buffer. // void OWBlock(unsigned char *data, int data_len) { int loop; for (loop = 0; loop < data_len; loop++) { data[loop] = OWTouchByte(data[loop]); } } //----------------------------------------------------------------------------- // Set all devices on 1-Wire to overdrive speed. Return '1' if at least one // overdrive capable device is detected. // int OWOverdriveSkip(unsigned char *data, int data_len) { // set the speed to 'standard' SetSpeed(1); // reset all devices if (OWTouchReset()) // Reset the 1-Wire bus return 0; // Return if no devices found // overdrive skip command OWWriteByte(0x3C); // set the speed to 'overdrive' SetSpeed(0); // do a 1-Wire reset in 'overdrive' and return presence result return OWTouchReset(); }OWTouchByte函数可以同时完成读写1-Wire总线数据,通过该函数可以实现数据块的读写。在一些平台上执行效率更高, Maxim提供的API就采用了这种函数。通过OWTouchByte函数,OWBlock函数简化了1-Wire总线的数据块发送和接收。注意:OWTouchByte(0xFF)与OWReadByte()等效,OWTouchByte(data)与OWWriteByte(data)等效。
//----------------------------------------------------------------------------- // Read and return the page data and SHA-1 message authentication code (MAC) // from a DS2432. // int ReadPageMAC(int page, unsigned char *page_data, unsigned char *mac) { int i; unsigned short data_crc16, mac_crc16; // set the speed to 'standard' SetSpeed(1); // select the device if (OWTouchReset()) // Reset the 1-Wire bus return 0; // Return if no devices found OWWriteByte(0xCC); // Send Skip ROM command to select single device // read the page OWWriteByte(0xA5); // Read Authentication command OWWriteByte((page << 5) & 0xFF); // TA1 OWWriteByte(0); // TA2 (always zero for DS2432) // read the page data for (i = 0; i < 32; i++) page_data[i] = OWReadByte(); OWWriteByte(0xFF); // read the CRC16 of command, address, and data data_crc16 = OWReadByte(); data_crc16 |= (OWReadByte() << 8); // delay 2ms for the device MAC computation // read the MAC for (i = 0; i < 20; i++) mac[i] = OWReadByte(); // read CRC16 of the MAC mac_crc16 = OWReadByte(); mac_crc16 |= (OWReadByte() << 8); // check CRC16... return 1; }
全部0条评论
快来发表一下你的评论吧 !