'''Rom ID Finder for Multidrop and hardware serial output with Atmega8'''------------------------------------------------------------------------''''''ThisprogramisbasedontheSearchAlgorythmdescribedinMAXIMAN817.'''The variable names are (as far as possible) identical to the application note.'''TheCRC8algorythmisbasedonpublicsourcesfoundontheinternet.''''''TheprogramsearchesandfindstheRomID's of multiple 1-wire devices'''connectedtoasocalledMultidrop.AMultidropisalinewith2ormore'''1-wire devices connected in normal or parasite Power mode.'''ParasitePowermodeisnotsupportedfornow.'''''''''This program can be adapted to any of the supported microprocessor'''bysimplychangingthe#chipasfollows:#chip16f877a,4'''and by choosing the right connections for the Hardware UART and OW input pin.''''''@Theo Loermans'''@licenceGPL'''@version 1.0'''@date08.03.2016'''******************************************************************************** #chip mega8, 16 ; ----- Define Hardware settings ; Required to LCD to Serial redirection #define LCD_IO 0 #define USART_BAUD_RATE 9600 Dir PORTD.1 Out Dir PORTD.0 In #define USART_BLOCKING ; #define debug 'ifactivate,thescriptproducesadditionaloutput#defineDQPortB.0;inputpinfor1-wiredevice(s),normalpowermodewithpullup4K7DimROM_NO(9)asbyte;9ROM_NObytestostoreIDBytes+CRC;don't use ROM_NO(0) Dim ByteNum as byte 'DS18X20Settings#defineSearchRom240'0xF0 (240) 'CommandforidentifyingSlaveRomcodes,#defineReadPowerSup180'0XB4 (180) 'ReadPowerSupplymode#defineSkipRom204'0xCC (204) 'Commandforaddressingalldevicessimultaneously'Start main program HSerPrint "Multidrop ID-Search.":CRLF HSerPrint "No. Family + Rom Number = ID-Number CRC Status":CRLF MasterRST 'resetandcheckpresenceofsensor(s)ifcommand=2thenHSerPrint"Check line, shorted?":CRLFEnd' exit script, resolve problem end if if command = 1 then HSerPrint "No device(s) detected!":CRLF End 'exitscriptfornow,futureexpandendif' sequence for checking Power Supply Mode OWout SkipRom OWout ReadPowerSup 'readpowerSupplyModeOWinBitCommandIfnotCommandThenHSerPrint"Parasite power mode detected!":CRLFHSerPrint"Not supported now, program end.":CRLFEnd' Parasite mode not supported now, stop program end If''Init Variable TotDevF = 0 LastDiscrepancy = 0 LastFamilyDiscrepancy = 0 LastDeviceFlag = 0 Returnvalue = 0 For Bytenum = 1 to 9 ROM_NO(Bytenum)=0 Next'StartIDSearchSearch' This is the first SearchDo If Returnvalue = 1 then #ifdef debug HSerPrint "Returnvalue = 1":CRLF HSerPrint "LastDeviceFlag = ":HSerPrint LastDeviceFlag:CRLF #Endif if TotDevF < 10 then HSerPrint " " HSerPrint TotDevF:HSerPrint " " HSerprint "0x":HSerPrint HEX(ROM_NO(1)):HSerPrint " " For Bytenum = 2 to 7 HSerprint "0x":HSerPrint HEX(ROM_NO(Bytenum)):HSerPrint " " next HSerprint " 0x":HSerPrint HEX(ROM_NO(8)):HSerPrint " " #ifdef debug HSerPrint "0x":HSerPrint HEX(CRC):HSerPrint " " #Endif If CRC = ROM_NO(8) Then 'CalculatedCRCidenticaltofoundCRC?' HSerPrint "OK :-)" Else HSerPrint "Error :-(" end if CRLF Else HSerPrint "ID-Search finished." : CRLF:CRLF End 'stopprogramendifSearch' second and further Search until Returnvalue = falseLoop'-------------------------SUB------------------------------------SubSearch' Start ID Search MasterRST If LastDeviceFlag = 1 Then Returnvalue = 0 Goto Ready 'SearchFinishedendifid_bit_number=0last_zero=0Bytenum=1BPiB=0' Bit position in Byte (ROM_NO) OWout SearchRom Do While id_bit_number < 64 ; (0-63) OWInBit id_bit 'read1bitfromdeviceOWInBitcmp_id_bit'read 1 bit (complement) from device if id_bit = 1 AND cmp_id_bit = 1 Then 'invalidvalue's HSerPrint "No answer from device(s)!": CRLF Returnvalue = 0 Goto Ready end if If id_bit = 0 AND cmp_id_bit = 0 Then '2ormoredeviceshavethesamebitvalueincommon#ifdefdebugHSerPrint"0-0 Discrepancy ":CRLF#EndifIfid_bit_number=LastDiscrepancyThenSearch_Direction=1elseIfid_bit_number>LastDiscrepancyThenSearch_Direction=0ElseCalcBPiBSelectCaseBPiBCase0SetSearch_Direction=ROM_NO(Bytenum).0Case1SetSearch_Direction=ROM_NO(Bytenum).1Case2SetSearch_Direction=ROM_NO(Bytenum).2Case3SetSearch_Direction=ROM_NO(Bytenum).3Case4SetSearch_Direction=ROM_NO(Bytenum).4Case5SetSearch_Direction=ROM_NO(Bytenum).5Case6SetSearch_Direction=ROM_NO(Bytenum).6Case7SetSearch_Direction=ROM_NO(Bytenum).7EndSelect#ifdefdebugHSerPrint"Bytenum= ":HSerPrintBytenum:HSerPrint" BPiB= ":HSerPrintBPiBHSerPrint" Search dir= ":HSerPrintSearch_direction:CRLF#EndifendifendifIfSearch_Direction=0Thenlast_zero=id_bit_numberIflast_zero<8ThenLastFamilyDiscrepancy=last_zeroendifendifElseSearch_Direction=id_bitEndifCalcBPiBSelectCaseBPiBCase0ifSearch_Direction=1thenSetROM_NO(Bytenum).0OnelseSetROM_NO(Bytenum).0OffendifCase1ifSearch_Direction=1thenSetROM_NO(Bytenum).1OnelseSetROM_NO(Bytenum).1OffendifCase2ifSearch_Direction=1thenSetROM_NO(Bytenum).2OnelseSetROM_NO(Bytenum).2OffendifCase3ifSearch_Direction=1thenSetROM_NO(Bytenum).3OnelseSetROM_NO(Bytenum).3OffendifCase4ifSearch_Direction=1thenSetROM_NO(Bytenum).4OnelseSetROM_NO(Bytenum).4OffendifCase5ifSearch_Direction=1thenSetROM_NO(Bytenum).5OnelseSetROM_NO(Bytenum).5OffendifCase6ifSearch_Direction=1thenSetROM_NO(Bytenum).6OnelseSetROM_NO(Bytenum).6OffendifCase7ifSearch_Direction=1thenSetROM_NO(Bytenum).7OnelseSetROM_NO(Bytenum).7OffendifEndSelect#ifdefdebugifid_bit_number<10thenHSerPrint" "HSerPrintid_bit_number:HSerPrint")"HSerPrint" id_bit=":HSerPrintid_bitHSerPrint" cmp_id_bit=":HSerPrintcmp_id_bitHSerPrint" Bytenum=":HSerPrintBytenumHSerPrint" BPiB=":HSerPrintBPiBHSerPrint" ROM_NO(":HSerPrintBytenum:HSerPrint")=0x"HSerPrintHex(ROM_NO(Bytenum))HSerPrint" Search dir=":HSerPrintSearch_Direction'CRLF HSerPrint " last_zero=":HSerPrint last_zero HSerPrint " LastDiscrepancy=":HSerPrint LastDiscrepancy HSerPrint " LastFamilyDiscrepancy=":HSerPrint LastFamilyDiscrepancy:CRLF #Endif OWoutBit Search_Direction 'sendsearch_directionbittodeviceBytenum=(id_bit_number/8)+1' calculate Byte number (1-8) for ROM_NO bytes id_bit_number = id_bit_number + 1 'incrementIDbitnumber(0-63)LoopLastDiscrepancy=last_zeroIfLastDiscrepancy=0ThenLastDeviceFlag=1endifCRC=0' Calculate CRC8: DALLAS/MAXIM, reversed Polynomial For index = 1 to 7 _DB=ROM_NO(index) Repeat 8 FB = (_DB XOR crc) AND 1 CRC = CRC/2 _DB = _DB/2 if FB = 1 then CRC = CRC XOR 140 '0x8CendifendRepeatnextReturnvalue=1Ready:ifReturnvalue=0thenLastDiscrepancy=0LastFamilyDiscrepancy=0LastDeviceFlag=0endifTotDevF=TotDevF+1' Increment Total Devices Foundend SubSub CRLF 'Carriagereturn,LinefeedHSerSend13HSerSend10EndsubSubCalcBPiB' by using bytes for the variables there are no fractions if you divide Bytenum = (id_bit_number/8)+1 'calculateBytenumber(1-8)BPiB=id_bit_number-((id_bit_number/8)*8)' calculate Bit position in Byte (0-7)end SubSub OWin (Command) IntOff 'InterruptDisabledRepeat8RotateCommandRight' LSB first Dir DQ Out Set DQ Off 'Starttimeslotwait6usDirDQIn' Release bus for one wire Reception wait 9 us If DQ On Then Set Command.7 1 If DQ Off Then Set Command.7 0 wait 55 us End Repeat IntOn 'InterruptEnabledendsubSubOWinBit(Command)IntOffDirDQOutSetDQOff' Start time slot wait 6 us Dir DQ In 'ReleasebusforonewireReceptionwait9usIfDQOnThenSetCommand=1ifDQOffthenSetCommand=0wait55usIntOnendsubSubOWout(InCommand)IntOffRepeat8wait1us' Recovery time between write cycles Dir DQ Out Set DQ Off 'Starttimeslotwait6usIfCommand.0OnthenDirDQInEndifwait60usDirDQInROTATECommandRight' LSB first End Repeat IntOnend subSub OWoutBit (In Command) IntOff wait 1 us 'RecoverytimebetweenwritecyclesDirDQOutSetDQOffwait6usIfCommand.0OnthenDirDQInEndifwait60usDirDQInIntOnendsubSubMasterRST(optionalcommand=0)IntOffDirDQOutSetDQOffwait480usDirDQInwait86us' 70 us default: tested low 30, High 143; at home: (143-30)/2=56+30=86 If DQ On then 'Nosensor(s)foundcommand=1endifwait394us'410 us default: tested 394 if DQ Off then 'linestilllowafterendtimeslot:shorted?Command=2endifIntOnendsub
Last edit: Theo 2016-03-09
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
There seems to be an issue with the compiler, I get the same kind of errors during compiling with a PIC. (i don't have PIC's , so I didn't try it before). So far no problems with AVR.(knock on wood!)
Perhaps Anobium can take a look at the problem.
Theo.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you Anobium, I discovered the same, but the reason was unknow to me.
Perhaps extend the compiler with an error message which tells you the name of the variable that is causing the problem?
Theo.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Last edit: Theo 2016-03-09
Variable DB changed into _DB, thanks to Anobium.
Beautiful !
Tomorrow I try it!
I bought it for pic16f877a and i got these errors:
@jackjames
There seems to be an issue with the compiler, I get the same kind of errors during compiling with a PIC. (i don't have PIC's , so I didn't try it before). So far no problems with AVR.(knock on wood!)
Perhaps Anobium can take a look at the problem.
Theo.
DB is for reserved ASM.
Please change DB = DB/2 to _DB = = _DB/2
Something like:
Thank you Anobium, I discovered the same, but the reason was unknow to me.
Perhaps extend the compiler with an error message which tells you the name of the variable that is causing the problem?
Theo.
Changing the variable name the compilation problem has been solved