void RomReadCode(uchar RomCode[]) { uchar i; for(i=0;i<8;i++) { RomCode[i]=owReadByte(0); // printf("text%d\n",i); } }
//---------------------------------------------------------------------- // This is the Main routine for swtmain1c // int main(short argc, char **argv) { char msg[200]; uchar data[256]; int portnum = 0; int n=0; int addr = 0; int len; uchar es = 0x00; uchar address[2]; ushort i; uchar sn[8],state[3], reg[3], send_block[37]; uchar family[2][8]; int done=FALSE; int channel=0,send_cnt=0; SMALLINT alternate=TRUE, alt=FALSE; ushort lastcrc16; uchar latch, set; uchar check; // check for required port name if (argc != 2) { sprintf(msg,"1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"1\" (Win32 TMEX)\n"); printf("%s",msg); return 0; } if((portnum = owAcquireEx(argv[1])) < 0) { printf("Did not Acquire port.\n",1); exit(1); } else { if(FindDevices(portnum,&family[0],0x1C,1)) { for(i=0;i<8;i++) sn[i] = family[0][i]; printf("device found: "); for(i=0;i<8;i++) printf("%02X ",sn[i]); printf("\n"); do { printf("PICK AN OPERATION:\n\n"); printf("(1) Read Channel state\n"); // Gives channel information printf("(2) Set Channel On/Off\n"); // Sets channel printf("(3) Read Channel Mask\n"); // Read Selection Mask printf("(4) Set Channel mask\n"); // Sets channel mask printf("(5) Read Channel Polarity\n"); // Read Polarity Selection printf("(6) Set Channel polarity\n"); // sets channel polarity printf("(7) Read Control/Status Register\n"); // Read Control/Status Reg printf("(8) Set Reset Mode On/Off\n"); // Set Reset Mode printf("(9) Clear Power on Reset\n"); // Clear Power on reset printf("(10) Get VCC state\n"); // Get VCC state printf("(11) Set OR conditional search\n"); // or condition search printf("(12) Set AND conditional search\n"); // and condition search printf("(13) Write Scratchpad\n"); // write scratchpad command printf("(14) Read Scratchpad\n"); // read scratchpad command printf("(15) Copy Scratchpad\n"); // copy scratchpad command printf("(16) Read Memory\n"); // read memory printf("(17) PIO access read with CRC confirmation\n"); // access read printf("(18) LED test\n"); // LED test printf("(19) QUIT\n"); scanf("%d",&n); if(n == 19) { n = 0; //used to finish off the loop done = TRUE; break; } switch(n) { case 1: // Channel Info printf("\nEnter the channel\n"); scanf("%d",&channel); if(readSwitch1C(portnum,&sn[0],&state[0])) { printf("The channel is "); if(getLatchState1C(channel,&state[0])) printf("on.\n"); else printf("off\n"); printf("The Level is "); if(getLevel1C(channel,&state[0])) printf("high.\n"); else printf("low.\n"); if(getSensedActivity1C(channel,&state[0])) printf("Activity was detected on the channel.\n\n"); else printf("No activity was detected on the channel.\n\n"); } else OWERROR_DUMP(stdout); break; case 2: // Sets channel printf("\nEnter the channel\n"); scanf("%d",&channel); printf("Turn channel off enter 0, on 1.\n"); scanf("%d",&set); if(setLatchState1C(portnum,&sn[0],channel,set)) { printf("Latch was set "); if(set) printf("on.\n"); else printf("off.\n"); } else OWERROR_DUMP(stdout); break; case 3: // Read Selection Mask printf("\nEnter the channel\n"); scanf("%d",&channel); if(readRegister1C(portnum,&sn[0],®[0])) { printf("register is %02X %02X %02X\n",reg[0],reg[1],reg[2]); printf("The Selection Mask for channel %d is ",channel); latch = (uchar) (0x01 << channel); if((reg[0] & latch) == latch) printf("set.\n\n"); else printf("not set.\n\n"); } else OWERROR_DUMP(stdout); break; case 4: // Sets channel mask printf("\nEnter the channel\n"); scanf("%d",&channel); printf("Turn channel mask off enter 0, on 1.\n"); scanf("%d",&set); if(setChannelMask1C(portnum,&sn[0],channel,set)) { printf("The mask for channel %d was set ",channel); if(set) printf("on.\n\n"); else printf("off.\n\n"); } else OWERROR_DUMP(stdout); break; case 5: // Read Polarity Selection printf("\nEnter the channel\n"); scanf("%d",&channel); printf("The Polarity for channel %d is ",channel); if(getChannelPolarity1C(portnum,&sn[0],channel)) printf("set.\n\n"); else printf("not set.\n\n"); break; case 6: // sets channel polarity printf("\nEnter the channel\n"); scanf("%d",&channel); printf("Turn channel polarity off enter 0, on 1.\n"); scanf("%d",&set); if(setChannelPolarity1C(portnum,&sn[0],channel,set)) { printf("The polarity for channel %d was set ",channel); if(set) printf("on.\n\n"); else printf("off.\n\n"); } else OWERROR_DUMP(stdout); break; case 7: // Read Control/Status Reg if(readRegister1C(portnum,&sn[0],®[0])) printf("The Constrol/Status register is as following in hex %02X\n\n", reg[2]); else OWERROR_DUMP(stdout); break; case 8: // Set Reset Mode printf("Turn reset mode off enter 0, on 1.\n"); scanf("%d",&set); if(setResetMode1C(portnum,&sn[0],set)) { printf("Reset Mode was turned "); if(set) printf("on.\n\n"); else printf("off.\n\n"); } else OWERROR_DUMP(stdout); break; case 9: // Clear Power on reset if(clearPowerOnReset1C(portnum,&sn[0])) printf("Power on reset was cleared.\n\n"); else OWERROR_DUMP(stdout); break; case 10: // Get VCC state if(readRegister1C(portnum,&sn[0],®[0])) { printf("VCC state register is %02X\n",reg[2]); if(getVCC1C(®[0])) printf("VCC is powered.\n\n"); else printf("VCC is grounded.\n\n"); } else OWERROR_DUMP(stdout); break; case 11: // or condition search if(orConditionalSearch1C(portnum,&sn[0])) printf("OR condition search was set.\n\n"); else OWERROR_DUMP(stdout); break; case 12: // and condition search if(andConditionalSearch1C(portnum,&sn[0])) printf("AND condition search was set.\n\n"); else OWERROR_DUMP(stdout); break; case 13: // write scratchpad printf("Enter the address to start writing: "); addr = getNumber(0, 550); if(menuSelect(&sn[0]) == MODE_TEXT) len = getData(data,MAX_LEN,MODE_TEXT); else len = getData(data,MAX_LEN,MODE_HEX); if(!writeScratch1C(portnum,&sn[0],addr,len,&data[0])) OWERROR_DUMP(stdout); break; case 14: // read scratchpad if(!readScratch1C(portnum,&sn[0],&len,&es,&address[0],&data[0])) { printf("error\n"); OWERROR_DUMP(stdout); } else { printf("Address bytes: %02X %02X\n",address[0],address[1]); printf("ES byte: %02X\n",es); printf("Length: %d\n",len); printf("Scratchpad data: "); for(i=0;i<len;i++) printf("%02X ",data[i]); printf("\n"); } break; case 15: // copy scratchpad if(!copyScratch1C(portnum,&sn[0])) { OWERROR_DUMP(stdout); } else { printf("Copy Scratchpad Complete.\n"); } break; case 16: // read memory printf("Enter the address to start reading: "); addr = getNumber(0, 550); printf("Enter the length you want to read: "); len = getNumber(0,256); if(!read1C(portnum,&sn[0],addr,len,&data[0])) { OWERROR_DUMP(stdout); } else { for(i=0;i<len;i++) printf("%02X ",data[i]); printf("\n"); } break; case 17: // and condition search if (!owTouchReset(portnum)) OWERROR_DUMP(stdout); if(!owWriteByte(portnum,0xCC)) printf("skip rom error.\n"); owWriteByte(portnum,0xF5); for(i=0;i<34;i++) send_block[send_cnt++] = 0xFF; if(!owBlock(portnum,FALSE,&send_block[0],send_cnt)) { OWERROR(OWERROR_BLOCK_FAILED); return FALSE; } setcrc16(portnum,0); lastcrc16 = docrc16(portnum,0xF5); for(i=0;i<34;i++) lastcrc16 = docrc16(portnum,send_block[i]); if(lastcrc16 != 0xB001) printf("CRC didn't match.\n"); printf("read data: "); for(i=0;i<34;i++) printf("%02X ",send_block[i]); printf("\n"); send_cnt = 0; for(i=0;i<34;i++) send_block[send_cnt++] = 0xFF; if(!owBlock(portnum,FALSE,&send_block[0],send_cnt)) { OWERROR(OWERROR_BLOCK_FAILED); return FALSE; } setcrc16(portnum,0); for(i=0;i<34;i++) lastcrc16 = docrc16(portnum,send_block[i]); if(lastcrc16 != 0xB001) printf("CRC2 didn't match.\n"); printf("read data2: "); for(i=0;i<34;i++) printf("%02X ",send_block[i]); printf("\n"); send_cnt = 0; for(i=0;i<34;i++) send_block[send_cnt++] = 0xFF; if(!owBlock(portnum,FALSE,&send_block[0],send_cnt)) { OWERROR(OWERROR_BLOCK_FAILED); return FALSE; } setcrc16(portnum,0); for(i=0;i<34;i++) lastcrc16 = docrc16(portnum,send_block[i]); if(lastcrc16 != 0xB001) printf("CRC3 didn't match.\n"); printf("read data3: "); for(i=0;i<34;i++) printf("%02X ",send_block[i]); printf("\n"); break; case 18: // LED test printf("\nEnter the channel to turn the LED on.\n"); scanf("%d",&channel); printf("Turn reset mode off enter 1, on 0.\n"); scanf("%d",&set); printf("Alternate on and off 0=No, 1=Yes.\n"); scanf("%d",&alternate); if (!owTouchReset(portnum)) OWERROR_DUMP(stdout); if(!owWriteByte(portnum,0xCC)) printf("skip rom error.\n"); owWriteByte(portnum,0x5A); for(i=0;i<256;i++) { if(channel == 0) { if(set == 0) { if(!alternate) { if(!owWriteByte(portnum,0xFE)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x01)) printf("write byte error.\n"); check = 0xFE; } else { if(alt) { if(!owWriteByte(portnum,0xFE)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x01)) printf("write byte error.\n"); check = 0xFE; alt = FALSE; } else { if(!owWriteByte(portnum,0xFF)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x00)) printf("write byte error.\n"); check = 0xFF; alt = TRUE; } } } else { if(!alternate) { if(!owWriteByte(portnum,0xFF)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x00)) printf("write byte error.\n"); check = 0xFF; } else { if(alt) { if(!owWriteByte(portnum,0xFE)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x01)) printf("write byte error.\n"); check = 0xFE; alt = FALSE; } else { if(!owWriteByte(portnum,0xFF)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x00)) printf("write byte error.\n"); check = 0xFF; alt = TRUE; } } } } else { if(set == 0) { if(!alternate) { if(!owWriteByte(portnum,0xFD)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x02)) printf("write byte error.\n"); check = 0xFD; } else { if(alt) { if(!owWriteByte(portnum,0xFD)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x02)) printf("write byte error.\n"); check = 0xFD; alt = FALSE; } else { if(!owWriteByte(portnum,0xFF)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x00)) printf("write byte error.\n"); check = 0xFF; alt = TRUE; } } } else { if(!alternate) { if(!owWriteByte(portnum,0xFF)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x00)) printf("write byte error.\n"); check = 0xFF; } else { if(alt) { if(!owWriteByte(portnum,0xFD)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x02)) printf("write byte error.\n"); check = 0xFD; alt = FALSE; } else { if(!owWriteByte(portnum,0xFF)) printf("write byte error.\n"); if(!owWriteByte(portnum,0x00)) printf("write byte error.\n"); check = 0xFF; alt = TRUE; } } } } send_block[0] = (uchar)owReadByte(portnum); send_block[1] = (uchar)owReadByte(portnum); if((send_block[0] != 0xAA) && (send_block[1] != check)) printf("confirmation byte was %02X and read back was %02X\n", send_block[0],send_block[1]); } default: break; } }while(!done); } else printf("DS28E04 not found on One Wire Network\n"); owRelease(portnum); } return 1; }
/* SetupVsens setup DS2438 to read Vsens voltage difference enable IAD, CA and EE of status configuration register ( page <0h> byte <0h>) Vsens A/D conversion occurs with a frequency of 36.41 measurements/sec once IAD is enabled ( set to '1'). No special command necessary. input parameters portnum port number SNum serial number of DS2438 device device (USB DS2490 or serial DS9097U) */ int SetupVsens(int portnum, uchar *SNum, char *device) { uchar datablock[50]; uchar conf_reg = 0x00; int send_cnt = 0; int i; ushort lastcrc8; int busybyte; double ti, tf; struct timezone tz; struct timeval tv; gettimeofday( &tv, &tz); ti = tv.tv_sec+1.0e-6*tv.tv_usec; /* enable IAD, CA and EE of configuration byte */ conf_reg |= IAD | CA | EE; owSerialNum(portnum,SNum,FALSE); // Recall the Status/Configuration page // Recall command datablock[send_cnt++] = 0xB8; // Page to Recall datablock[send_cnt++] = 0x00; if(!owBlock(portnum,FALSE,datablock,send_cnt)) return FALSE; send_cnt = 0; if(owAccess(portnum)) { // Read the Status/Configuration byte // Read scratchpad command datablock[send_cnt++] = 0xBE; // Page for the Status/Configuration byte datablock[send_cnt++] = 0x00; for(i=0;i<9;i++) datablock[send_cnt++] = 0xFF; if(owBlock(portnum,FALSE,datablock,send_cnt)) { setcrc8(portnum,0); for(i=2;i<send_cnt;i++) lastcrc8 = docrc8(portnum,datablock[i]); if(lastcrc8 != 0x00) return FALSE; }//Block else return FALSE; if ( datablock[2] & conf_reg ) { syslog(LOG_DEBUG, "SetupVsens: IAD, CA and EE are set: return!\n"); gettimeofday( &tv, &tz); tf = tv.tv_sec+1.0e-6*tv.tv_usec; syslog(LOG_DEBUG, "SetupVsens: elapsed time: %f\n", tf -ti); return TRUE; } else { syslog(LOG_DEBUG, "SetupVsens: IAD, CA and EE are not set. Continue to setup\n"); } }//Access if(owAccess(portnum)) { send_cnt = 0; // Write the Status/Configuration byte // Write scratchpad command datablock[send_cnt++] = 0x4E; // Write page datablock[send_cnt++] = 0x00; // IAD, CA and EE set to "1" datablock[send_cnt++] |= conf_reg; // do not change the rest for(i=0;i<7;i++) datablock[send_cnt++] = datablock[i+3]; if(owBlock(portnum,FALSE,datablock,send_cnt)) { send_cnt = 0; if(owAccess(portnum)) { // Copy the Status/Configuration byte // Copy scratchpad command datablock[send_cnt++] = 0x48; // Copy page datablock[send_cnt++] = 0x00; if(owBlock(portnum,FALSE,datablock,send_cnt)) { busybyte = owReadByte(portnum); while(busybyte == 0) busybyte = owReadByte(portnum); gettimeofday( &tv, &tz); tf = tv.tv_sec+1.0e-6*tv.tv_usec; syslog(LOG_DEBUG, "SetupVsens: elapsed time: %f\n", tf -ti); return TRUE; }//Block }//Access }//Block }//Access return FALSE; }
//---------------------------------------------------------------------- // Family 0x10 Demo // void main(void) { int temp; int sign; BYTE i; //---------------------------------------- ADCON1 = 0x07; // PortB digital bTRD6 = 1; // RD6 = Serial Input bTRD7 = 0; // RD7 = Serial Output bLD7 = 1; printf("\nFamily 0x10 Demo\n%"); //////////////////////////////////////////////////////////// // Initialise the OneWire network (bus) // attempt to acquire the 1-Wire Nets if (!owAcquire(0, NULL)) { printf("Acquire failed.\n%"); while(1) ; } //////////////////////////////////////////////////////////// // Let's set the alarmtemps of all devices on the bus. // To demonstrate this, we write a low alarmtemp of 19 // and a high alarmtemp of 21 to all devices. // Finally we copy the alarmtemps to non-volatile EEPROM. printf("\nStart - Set AlarmTemperatures.\n%"); // Reset the devices owTouchReset(0); // Address all devices on the net owWriteByte(0, OW_SKIPROM); // Use the WriteScratchpad command to set AlarmTemps owWriteByte(0, OW_WRITESCRATCHPAD); owWriteByte(0, 19); // Valid RoomTemperature owWriteByte(0, 21); // 19 <= Temp < 21 printf("End - Set AlarmTemperatures.\n%"); // Set alarmtemps done // Copy the temps to EEPROM printf("\nStart - Write AlarmTemperatures to EEPROM.\n%"); // Reset the devices owTouchReset(0); // Address all devices on the net owWriteByte(0, OW_SKIPROM); // send the CopyScratchpad command to copy the AlarmTemps to EEPROM // If the bus supports Strong PullUp, we use it (because // there might be parasite powered devices on this bus). if(owHasPowerDelivery(0)) { printf("The bus has a Strong PullUp\n%"); owWriteBytePower(0, OW_COPYSCRATCHPAD); // Eeprom update takes 10 milliseconds max. 11ms is safer Wait(11); // turn off the strong pull-up owLevel(0, MODE_NORMAL); } else { printf("The bus has NO Strong PullUp\n%"); owWriteByte(0, OW_COPYSCRATCHPAD); // Eeprom update takes 10 milliseconds max. 11ms is safer Wait(11); } printf("End - Write AlarmTemperatures to EEPROM.\n%"); // Copy the temps to EEPROM done while(1) { //////////////////////////////////////////////////////////// // We do a temp conversion on all DS18S20's printf("\nStart - TemperatureConversions\n%"); // Reset the devices owTouchReset(0); // Address all devices on this net owWriteByte(0, OW_SKIPROM); // Send the convert command and start power delivery if available. // Note that we use a different way here to determine // Strong PullUp capability. if(owWriteBytePower(0, OW_CONVERTT)) { printf("We are using a Strong PullUp.\n%"); } else { printf("No Strong PullUp configured.\n%"); owWriteByte(0, OW_CONVERTT); } // Coversion takes 750 milliseconds max. 751ms is safer Wait(751); // turn off the 1-Wire Net strong pull-up owLevel(0, MODE_NORMAL); printf("End - TemperatureConversions\n%"); //////////////////////////////////////////////////////////// // We will now demonstrate an alarmsearch printf("\nStart - Find devices in alarmstate\n%"); // Find the devices with temp < 19 or temp >= 21 if(!owFirst(0, TRUE, TRUE)) { printf("No devices in AlarmState found.\n%"); } else { do { printf("Device in AlarmState found: 0x%02X%02X%02X%02X%02X%02X%02X%02X\n%", owNetCurrent.SerialNum[7], owNetCurrent.SerialNum[6], owNetCurrent.SerialNum[5], owNetCurrent.SerialNum[4], owNetCurrent.SerialNum[3], owNetCurrent.SerialNum[2], owNetCurrent.SerialNum[1], owNetCurrent.SerialNum[0]); } while(owNext(0, TRUE, TRUE)); } printf("End - Find devices in alarmstate\n%"); //////////////////////////////////////////////////////////// // We will now demonstrate a normal search. For // every device found, it's temperature is retrieved. printf("\nStart - Find devices and get their temperatures.\n%"); if(!owFirst(0, TRUE, FALSE)) { printf("No devices found.\n%"); } else { do { printf("Device found: 0x%02X%02X%02X%02X%02X%02X%02X%02X%", owNetCurrent.SerialNum[7], owNetCurrent.SerialNum[6], owNetCurrent.SerialNum[5], owNetCurrent.SerialNum[4], owNetCurrent.SerialNum[3], owNetCurrent.SerialNum[2], owNetCurrent.SerialNum[1], owNetCurrent.SerialNum[0]); // Init the crc setcrc8(0, 0); // Read the device's memory owWriteByte(0, OW_READSCRATCHPAD); for(i = 0; i < SCRATCHPAD_SIZE; i++) { ScratchPad[i] = owReadByte(0); docrc8(0, ScratchPad[i]); } // Check the CRC if(owNetCurrent.utilcrc8 != 0) { printf(", crc is NOT OK.\n%"); } else { // We don't want to use float's temp = ((int)ScratchPad[SCRATCHPAD_TEMPERATUREMSB] << 8) | (int)ScratchPad[SCRATCHPAD_TEMPERATURELSB]; if(temp < 0) { temp = -temp; sign = '-'; } else if(temp == 0) { sign = ' '; } else { sign = '+'; } printf(", Temperature is %c%d.%d degrees\n%", sign, (temp >> 1), (temp & 0x01) ? 5 : 0); } } while(owNext(0, TRUE, FALSE)); } printf("End - Find devices and get their temperatures.\n%"); //////////////////////////////////////////////////////////// Wait(1); // Just a convenient way to set a breakpoint } }
//---------------------------------------------------------------------- // Read the temperature of a DS18B20 (family code 0x28) // // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to // OpenCOM to indicate the port number. // 'SerialNum' - Serial Number of DS18B20 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 ReadTemperature28(int portnum, uchar *SerialNum, float *Temp) { uchar rt=FALSE; uchar send_block[30],lastcrc8; int send_cnt, tsht, i, loop=0; int power; // set the device serial number to the counter device owSerialNum(portnum,SerialNum,FALSE); for (loop = 0; loop < 2; loop ++) { // check if the chip is connected to VDD if (owAccess(portnum)) { owWriteByte(portnum,0xB4); power = owReadByte(portnum); } // access the device if (owAccess(portnum)) { // send the convert command and if nesessary start power delivery if (power) { if (!owWriteBytePower(portnum,0x44)) return FALSE; } else { if (!owWriteByte(portnum,0x44)) return FALSE; } // sleep for 1 second msDelay(1000); // turn off the 1-Wire Net strong pull-up if (power) { 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_cnt = 0; 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)) { // initialize the CRC8 setcrc8(portnum,0); // 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[2] << 8; tsht = tsht | send_block[1]; if (tsht & 0x00001000) tsht = tsht | 0xffff0000; *Temp = ((float) tsht)/16; // success rt = TRUE; break; } } } } } // return the result flag rt return rt; }