uint32_t OneWire_Init( void ) { printf("\n%s called",__FUNCTION__); printf("\nScanning 1-wire bus..."); tempidx = -1; // Assume we don't find a temperature sensor for( int i = 0; i < sizeof(tcidmapping); i++ ) { tcidmapping[i] = -1; // Assume we don't find any thermocouple interfaces } int rslt = OWFirst(); numowdevices = 0; while (rslt && numowdevices < MAX_OW_DEVICES) { memcpy(owdeviceids[numowdevices], ROM_NO, sizeof(ROM_NO)); numowdevices++; rslt = OWNext(); } if(numowdevices) { for( int iter = 0; iter < numowdevices; iter++ ) { printf("\n Found "); for( int idloop = 7; idloop >= 0; idloop-- ) { printf("%02x", owdeviceids[iter][idloop]); } uint8_t family = owdeviceids[iter][0]; if(family == OW_FAMILY_TEMP) { selectdevbyidx(iter); xferbyte(OW_WRITE_SCRATCHPAD); xferbyte(0x00); xferbyte(0x00); xferbyte(0x1f); // Reduce resolution to 0.5C to keep conversion time reasonable tempidx = iter; // Keep track of where we saw the last/only temperature sensor printf(" [Temperature sensor]"); } else if(family == OW_FAMILY_TC) { selectdevbyidx(iter); xferbyte(OW_READ_SCRATCHPAD); xferbyte(0xff); xferbyte(0xff); xferbyte(0xff); xferbyte(0xff); uint8_t tcid = xferbyte(0xff) & 0x0f; tcidmapping[tcid] = iter; // Keep track of the ID mapping printf(" [Thermocouple interface, ID %x]",tcid); } } } else { printf(" No devices found!"); } return numowdevices; }
uint8_t doSlave(uint8_t* message) { uint8_t i; uint8_t cmd = message[0]; uint8_t datalen = 0; uint8_t retval = 0; debugPrint("command=%x\r\n", cmd); if (cmd == 0x10) { } else if (cmd == 0x11) { message[2] = OWFirst() ? 1 : 0; datalen = 1; } else if (cmd == 0x12) { message[2] = OWNext() ? 1 : 0; datalen = 1; } else if (cmd == 0x13) { datalen = 8; for (i = 0; i < 8; i++) { message[i + 2] = getROM_NO(i); } } else if (cmd == 0x16) { OWTargetSetup(0x00); } else if (cmd == 0x19) { message[2] = OWReadByte(); datalen = 1; } else if (cmd == 0x1B) { uint8_t len; uint8_t* _buf; len = message[1]; _buf = message + 2; retval = OWBlock(_buf, len) ? 0x00 : 0xFF; memcpy(message + 2, _buf, len); datalen = len; } else if (cmd == 0x1C) { message[2] = OWReset(); datalen = 1; } else if (cmd == 0x1D) { // OWWriteByte(message[1]); uint8_t len; uint8_t* _buf; len = message[1]; _buf = message + 2; retval = OWBlock(_buf, len) ? 0x00 : 0xFF; memcpy(message + 2, _buf, len); datalen = len; } else if (cmd == 0x1E) { // OWLevel(MODE_NORMAL); } else if (cmd == 0x1F) { OWTargetSetup(message[1]); } else if (cmd == 0x20) { OWFamilySkipSetup(); } else if (cmd == 0x21) { uint8_t address[8]; memcpy(address, message + 2, 8); message[2] = OWSearchROM(address); datalen = 1; } else if (cmd == 0x22) { uint8_t address[8]; memcpy(address, message + 2, 8); message[2] = OWMatchROM(address); datalen = 1; } else { retval = 0xFF; } message[0] = retval; if (retval == 0xFF) { datalen = 0; } message[1] = datalen; return datalen; }
void DS1820main(void) { static u8 fistsech=0; s16 temp1,temp2; //u8 buff[16]; // u8 i,j,num=DS18B20_NUM; if(fistsech==0) { Delay_init(72);//72M Delay_ms(20); Init_DS18B20_IO(); #if 0 if(1==OWFirst()) //第一次搜索到ROM数据 { memcpy(&ID_Buff[0][0],GetRomAddr(),8);//复制ID数据到IDbuff for(temp1=1;temp1<DS18B20_NUM;temp1++) { if( OWSearch()==1) { memcpy(&ID_Buff[temp1][0],GetRomAddr(),8);//复制ID数据到IDbuff } else { break;//没有器件 } } } else{ return; } #endif temp[0]= '\t'; temp[1]='T'; temp[2]='i'; temp[3]='m'; temp[4]='e'; temp[5]=':'; temp[11]= '\t'; temp[12]='T'; temp[13]='e'; temp[14]='m'; temp[15]='p'; temp[16]=':'; temp[23]='\t'; temp[20]='.' ; temp[27]='.' ; temp[30]= '\t'; temp[31]= '\r'; temp[32]= '\n'; fistsech=1; } __disable_irq();// 相当于 CPSID I //读取2个指定ID的温度传感器数值 // tempA是在顶部没有划的,tempB顶部划了一下:-) temp1 = DS18B20_ReadDesignateTemper(ID_Buff[0]); __enable_irq();// 相当于 CPSIE I // temp2 = DS18B20_ReadDesignateTemper(ID_Buff[1]); if(temp1==0) return ; Temp_pid[0]=temp1; //用于PID计算 temp[6]=time_run/10000+0x30; temp[7]=time_run%10000/1000+0x30; temp[8]=time_run%10000%1000/100+0x30; temp[9]=time_run%10000%1000%100/10+0x30; temp[10]=time_run%10000%1000%100%10+0x30; temp[17]= temp1/10000+0x30; temp[18]= temp1%10000/1000+0x30; temp[19]= temp1%10000%1000/100+0x30; temp[21]= temp1%10000%1000%100/10+0x30; temp[22]= temp1%10000%1000%100%10+0x30; temp[24]=temp2/10000+0x30; temp[25]= temp2%10000/1000+0x30; temp[26]= temp2%10000%1000/100+0x30; temp[28]= temp2%10000%1000%100/10+0x30; temp[29]= temp2%10000%1000%100%10+0x30; temp[30] = temp1; temp[31] = temp2; }
uint32_t OneWire_Init(void) { printf("\n%s called", __FUNCTION__); Sched_SetWorkfunc(ONEWIRE_WORK, OneWire_Work); printf("\nScanning 1-wire bus..."); tempidx = -1; // Assume we don't find a temperature sensor for (int i = 0; i < sizeof(tcidmapping); i++) { tcidmapping[i] = -1; // Assume we don't find any thermocouple interfaces } uint32_t save = VIC_DisableIRQ(); int rslt = OWFirst(); VIC_RestoreIRQ( save ); numowdevices = 0; while (rslt && numowdevices < MAX_OW_DEVICES) { memcpy(owdeviceids[numowdevices], ROM_NO, sizeof(ROM_NO)); numowdevices++; save = VIC_DisableIRQ(); rslt = OWNext(); VIC_RestoreIRQ( save ); } if (numowdevices) { for (int iter = 0; iter < numowdevices; iter++) { printf("\n Found "); for (int idloop = 7; idloop >= 0; idloop--) { printf("%02x", owdeviceids[iter][idloop]); } uint8_t family = owdeviceids[iter][0]; if (family == OW_FAMILY_TEMP1 || family == OW_FAMILY_TEMP2 || family == OW_FAMILY_TEMP3) { const char* sensorname = "UNKNOWN"; if (family == OW_FAMILY_TEMP1) { sensorname = "DS1822"; } else if (family == OW_FAMILY_TEMP2) { sensorname = "DS18B20"; } else if (family == OW_FAMILY_TEMP3) { sensorname = "DS18S20"; } save = VIC_DisableIRQ(); selectdevbyidx(iter); xferbyte(OW_WRITE_SCRATCHPAD); xferbyte(0x00); xferbyte(0x00); xferbyte(0x1f); // Reduce resolution to 0.5C to keep conversion time reasonable VIC_RestoreIRQ(save); tempidx = iter; // Keep track of where we saw the last/only temperature sensor printf(" [%s Temperature sensor]", sensorname); } else if (family == OW_FAMILY_TC) { save = VIC_DisableIRQ(); selectdevbyidx(iter); xferbyte(OW_READ_SCRATCHPAD); xferbyte(0xff); xferbyte(0xff); xferbyte(0xff); xferbyte(0xff); uint8_t tcid = xferbyte(0xff) & 0x0f; VIC_RestoreIRQ( save ); tcidmapping[tcid] = iter; // Keep track of the ID mapping printf(" [Thermocouple interface, ID %x]",tcid); } } } else { printf(" No devices found!"); } if (numowdevices) { Sched_SetState(ONEWIRE_WORK, 2, 0); // Enable OneWire task if there's at least one device } return numowdevices; }
//-------------------------------------------------------------------------- // TEST BUILD MAIN // int main(void) { int rslt,i,cnt; double temp; char buf[12]; // find ALL devices printf("\nFIND ALL\n"); cnt = 0; rslt = OWFirst(); while (rslt) { // print device found for (i = 7; i >= 0; i--) printf("%02X", ROM_NO[i]); printf(" %d\n",++cnt); rslt = OWNext(); } // find only 0x10 printf("\nFIND ONLY 0x10\n"); cnt = 0; OWTargetSetup(0x10); while (OWNext()) { // check for incorrect type if (ROM_NO[0] != 0x10) break; // print device found for (i = 7; i >= 0; i--) printf("%02X", ROM_NO[i]); printf(" %d\n",++cnt); } if (cnt == 1) { DS1820_startConversion(); uint8_t spdata[10]; sleep10(7); DS1820_readScratchPad(spdata); temp = DS1820_getTemperature(spdata); temp = temp*9.0/5.0 +32.0; printf("TEMP(F) = %s\n\n",dtostrf(temp,7,2,(char *)buf)); } // find all but 0x04, 0x1A, 0x23, and 0x01 printf("\nFIND ALL EXCEPT 0x10, 0x04, 0x0A, 0x1A, 0x23, 0x01\n"); cnt = 0; rslt = OWFirst(); while (rslt) { // check for incorrect type if ((ROM_NO[0] == 0x04) || (ROM_NO[0] == 0x1A) || (ROM_NO[0] == 0x01) || (ROM_NO[0] == 0x23) || (ROM_NO[0] == 0x0A) || (ROM_NO[0] == 0x10)) OWFamilySkipSetup(); else { // print device found for (i = 7; i >= 0; i--) printf("%02X", ROM_NO[i]); printf(" %d\n",++cnt); } rslt = OWNext(); } //////////////////////////////////////////////////////////////////////////////////////////////// // find ALL DS2438 double voltage = 0.0; int myDS2438 = 0; uint8_t DS2438deviceNumber[8]; double bigT = -1.; int micro10voltage = 0; printf("\nFind all DS2438; Read Temperature and Voltage\n"); for (rslt=OWFirstType(0x26); rslt; rslt=OWNextType(0x26)) { // print device found for (i = 7; i >= 0; i--) printf("%02X", ROM_NO[i]); printf("\n"); // Save the first ROM if (myDS2438 == 0) { myDS2438++; for (i = 0; i < 8; i++) { DS2438deviceNumber[i] = ROM_NO[i]; } } if (myDS2438 > 0) { // (default) Read Vdd SetupAtoD(0x0F, DS2438deviceNumber); bigT = Get_Temperature(DS2438deviceNumber); printf("TEMP = %s\n\n",dtostrf(bigT,7,3,(char *)buf)); voltage = ReadAtoD(DS2438deviceNumber); printf("VDD = %s\n",dtostrf(voltage,6,2,(char *)buf)); } if (myDS2438 > 0) { // (default) Read Vad SetupAtoD(0x07, DS2438deviceNumber); voltage = ReadAtoD(DS2438deviceNumber); micro10voltage = getDS2438Voltage(DS2438deviceNumber); printf("VAD = %s\n",dtostrf(voltage,6,2,(char *)buf)); printf("Micro V = %d\n", micro10voltage); } } sleep(1); return 0 ; }