int Copy2Mem43(int portnum, uchar *SerialNum) { uchar rt=FALSE; uchar read_data; owSerialNum(portnum, SerialNum, FALSE); if(owAccess(portnum)) { if (!owWriteBytePower(portnum, COPY_SCRATCH_CMD)) return FALSE; owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write LSB of target addr owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write MSB of target addr owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x1f); //write E/S msDelay(500); owLevel(portnum,MODE_NORMAL); read_data = owReadBytePower(portnum); if (read_data == 0xaa) rt=TRUE; } return rt; }
int Write43(int portnum, uchar *SerialNum, uchar *page_buffer) { uchar rt=FALSE; ushort lastcrc16; int i; owSerialNum(portnum, SerialNum, FALSE); if(owAccess(portnum)) { mprintf(" Writing Scratchpad...\n"); if (!owWriteBytePower(portnum, WRITE_SCRATCH_CMD)) return FALSE; setcrc16(portnum, 0); docrc16(portnum,(ushort)WRITE_SCRATCH_CMD); owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write LSB of target addr docrc16(portnum,(ushort)0x00); owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write MSB of target addr docrc16(portnum,(ushort)0x00); for(i = 0; i < 32; i++) //write 32 data bytes to scratchpad { owLevel(portnum,MODE_NORMAL); owWriteBytePower(portnum, i); lastcrc16 = docrc16(portnum,i); } for(i = 0; i < 2; i++) //read two bytes CRC16 { owLevel(portnum,MODE_NORMAL); lastcrc16 = docrc16(portnum,(ushort)owReadBytePower(portnum)); } mprintf(" CRC16: %x\n", lastcrc16); if(lastcrc16 == 0xb001) { //copy to mem owLevel(portnum, MODE_NORMAL); if(Copy2Mem43(portnum, SerialNum)) rt=TRUE; } } return rt; }
// routine for reading a memory page of a DS28EC20P EEPROM // expects 32 byte deep buffer int ReadMem43(int portnum, uchar *SerialNum, uchar *page_buffer) { uchar rt=FALSE; ushort lastcrc16; int i; uchar read_data; owSerialNum(portnum, SerialNum, FALSE); if(owAccess(portnum)) { if (!owWriteBytePower(portnum, E_READ_MEM_CMD)) return FALSE; setcrc16(portnum, 0); //init crc docrc16(portnum,(ushort)E_READ_MEM_CMD); owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write LSB of target addr docrc16(portnum,(ushort)0x00); owLevel(portnum, MODE_NORMAL); owWriteBytePower(portnum, 0x00); //write MSB of target addr docrc16(portnum,(ushort)0x00); for(i = 0; i < 32; i++) { owLevel(portnum,MODE_NORMAL); page_buffer[i] = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, page_buffer[i]); } for(i = 0; i < 2; i++) { owLevel(portnum,MODE_NORMAL); read_data = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, read_data); } if (lastcrc16 == 0xb001) rt=TRUE; } return rt; }
// routine for reading the scratchpad of a DS28EC20P EEPROM // 80 pages of 32byte // 32byte scratchpad // expects 32 byte deep buffer int ReadScratch43(int portnum, uchar *SerialNum, uchar *page_buffer) { uchar rt=FALSE; ushort lastcrc16; int i; ushort target_addr = 0; uchar read_data; owSerialNum(portnum, SerialNum, FALSE); if(owAccess(portnum)) { mprintf(" Reading Scratchpad...\n"); if (!owWriteBytePower(portnum, READ_SCRATCH_CMD)) return FALSE; setcrc16(portnum, 0); //init crc docrc16(portnum,(ushort)READ_SCRATCH_CMD); owLevel(portnum,MODE_NORMAL); //read 2 byte address and 1 byte status read_data = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, read_data); target_addr = read_data; owLevel(portnum,MODE_NORMAL); read_data = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, read_data); target_addr |= read_data << 8; owLevel(portnum,MODE_NORMAL); read_data = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, read_data); mprintf("E/S: 0x%x\n", read_data); for(i = 0; i < 32; i++) { owLevel(portnum,MODE_NORMAL); page_buffer[i] = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, page_buffer[i]); } for(i = 0; i < 2; i++) { owLevel(portnum,MODE_NORMAL); read_data = owReadBytePower(portnum); lastcrc16 = docrc16(portnum, read_data); } if (lastcrc16 == 0xb001) rt=TRUE; } return rt; }
//---------------------------------------------------------------------- // 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; }
//---------------------------------------------------------------------- // Read the temperature of a DS18B20 // // '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 ReadTemperature28(int portnum, uchar *SerialNum, float *Temp) { uchar rt=FALSE; uchar send_block[30],lastcrc8; int send_cnt, tsht, i, loop=0; float tmp,cr,cpc; // set the device serial number to the counter device owSerialNum(portnum,SerialNum,FALSE); for (loop = 0; loop < 2; loop ++) { // access the device if (owAccess(portnum)) { // send the convert command and start power delivery if (!owWriteBytePower(portnum,0x44)) 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_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[1] >> 4) + ((send_block[2] & 0x07) << 4); if (send_block[2] & 0x80) 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; // TODO: Check config register if(send_block[1] & 0x01) { tmp += 0.0625f; } else if(send_block[1] & 0x02) { tmp += 0.125f; } else if(send_block[1] & 0x04) { tmp += 0.25f; } else if(send_block[1] & 0x08) { tmp += 0.5f; } *Temp = tmp; // success rt = TRUE; break; } } } } }
//---------------------------------------------------------------------- // Main AlarmSearch Test // void main(void) { BYTE portnum; //---------------------------------------- ADCON1 = 0x07; // PortB digital bTRD6 = 1; // RD6 = Serial Input bTRD7 = 0; // RD7 = Serial Output bLD7 = 1; printf("\nEnumeration (AlarmSearch) test\n%"); // Initialise the OneWire nets and the temperature devices for(portnum = 0; portnum < MAX_PORTNUM; portnum++) { // attempt to acquire the 1-Wire Nets if (!owAcquire(portnum,NULL)) { printf("Acquire failed for OneWire net %d.\n%", portnum); } else { // Reset the devices owTouchReset(portnum); // Address all devices on this net owWriteByte(portnum, OW_SKIPROM); // send the WriteScratchpad command to set AlarmTemps owWriteByte(portnum, OW_WRITESCRATCHPAD); owWriteByte(portnum, 19); // Valid RoomTemperature owWriteByte(portnum, 21); // 19 <= Temp < 21 } } while(1) { // Do a temp conversion on all DS18S20's on all nets simultanously for(portnum = 0; portnum < MAX_PORTNUM; portnum++) { // Reset the devices owTouchReset(portnum); // Address all devices on this net owWriteByte(portnum, OW_SKIPROM); // send the convert command and start power delivery if(!owWriteBytePower(portnum, OW_CONVERTT)) { printf("\nNo Strong PullUp on net %d. Trying without SPU.\n%", portnum); owWriteByte(portnum, OW_CONVERTT); } } // Conversion takes 750 milliseconds max. Safer is 751ms Wait(751); // turn off the 1-Wire Net strong pull-up for(portnum = 0; portnum < MAX_PORTNUM; portnum++) { owLevel(portnum, MODE_NORMAL); } for(portnum = 0; portnum < MAX_PORTNUM; portnum++) { // Find the device(s) in AlarmState printf("\nEnumeration for net %d.\n%", portnum); if(!owFirst(portnum, TRUE, TRUE)) { printf("No devices in AlarmState found on this net.\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(portnum, TRUE, TRUE)); } } Wait(10); // Just a convenient way to set a breakpoint } }