How to Implement 1-wire Devices - Page 1
Forum section: electronics / beginners

author: Docara
author_rank: Regular Contributor
timestamp: June 30, 2018, 10:36:26 am
content: Hi,I wish to implement 3x temperature sensing, remote I/O Switches, RTCC and possibly EEPROM functionality on one 1-wire bus. It seems the use of a Master DS2840B line driver which incorporates a search algorithm (partial) 12C to RS232 conversion would aide things no-end.Initially, I looked into using an Arduino and an available library but, generally, they seem flaky and only meant for one DS18B20 device.Looking through the Datasheets - I am struggling to properly understand the communication requirements of these devices. For a non-programmer - the search algorithm, even with the DS2840B, looks horrendous to implement as does the CRC on the fly checking.Has anyone used these in anger and could offer some guidance?Many thanksMattP.S. this is the best example of real world example I could findhttp://itech.fgcu.edu/faculty/zalewski/CDA4170/files/1wire.pdf] http://itech.fgcu.edu/faculty/zalewski/CDA4170/files/1wire.pdf

author: BradC
author_rank: Super Contributor
timestamp: June 30, 2018, 10:54:21 am
content: I did it in a PIC 12F615 in C, so it's not hard. I was memory constrained for storing the serial numbers so I simply iterated the search algorithm and then reported the temperature of the next sensor as they were detected. I also do it in SPIN on a Parallax Propeller. Again, not difficult and much easier when you have more ram to play with.If I get a chance over the next couple of days I'll see if I can dig out the code and post it.

author: madires
author_rank: Super Contributor
timestamp: June 30, 2018, 11:36:17 am
content: You can download several app notes on 1-Wire from Maxim which explain everything, some include code examples. Though, the slightly different specs for the protocol timing in each app note are a bit confusing. The search algorithm for ROM codes is similar to a binary search tree. And there are tons of examples for CRC8 and CRC16 (be aware that different CRC versions use different polynomials and start values).This is the CRC8 I've written:Code:/* CRC */uint8_t CRC8; /* current CRC-8 *//** calculate CRC-8* - CRC = X^8 + X^5 + X^4 + 1* - start value: 0x00* - uses variable CRC8 to track current CRC** requires:* - Byte: new input byte*/void OneWire_CRC8(uint8_t Byte){uint8_t n = 0; /* counter */uint8_t Bit; /* LSB */while (n < 8) /* 8 bits */{/* XOR current LSB of input with CRC8's current X^8 */Bit = CRC8 ^ Byte; /* XOR */Bit &= 0b00000001; /* filter LSB *//* shift CRC right */CRC8 >>= 1; /* for next bit */if (Bit) /* XORed LSB is 1 */{/** XOR CRC's X^5 and X^4 with 1* - XOR with 0b00011000* - since CRC is already shifted right: XOR with 0b00001100* - since we have to feed the XORed LSB back into the CRC* and the MSB is 0 after shifting: XOR with 0b10001100*/CRC8 ^= 0b10001100; /* XOR */}/* when 0:* - XOR would keep the original bits* - MSB will be 0 after a right shift anyway*//* shift input right */Byte >>= 1; /* for next input bit */n++; /* next bit */}}

author: Docara
author_rank: Regular Contributor
timestamp: June 30, 2018, 11:39:56 am
content: Hi Brad,Thanks for the reply.I have to be honest I was wondering to whether or not to just kick the search algorithm into touch and just do a local 1 pin bit banged search for any new or replacement devices.As you have programmed the 1-wire stuff would you mind doing me a favour and a look through the DS2840B datasheets and let me know if using this might have made things easier for youhttps://www.maximintegrated.com/en/products/interface/controllers-expanders/DS2480B.htmlhttp://pdfserv.maximintegrated.com/en/an/AN192.pdfThanksMatt

author: towlerg
author_rank: Contributor
timestamp: June 30, 2018, 12:18:05 pm
content: Definatley the bit bang, save serial numbers is eeprom (if you have). Where you have more than one temperature sensor you usually need to know pyhsically where each device is, either write one time data collection routine or if you're lazy use your programmer to write directly to eeprom.If you don't have eeprom most processors will allow you to read and write program memory.As to libruaries I know thats the Arduino goto move but if I were you I would dissect a working lib for the bit bang and work from there.

author: Docara
author_rank: Regular Contributor
timestamp: June 30, 2018, 12:18:40 pm
content: Hi Madires,Thanks for the reply and codeSo if my VERY limited knowledge of C doesn't embarrass me tooo much - does CRC-8 have to be defined elsewhere - I'm guessing this is part of the raw data being read in from a device before a CRC is carried out?With the help of an online C compiler site I added CRC8=0b10010101 (is the syntax/command correct) before theCode:uint8_t CRC8; /* current CRC-8 */But it throws up an errorCode:main.cpp:2:1: error: 'uint8_t' does not name a typeuint8_t CRC8; /* current CRC-8 */main.cpp:14:19: error: variable or field 'OneWire_CRC8' declared voidvoid OneWire_CRC8(uint8_t Byte)main.cpp:14:19: error: 'uint8_t' was not declared in this scope(Code format for clarity)Could you explain a little more pleaseThanksMatt

author: Docara
author_rank: Regular Contributor
timestamp: June 30, 2018, 12:46:06 pm
content: Hi Towlerg,Thank you for your response.An external EEPROM or even an internal one is not a problem.Just to give you all bit of background, as mentioned I'm non a programmer so I use a program called Flowcode.Whilst the supplied help file is non existent (and what is there is piss poor) the software is brilliant and you are able to produce VERY complicated software through graphical icons. Matrix have used the same premise as a flow chart from years ago. The software is microprocessor independent (to a point) and can be changed at will without a rewrite (other than pin designations obviously).Now, what I am trying to do is implement C routine(s) into this software to allow the basic low level communication of a 1-wire system with ease. The software is designed to have code inserted into it and your are able to pass variable values back and forth so why not!This is why I am not using the Arduino IDE because it seems to need #includes pulling in code from elsewhere and I find it very hard to follow all the links, I intend to recreate the wheel starting from scratch but also address one of the main shortcomings of i2C which of course is cable length etc etc which is why i want to use the DS2480B to do a lot of the work for me.Matt

author: C
author_rank: Super Contributor
timestamp: June 30, 2018, 01:44:37 pm
content: DocaraThat scan function first looks scary until you understand what is happening.First, each device on the One-wire bus has a 64 bit ID. This allows a huge number of devices. But due to binary, you can divide this number down fast. Each bit of that ID you process is a divide by two of ID's.For example think of an ID with just 3 bits. That would allow for 8 devices, but just knowing state of one bit lets you divide that into two groups of 4. The scan lets you ask if there is a device in group one & if there is a device in group two.So with 64 bits, scan lets you ask the needed 128 questions for each bit to get a full ID.But One-Wire is using a trick to make this even faster. That 64 bit ID contains a 8-bit CRC of the remaining bits. It is possible to have many devices with the same CRC, but most of the time the CRC will be unique to one device. So if you have no matching CRC's then just the CRC part acts somewhat like a short 8-bit ID for that 64-bit ID.So when you put this togetherScan lets you ask if a bit in the ID is a ONE, ZERO or no device exists.This lets you build a binary tree of devices on the bus.And due to CRC, most of time the tree is 8 or less levels deep.Think of what a binary tree is in the PC world. In the file system, for each level you could have up to two folders, A One folder & A Zero Folder. The folder would be missing if there was not a device for that branch.When you get to a point of just one device below the branch you could stop having more branches and have a file with filename = ID.The last 8-bits tells you what type of device. So from the above tree you could make lists of each type of device on bus.EDITBeen a while ago doing low level One-wire. Scan works from LSB to MSB, making strike-out text invalid.

author: madires
author_rank: Super Contributor
timestamp: June 30, 2018, 02:20:03 pm
content: Quote from: Docara on June 30, 2018, 12:18:40 pmCRC8 is a global variable which contains the current CRC value. Before running OneWire_CRC8() for the received data CRC8 needs to be set to the start value 0x00. Then you call OneWire_CRC8() for each data byte, and after that CRC8 holds the CRC value for the data.This might sound harsh but I won't help you with the compiler errors. If you like to program you have to learn and understand the programming language. Copy & paste is fast and simple. However, it doesn't teach you the basics. This is meant to encourage you to learn.

author: Docara
author_rank: Regular Contributor
timestamp: June 30, 2018, 03:41:47 pm
content: Hi Maderies,I dont care about the compiling error! (see below)I was just letting you know there was a problem - I dont know enough to know if what you posted is a finished bit of code or not so I was trying to helpQuoteNot wishing to sound ungrateful butat the momentI dont want or have the time to learn how to program - I just need to know enough to get myself out of the crap!!I've been working on a project which should have taken 3-6 months and has ended up taking over 3 years. I've run out of money and need to finish sooner rather than later so I can go back to work. I've installed cabling which I cant change and I'm now sorting out the electronics for what I have installed, part of that is 1-wire remote sensing and remote switching. 3years ago when I was doing some research and found 1-wire and also found the Arduino libraries I thought it would be a relatively straightforward matter to implement - I didn't take on board there complexities or try them out - and now I'm here LOL.Matt

author: Docara
author_rank: Regular Contributor
timestamp: June 30, 2018, 04:01:03 pm
content: Hello C,Thank for you input.You are quite right about the binary tree. I just cant visualise how to formulate even an idea to sort out a program of what's going on beyond one branch. How you make sure you go down all possible branches whilst keeping track of where you are is hard and confusing.I now what I want to achieve and roughly what I need to do but I then read the datasheets for these devices and I suddenly develop Maxim dyslexia - I read the words ......... and that's it!!! No understanding or comprehension whatsoever comes out the other side - very weird.

author: C
author_rank: Super Contributor
timestamp: June 30, 2018, 05:35:41 pm
content: With the scan function you get three answers for each bit.There are some devices with a One in this position.There are some devices with a Zero in this position.else there are no devices.Now the easy way is to use recursion.start with what Current_bit = 0Pass the current_bit to recursion subadd one to local_current_bitWhile local_current_bit<64Check for One for bit if soAdd One-Bit folderand recurse ( local_current_bit) .Check for Zero for bit if soAdd zero-bitfolderand recurse ( local_current_bit) .end whilereturnYou can do the same with lists by creating a check from here list.As you check a bit level remove from list.If you find a device at bit level then add to list.So a blank list causes a check of bit one.No response = nothing on bus.In place of creating files above you add to the liist.You need to think of simpleThen you start doing an address, you have all devices monitoring the bus.If the address bit does not match bit in device ID then that device will stop monitoring.A good address leaves one device still monitoring bus waiting for commands.If you want C source code for this there are some examples on net.Think of what you should see.You have three temp sensorsYou should have one path containing all three sensors and then a path change for each due to serial number.

author: Docara
author_rank: Regular Contributor
timestamp: June 30, 2018, 06:55:22 pm
content: Hi C,I see that you have tried to put into words what I should do in code - thank you for that.I'm just re-reading Maxim's Application Note 187 and have your example on screen - I have just noticed that in the AP187 version I found on the net it includes code at the end. Again I will go through it and see if anything sticks in the grey matter.Will report backMatt

author: C
author_rank: Super Contributor
timestamp: June 30, 2018, 07:42:59 pm
content: If you lookthe low level is not too badAPPLICATION NOTE 1261-Wire Communication Through Softwarehttps://pdfserv.maximintegrated.com/en/an/AN126.pdfThe search is doing two reads for each ID bit then writing what path you want to continue on.This two bit response gives you four states.shown in table 1 of APPLICATION NOTE 1871-Wire Search AlgorithmThere are many complete libraries that exist all ready for one-wirejust pick a language that you understand and check if it's search function will return many devices.The hard part is the low level timing. Smart would be to use a table to make these easy to adjust.C

author: Docara
author_rank: Regular Contributor
timestamp: June 30, 2018, 09:27:59 pm
content: Yeah the timing and slew is why I would like to use the DS2480B (RS232) line driver as the master (or one of the other variants for I2C). The DS2480 also has part of the search algorithm built in.Its keeping track of the path is causing me the headache, I think I could eventual muddle through the (basic) search function.like I said I'm not software where do these libraries exist that you refer to? The only ones I can find is via Maxim.Additionally, I remember that there was another line driver which included the full search algorithm the DS9490R but it's for a USB interface.I subsequently found this for android which looks really interesting and handy for a fall back. Worse case scenario I could always enter the address in manually via a simple touch screen.https://www.maximintegrated.com/en/app-notes/index.mvp/id/5705

author: C
author_rank: Super Contributor
timestamp: June 30, 2018, 10:10:29 pm
content: just google:one wire library"what micro-controller are you trying to use?

author: Docara
author_rank: Regular Contributor
timestamp: July 02, 2018, 09:29:34 am
content: Hi,Any uC really!As mentioned I do not use/like the Arduino IDE, unfortunately this limits 'libraries'At the moment I'm looking at the STM32F103 range or similar because I need a wouldlikea CAN port and need two UART's although PIC's are not out and need quite a few I/O's

author: BradC
author_rank: Super Contributor
timestamp: July 02, 2018, 09:59:48 am
content: This is my code for a 12F615.It does a "start conversion" to the entire bus, then it implements the one wire search and uses that to iterate through thermometers to read, one at a time.Honestly one wire isn't hard. The timing is as loose as you want it to be.I've also attached a library for the Parallax Propeller that uses one wire in two wire mode (data & pull down) to allow running the devices at 5V on a 3.3V uC. Again, it implements the one-wire search but this time it builds an array of 64bit words that list all the serial numbers it finds.Check out the read/write bit routines. The timing is seriously loose, and it's not complex.

author: Docara
author_rank: Regular Contributor
timestamp: July 02, 2018, 10:18:39 am
content: Wow! BradC thank you so much I will down load and look through it now and get back to you

author: Docara
author_rank: Regular Contributor
timestamp: July 02, 2018, 10:44:20 am
content: Hi BradCAm I right in thinking the full Search Routine starts at the red arrow in the enclosed?

author: BradC
author_rank: Super Contributor
timestamp: July 02, 2018, 12:17:46 pm
content: Sort of.onewire_reset_search() prepares the search routine with a blank canvas.You then call onewire_search(). If it returns true you will find the serial number in ROM_NO.I use this in onewire_gettemp() to get the temperature from the chip with the serial number in ROM_NO.You can call onewire_search() repeatedly and it will iterate all the devices on the bus, putting the serial number in ROM_NO. When it returns false it has run out of devices on the bus and you call onwire_search_reset() to start again.Because of the lack of memory on this chip, I have a button which iterates the devices on the bus and it just repeatedly does a conversion/read cycle. Each time I press the button it steps to the next device. When it runs out of devices it resets the search routine and starts again.I'm sorry, my code is never well documented or particularly elegant.I forget where I found the search routine. It was either in an app note or pseudocode somewhere. I've ported it around to a couple of languages and each time it's elegance loses something in translation.Recently I started numbering devices with a "role" and stored this in the device eeprom. I use another CRC in the eeprom second byte to verify this device is actually programmed with my bits. That way I can pre-program the sensor before I put them in, and as the search routine steps through them it can identify which serial number is for which role. Saves me storing the serial numbers and makes sensor replacement a lot easier.The point I was trying to make is onewire isn't difficult.

author: C
author_rank: Super Contributor
timestamp: July 02, 2018, 03:56:46 pm
content: Quote from: Docara on July 02, 2018, 09:29:34 amThe STM32F103 or better would be good idea.To have simple software you do need more Ram.Better to have more then needed then not enough.A UART is not a great interface between CPU's as you most often have to encode/decode data. It is probably best to create a packet like encoding. CAN does packets in hardware.CAN is a great way to connect many controllers.Remember that it is priority event based packet.The One-wire bus uses packets.With One-wire scan you can make this bus plug & play.Think you will find that you need a lot of data per device if you want to have easy to create software with great function.Before you start on higher level software, think of big picture.You could have a one-wire temperature sensor controlling many outputs on many micro-controllers.One-wire scan can give you a list of devices on the busEach device will need some data to the device & you will get some data back. You will need data for control values. You would probably like some Human data for this ID is _______Great simple software would be using lists, arrays and structures.Takes ram to do this.Part of this would be creating event lists.In place of writing a large amount of code, think of processing the data and making this easy.You do this by having just one set of code that handles all the DS18B20's on bus and using RAM to get/save data.To add an additional DS18B20 then becomes just setting up data after scan finds it.In place of a serial interface to PC you you could have what looks like a USB drive to PC but Ram to controller.Just so you knowA Raspberry Pi & linux can do One-wire. Each device is in the file tree. This lets linux software use commands for files to work with one-wire.

author: Docara
author_rank: Regular Contributor
timestamp: July 02, 2018, 05:19:19 pm
content: Hi BradCPlease don't apologiseThe problem is if you don't know how to do something its always bloody difficult!! The thing is my only real experience is with the BASIC programming language years ago on the likes of BBC B Micro, Sinclair Spectrum etc or limited Assembly on a Z80 from the same time. So I'm really struggling with the logic of the C code.At present I am looking trhrough a 1wire library for Ardiono on Github. I didn't realise you could examine code within libraries.Does the CRC system come into play suring the search?Matt

author: C
author_rank: Super Contributor
timestamp: July 02, 2018, 05:46:43 pm
content: If you want error free communications you use something like CRC to check for valid data.So for a read you can not check CRC but will not know if there is an error.For a write to a device that uses CRC you have to have proper value or device will think it's an error

author: madires
author_rank: Super Contributor
timestamp: July 02, 2018, 05:58:29 pm
content: The ROM code also includes a CRC8 to make sure that the addressing of devices is correct.

author: C
author_rank: Super Contributor
timestamp: July 02, 2018, 06:06:49 pm
content: Think of C as if it is a smart assembler. This is what it started as.A pointer is just an address like you would have used on Z80Z80 assembly can work with many data types, in C you state type.In C you also get higher level things you find in basic.https://www.arduino.cc/reference/en/http://www.cplusplus.com/

author: Docara
author_rank: Regular Contributor
timestamp: July 03, 2018, 04:52:12 pm
content: HiAfter looking through sample code which implements the Search Algorithm I think it is way beyond my programming skills to implement. I can't even get a picture in my mind of how it works - which is, I guess, half the battle to start coding. The techniques that have been used just don't make any sense to me, and I just don't know how to proceed from here.I have contacted Maxim and submitted a product request for the full S.A. to be implemented in their family of line drivers - the DS2480B being a member. They already do it in their USB to 1-wre chip.Anyway thank you all for you replies to my question.Dave mate if you're reading this perhaps a tutorial is in orderMatt

author: C
author_rank: Super Contributor
timestamp: July 03, 2018, 06:21:46 pm
content: With your limited program ability, you might want to work on this part first.If you were to get a Raspberry PI you could work on your software knowledge &Their is a very simple software interface from the Raspberry PI to One-Wire. The actually communications to/from One-Wire is all written. Your software would be working with files & folders. In addition you could have access to many software languages.The One-wire has a 64 bit IDOne way is to translate to something you might understand.Convert this to a directory treeThink of the ROM_Scan function telling you of existence of two possible folders in a path.Rom_Scan starts from the LSB and works to MSBSo reading the first 8-bits gives you the family code.18B20 has family code of 28H which is 0b101000With only some 18B20's on bus, if you convert to a windows path you get a path starting with"0\0\0\1\0\1\0\0"When Rom_Scan checks first bit you get0 bit exists = a folder with folder_name = "0" exists1 bit does not exist = a folder with folder_name = "1" does not existswhen checking the 4'th bit you get 1 exists No 0After first 8 you start reading the serial number and the CRC.Now if you had something else with a different family code in addition to 18B20's, one or more of those bit answers would have0 exists & 1 existsAny time you get this both exists, you need to remember one path and keep searching other path.Now if you drop the FLOWCODEYou could just use a ESP32This will give you a lot of simple options.You could have a WIFI Web page for 18b20's and moreYou could still have serial connectionYou could still have CANDo a YouTube search of"esp32 arduino 18b20"There are a huge number of results.And some are specific to having many 18B20 temperature sensorsStarting from low end micro-controller can be much harder then starting from HIGH that you can modify to make it match what you want.