//-------------------------------------------------------------------------- // Send 8 bits of communication to the 1-Wire Net and verify that the // 8 bits read from the 1-Wire Net is 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. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // 'sendbyte' - 8 bits to send (least significant byte) // // Returns: TRUE: bytes written and echo was the same // FALSE: echo was not the same // SMALLINT owWriteBytePower(int portnum, SMALLINT sendbyte) { if(owTouchByte(portnum,sendbyte) != sendbyte) return FALSE; if(owLevel(portnum,MODE_STRONG5) != MODE_STRONG5) return FALSE; return TRUE; }
//-------------------------------------------------------------------------- // Read 8 bits of communication to the 1-Wire Net and then turn on // power delivery. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // // Returns: byte read // FALSE: power delivery failed // SMALLINT owReadBytePower(int portnum) { SMALLINT getbyte; if (!owHasPowerDelivery(portnum)) return FALSE; getbyte = owTouchByte(portnum,0xFF); if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5) return FALSE; return getbyte; }
//---------------------------------------------------------------------- // Read the temperature of a DS1920/DS1820 // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // 'SerialNum' - Serial Number of DS1920/DS1820 to read temperature from // 'Temp ' - pointer to variable where that temperature will be // returned // // Returns: TRUE(1) temperature has been read and verified // FALSE(0) could not read the temperature, perhaps device is not // in contact // int ReadTemperature(int portnum, uchar *SerialNum, float *Temp) { int rt=FALSE; uchar send_block[30],lastcrc8; int send_cnt=0, tsht, i, loop=0; float tmp,cr,cpc; setcrc8(portnum,0); // set the device serial number to the counter device owSerialNum(portnum,SerialNum,FALSE); for (loop = 0; rt==FALSE && loop < 2; loop ++) { // access the device if (owAccess(portnum)) { // send the convert temperature command owTouchByte(portnum,0x44); // set the 1-Wire Net to strong pull-up if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5) return FALSE; // sleep for 1 second msDelay(1000); // turn off the 1-Wire Net strong pull-up if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL) return FALSE; // access the device if (owAccess(portnum)) { // create a block to send that reads the temperature // read scratchpad command send_block[send_cnt++] = 0xBE; // now add the read bytes for data bytes and crc8 for (i = 0; i < 9; i++) send_block[send_cnt++] = 0xFF; // now send the block if (owBlock(portnum,FALSE,send_block,send_cnt)) { // perform the CRC8 on the last 8 bytes of packet for (i = send_cnt - 9; i < send_cnt; i++) lastcrc8 = docrc8(portnum,send_block[i]); // verify CRC8 is correct if (lastcrc8 == 0x00) { // calculate the high-res temperature tsht = send_block[1]/2; if (send_block[2] & 0x01) tsht |= -128; tmp = (float)(tsht); cr = send_block[7]; cpc = send_block[8]; if (((cpc - cr) == 1) && (loop == 0)) continue; if (cpc == 0) return FALSE; else tmp = tmp - (float)0.25 + (cpc - cr)/cpc; *Temp = tmp; // success rt = TRUE; } } } } } // return the result flag rt return rt; }
//-------------------------------------------------------------------------- // Send 8 bits of read communication to the 1-Wire Net and and return the // result 8 bits read from the 1-Wire Net. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // // Returns: 8 bits read from 1-Wire Net // SMALLINT owReadByte(int portnum) { return owTouchByte(portnum,(SMALLINT)0xFF); }
//-------------------------------------------------------------------------- // Send 8 bits of communication to the 1-Wire Net and verify that the // 8 bits read from the 1-Wire Net is the same (write operation). // The parameter 'sendbyte' least significant 8 bits are used. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // 'sendbyte' - 8 bits to send (least significant byte) // // Returns: TRUE: bytes written and echo was the same // FALSE: echo was not the same // SMALLINT owWriteByte(int portnum, SMALLINT sendbyte) { return (owTouchByte(portnum,sendbyte) == (0xff & sendbyte)) ? TRUE : FALSE; }
//---------------------------------------------------------------------- // Main Test // int main() //short argc, char **argv) { int PortNum=1,rslt,i,j,testcnt=0,length; uchar TempSerialNum[8]; uchar tran_buffer[2000], filename[10]; char return_msg[128]; int portnum=0; // check for required port name if (argc != 2) { printf("1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"1\" (Win32 TMEX)\n"); exit(1); } // attempt to acquire the 1-Wire Net if (!owAcquire(portnum, argv[1], return_msg)) { printf("%s",return_msg); exit(1); } // success printf("%s",return_msg); //---------------------------------------- // Introduction printf("\n/---------------------------------------------\n"); printf(" The following is a test excersize of the\n" " 1-Wire Net public domain library Version 2.00.\n\n" " This test was run using with 2 DS1920's (DS1820),\n" " 1 DS1971 (DS2430), and 1 DS1996.\n\n"); //---------------------------------------- // First the devices on the 1-Wire Net printf("\n/---------------------------------------------\n"); printf("TEST%d: Searching for devices on 1-Wire Net\n",testcnt++); // find the first device (all devices not just alarming) rslt = owFirst(portnum,TRUE, FALSE); while (rslt) { // print the Serial Number of the device just found PrintSerialNum(portnum); // find the next device rslt = owNext(portnum,TRUE, FALSE); } //---------------------------------------- // now search for the part with a 0x0C family code (DS1996) printf("\n/---------------------------------------------\n"); printf("TEST%d: Set to find first device with 0x0C family code\n",testcnt++); owFamilySearchSetup(portnum,0x0C); // find the first 0x0c device TempSerialNum[0]=0; while (TempSerialNum[0]!=0x0c && owNext(portnum,TRUE,FALSE)) { owSerialNum(portnum,TempSerialNum,TRUE); } printf("search result %d\n",TempSerialNum[0]==0x0c); // print the Serial Number of the device just found PrintSerialNum(portnum); //---------------------------------------- // Access a device and read ram printf("\n/---------------------------------------------\n"); printf("TEST%d: Access the current device and read ram\n",testcnt++); printf("owAccess %d\n",owAccess(portnum)); printf("Read Ram 0xF0: %02X\n",owTouchByte(portnum,0xF0)); printf("Address0 0x00: %02X\n",owTouchByte(portnum,0x00)); printf("Address1 0x00: %02X\n",owTouchByte(portnum,0x00)); printf("Page 0: "); for (i = 0; i < 32; i++) printf("%02X ",owTouchByte(portnum,0xFF)); printf("\n"); //---------------------------------------- // Read ram with owBlock printf("\n/---------------------------------------------\n"); printf("TEST%d: Read ram with owBlock\n",testcnt++); for (i = 0; i < 32; i++) tran_buffer[i] = 0xFF; printf("owBlock %d\n",owBlock(portnum,FALSE,tran_buffer,32)); printf("Page 1: "); for (i = 0; i < 32; i++) printf("%02X ",tran_buffer[i]); printf("\n"); //---------------------------------------- // Write a packet in each page of DS1996 printf("\n/---------------------------------------------\n"); printf("TEST%d: Place the DS1996 into overdrive\n",testcnt++); printf("owOverdriveAccess %d\n",owOverdriveAccess(portnum)); //---------------------------------------- // Write 4 packets with owWritePacketStd printf("\n/---------------------------------------------\n"); printf("TEST%d: Write 4 packets with owWritePacketStd\n",testcnt++); for (j = 0; j < 4; j++) { for (i = 0; i < 29; i++) tran_buffer[i] = (uchar)i + j; printf("Write page %d: %d\n",j,owWritePacketStd(portnum,j,tran_buffer,29,FALSE,FALSE)); for (i = 0; i < 29; i++) tran_buffer[i] = 0; length = owReadPacketStd(portnum,TRUE,j,tran_buffer); printf("Read page %d: %d\n",j,length); for (i = 0; i < length; i++) printf("%02X",tran_buffer[i]); printf("\n"); } //---------------------------------------- // Write a file to DS1996 printf("\n/---------------------------------------------\n"); printf("TEST%d: Format and write a file (in overdrive)\n",testcnt++); sprintf(filename,"DEMO"); // set the data to write for (i = 0; i < 2000; i++) tran_buffer[i] = i % 255; printf("Format and write file DEMO.000 %d\n", owFormatWriteFile(portnum,filename,2000,tran_buffer)); // clear the buffer for (i = 0; i < 2000; i++) tran_buffer[i] = 0x55; printf("Read file DEMO.000 %d\n",owReadFile(portnum,filename,tran_buffer)); // print the data result for (i = 0; i < 2000; i++) { if ((i % 0x20) == 0) printf("\n%03X ",i); printf("%02X",tran_buffer[i]); } printf("\n"); //---------------------------------------- // Turn off overdrive printf("\n/---------------------------------------------\n"); printf("TEST%d: Turn off overdrive\n",testcnt++); printf("Set 1-Wire Net speed to normal %d\n",owSpeed(portnum,MODE_NORMAL)); //---------------------------------------- // Verify a device printf("\n/---------------------------------------------\n"); printf("TEST%d: Verify the current device\n",testcnt++); printf("owVerify (normal) %d\n",owVerify(portnum,FALSE)); printf("owVerify (alarm) %d\n",owVerify(portnum,TRUE)); //---------------------------------------- // Skip the first family code found printf("\n/---------------------------------------------\n"); printf("TEST%d: Skip the first family code found\n",testcnt++); // find the next device printf("search result of owFirst %d\n",owFirst(portnum,TRUE, FALSE)); // print the Serial Number of the device just found PrintSerialNum(portnum); // skip the first family type found owSkipFamily(portnum); printf("owSkipFamily called\n"); // find the next device printf("search result of owNext %d\n",owNext(portnum,TRUE, FALSE)); // print the Serial Number of the device just found PrintSerialNum(portnum); //---------------------------------------- // Find first family code (DS1920) and read temperature printf("\n/---------------------------------------------\n"); printf("TEST%d: Find first family code (DS1920) and read temperature\n",testcnt++); // find the next device printf("search result of owFirst %d\n",owFirst(portnum,TRUE, FALSE)); // print the Serial Number of the device just found PrintSerialNum(portnum); // send the convert temperature command printf("Convert temperature command %02X\n",owTouchByte(portnum,0x44)); // set the 1-Wire Net to strong pull-up printf("Set power delivery %d\n",owLevel(portnum,MODE_STRONG5)); // sleep for 1 second msDelay(1000); // turn off the 1-Wire Net strong pull-up printf("Disable power delivery %d\n",owLevel(portnum,MODE_NORMAL)); // read the DS1920 temperature value printf("Access the DS1920 %d\n",owAccess(portnum)); tran_buffer[0] = 0xBE; tran_buffer[1] = 0xFF; tran_buffer[2] = 0xFF; printf("Block to read temperature %d\n",owBlock(portnum,FALSE,tran_buffer,3)); // interpret the result printf("result: DS1920 temperature read: %d C\n", (tran_buffer[1] | ((int)tran_buffer[2] << 8)) / 2); //---------------------------------------- // Verify the current device, could also be alarming printf("\n/---------------------------------------------\n"); printf("TEST%d: Verify the current device, could also be alarming\n",testcnt++); printf("owVerify (normal) %d\n",owVerify(portnum,FALSE)); printf("owVerify (alarm) %d\n",owVerify(portnum,TRUE)); //---------------------------------------- // Test setting the Serial Number with owSerialNum printf("\n/---------------------------------------------\n"); printf("TEST%d: Test setting the Serial Number with owSerialNum\n",testcnt++); // set the Serial Num to 0 to 7 for (i = 0; i < 8; i++) TempSerialNum[i] = (uchar)i; owSerialNum(portnum,TempSerialNum,FALSE); // read back the Serial Number PrintSerialNum(portnum); //---------------------------------------- // Verify the current device (should fail, no such device) printf("\n/---------------------------------------------\n"); printf("TEST%d: Verify the current device (should fail, no such device)\n",testcnt++); printf("owVerify (normal) %d\n",owVerify(portnum,FALSE)); printf("owVerify (alarm) %d\n",owVerify(portnum,TRUE)); // release the 1-Wire Net owRelease(portnum,return_msg); printf("%s",return_msg); exit(0); return 0; }
//---------------------------------------------------------------------- // Writes specific bits to the control register of the 1-Wire // clock. Any of the 8 control register bits can be written. // // Note: If specific bits are desired to be left alone, place values // other than TRUE (1) or FALSE (0) in them (such as -99) // when calling the function. // // Parameters: // portnum The port number of the port being used for the // 1-Wire network. // SNum The 1-Wire address of the device with which to communicate. // dsel Delay select bit // stopstart STOP/START (in Manual Mode). // automan Automatic/Manual Mode. // osc Oscillator enable (start the clock ticking...) // ro Read only (during expiration: 1 for read-only, 0 for destruct). // wpc Write-Protect cycle counter bit. // wpi Write-Protect interval timer bit. // wpr Write-Protect RTC and clock alarm bit. // // Returns: TRUE if the write worked. // FALSE if there was an error in writing the status register. // // SMALLINT setControlRegister(int portnum, uchar * SNum, SMALLINT dsel, SMALLINT startstop, SMALLINT automan, SMALLINT osc, SMALLINT ro, SMALLINT wpc, SMALLINT wpi, SMALLINT wpr) { uchar controlregister[2]; // to store both status and control bytes SMALLINT writetopart = FALSE; SMALLINT writeprotect = FALSE; int i = 0; controlregister[0] = 0; controlregister[1] = 0; // read 1 bytes from 1-Wire clock device (memory bank 2) // starting at address 0x01. This is the control register. if (!readNV(2, portnum, SNum, 0x00, FALSE, &controlregister[0], 2)) { return FALSE; } // if necessary, update control register if (((controlregister[1] & 0x80) > 0) && (dsel == FALSE)) // turn dsel to 0 { // The dsel bit is in 7th bit position (MSb) of byte. // 'And'ing with 0x7F (127 decimal or 01111111 binary) will // guarantee a 0-bit in the position. controlregister[1] = controlregister[1] & 0x7F; writetopart = TRUE; } if (((controlregister[1] & 0x80) == 0) && (dsel == TRUE)) // turn dsel to 1 { // The dsel bit is in 7th bit position (MSb) of byte. // 'Or'ing with 0x80 (16) will guarantee a 1-bit in the position and // turn the dsel bit to 1. controlregister[1] = controlregister[1] | 0x80; writetopart = TRUE; } if (((controlregister[1] & 0x40) > 0) && (startstop == FALSE)) // turn startstop to 0 { // The startstop bit is in the 6th bit position of the control register. // 'And'ing with 0xBF (191 decimal or 10111111 binary) will // guarantee a 0-bit in the position. controlregister[1] = controlregister[1] & 0xBF; writetopart = TRUE; } if (((controlregister[1] & 0x40) == 0) && (startstop == TRUE)) // turn startstop to 1 { // The startstop bit is in the 6th bit position of the control register. // 'Or'ing with 0x40 (64) will guarantee a 1-bit in the position. controlregister[1] = controlregister[1] | 0x40; writetopart = TRUE; } if (((controlregister[1] & 0x20) > 0) && (automan == FALSE)) // turn automan to 0 { // The automan bit is in the 5th bit position of the control register. // 'And'ing with 0xDF (223 decimal or 11011111 binary) will // guarantee a 0-bit in the position. controlregister[1] = controlregister[1] & 0xDF; writetopart = TRUE; } if (((controlregister[1] & 0x20) == 0) && (automan == TRUE)) // turn automan to 1 { // The automan bit is in the 5th bit position of the control register. // 'Or'ing with 0x20 (32) will guarantee a 1-bit in the position. controlregister[1] = controlregister[1] | 0x20; writetopart = TRUE; } if (((controlregister[1] & 0x10) > 0) && (osc == FALSE)) // turn oscillator off { // The oscillator enable bit is in 4th bit position (16) of byte. // 'And'ing with 0xEF (239 decimal or 11101111 binary) will // guarantee a 0-bit in the position and turn the oscillator off. controlregister[1] = controlregister[1] & 0xEF; writetopart = TRUE; } if (((controlregister[1] & 0x10) == 0) && (osc == TRUE)) // turn oscillator on { // The oscillator enable bit is in 4th bit position (16) of byte. // 'Or'ing with 0x10 (16) will guarantee a 1-bit in the position and // turn the oscillator on. controlregister[1] = controlregister[1] | 0x10; writetopart = TRUE; } if (((controlregister[1] & 0x08) > 0) && (ro == FALSE)) // turn ro to 0 { // The ro bit is in the 3rd bit position of the control register. // 'And'ing with 0xF7 (247 decimal or 11110111 binary) will // guarantee a 0-bit in the position. controlregister[1] = controlregister[1] & 0xF7; writetopart = TRUE; } if (((controlregister[1] & 0x08) == 0) && (ro == TRUE)) // turn ro to 1 { // The ro bit is in the 3rd bit position of the control register. // 'Or'ing with 0x08 will guarantee a 1-bit in the position. controlregister[1] = controlregister[1] | 0x08; writetopart = TRUE; } if (((controlregister[1] & 0x04) > 0) && (wpc == FALSE)) // turn wpc to 0 { // The wpc bit is in the 2nd bit position of the control register. // 'And'ing with 0xFB (251 decimal or 11111011 binary) will // guarantee a 0-bit in the position. controlregister[1] = controlregister[1] & 0xFB; writeprotect = TRUE; writetopart = TRUE; } if (((controlregister[1] & 0x04) == 0) && (wpc == TRUE)) // turn wpc to 1 { // The wpc bit is in the 2nd bit position of the control register. // 'Or'ing with 0x04 will guarantee a 1-bit in the position. controlregister[1] = controlregister[1] | 0x04; writeprotect = TRUE; writetopart = TRUE; } if (((controlregister[1] & 0x02) > 0) && (wpi == FALSE)) // turn wpi to 0 { // The wpi bit is in the 1st bit position of the control register. // 'And'ing with 0xFD (253 decimal or 11111101 binary) will // guarantee a 0-bit in the position. controlregister[1] = controlregister[1] & 0xFD; writeprotect = TRUE; writetopart = TRUE; } if (((controlregister[1] & 0x02) == 0) && (wpi == TRUE)) // turn wpi to 1 { // The wpi bit is in the 1st bit position of the control register. // 'Or'ing with 0x02 will guarantee a 1-bit in the position. controlregister[1] = controlregister[1] | 0x02; writeprotect = TRUE; writetopart = TRUE; } if (((controlregister[1] & 0x01) > 0) && (wpr == FALSE)) // turn wpr to 0 { // The wpr bit is in the 0th bit position of the control register. // 'And'ing with 0xFE (254 decimal or 11111110 binary) will // guarantee a 0-bit in the position. controlregister[1] = controlregister[1] & 0xFE; writeprotect = TRUE; writetopart = TRUE; } if (((controlregister[1] & 0x01) == 0) && (wpr == TRUE)) // turn wpr to 1 { // The wpr bit is in the 0th bit position of the control register. // 'Or'ing with 0x01 will guarantee a 1-bit in the position. controlregister[1] = controlregister[1] | 0x01; writeprotect = TRUE; writetopart = TRUE; } // write new control register (if any) to part. if (writetopart == TRUE) { // if writeprotect is true, then copyscratchpad 3 times... if (writeprotect == TRUE) { owSerialNum(portnum, SNum, 1); // write to scratchpad if (!writeScratchpd(portnum, 0x200, &controlregister[0], 2)) { return FALSE; } // copy the scratchpad 3 times to write protect for (i = 0; i < 3; i++) { if (!owAccess(portnum)) return FALSE; // if Access is unsuccessful if(!((0x55 != (uchar)owTouchByte(portnum,0x55)) && // sent the copy command (0x00 != (uchar)owTouchByte(portnum, 0x00)) && // write the target address 1 (0x02 != (uchar)owTouchByte(portnum, 0x02)))) // write the target address 2 { return FALSE; } if (i == 0) { if(!owTouchByte(portnum,0x01)) return FALSE; } else { if(! owTouchByte(portnum, 0x81)) // The AA bit gets set on consecutive return FALSE; // copyscratchpads (pg. 12 of datasheet) } } } else { if (!writeNV(2, portnum, SNum, 0x00, &controlregister[0], 2)) { return FALSE; } } } return TRUE; }