{"id":217,"date":"2012-02-05T18:39:44","date_gmt":"2012-02-05T17:39:44","guid":{"rendered":"http:\/\/robin.jansman.info\/wordpress\/?p=217"},"modified":"2016-07-07T17:36:13","modified_gmt":"2016-07-07T16:36:13","slug":"1-wire-ds18b20-thermometer","status":"publish","type":"post","link":"http:\/\/www.mb200d.nl\/wordpress\/2012\/02\/1-wire-ds18b20-thermometer\/","title":{"rendered":"1-Wire DS18B20 Thermometer"},"content":{"rendered":"<p>Measuring room or object temperatures with the DS18B20. It is a digital component wich does all the work for you. You only need to COMMAND it do the measurement. A short moment later you can request 2 bytes containing the temperature.<\/p>\n<p>Of course there is a protocal wich says how to communicate. See 1-Wire and the DS18B20 datasheet.<\/p>\n<p>How 1-Wire works (by Maxim): <a href=\"http:\/\/www.maxim-ic.com\/products\/1-wire\/flash\/overview\/index.cfm\" target=\"_blank\">http:\/\/www.maxim-ic.com\/products\/1-wire\/flash\/overview\/index.cfm<\/a><\/p>\n<div id=\"attachment_225\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/robin.jansman.info\/wordpress\/wp-content\/uploads\/2012\/02\/maxim-1-wire.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-225\" class=\"size-medium wp-image-225\" title=\"maxim-1-wire\" src=\"http:\/\/robin.jansman.info\/wordpress\/wp-content\/uploads\/2012\/02\/maxim-1-wire-300x179.png\" alt=\"Maxim 1-Wire\" width=\"300\" height=\"179\" srcset=\"http:\/\/www.mb200d.nl\/wordpress\/wp-content\/uploads\/2012\/02\/maxim-1-wire-300x179.png 300w, http:\/\/www.mb200d.nl\/wordpress\/wp-content\/uploads\/2012\/02\/maxim-1-wire-500x300.png 500w, http:\/\/www.mb200d.nl\/wordpress\/wp-content\/uploads\/2012\/02\/maxim-1-wire.png 1001w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-225\" class=\"wp-caption-text\">Maxim 1-Wire, how to communicate<\/p><\/div>\n<p>Used parts:<\/p>\n<ul>\n<li>PIC16F689\n<ol>\n<li>FLASH: 4096 Words<\/li>\n<li>RAM: 256 Bytes<\/li>\n<li>IO Pins: 18<\/li>\n<li>Clock internal: 8MHz internal, 4MHz default config<\/li>\n<li>Clock external:up to 20MHz external<\/li>\n<\/ol>\n<\/li>\n<li>LCD 2&#215;16, HD44780 Compatible<\/li>\n<li>DS18B20<\/li>\n<\/ul>\n<p>[nggallery id=5]<\/p>\n<p><!--more--><\/p>\n<p>All codings are done with Microchip&#8217;s MPLAB X IDE and code is compiled by HI TECH&#8217;s (lite) tools. This software can be downloaded at Microchip&#8217;s website.<\/p>\n<p>HEX files generated by the compiler will be uploaded to the microcontroller with XWisp (software) and Wisp628\/648 (hardware). In-Circuit-Serial-Programming ICSP.<\/p>\n<p>For 1-wire code see: <a title=\"PIC16 C Sample: 1-Wire\" href=\"http:\/\/robin.jansman.info\/wordpress\/2012\/02\/pic16-c-sample-1-wire\/\">PIC16 C Sample 1-Wire<\/a><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nunsigned int address&#x5B;8];  \/\/For storing 64bit 1-wire address\r\nbit devicePresent = 0;\r\nvoid main(void)\r\n{\r\n   if(owReset())  \/\/Device present\r\n   {\r\n      owWriteByte(0x33);  \/\/Read ROM command (request address)\r\n      for(int i = 0; i &amp;lt; 8; i++)\r\n      {\r\n         int b = owReadByte();\r\n         address&#x5B;i] = b;\r\n      }\r\n      devicePresent = 1; \/\/Flag for having found the address\r\n   }\r\n\r\n   while(1)\/\/Infinite loop\r\n   {\r\n      owMatchRom(address); \/\/First send device address\r\n      owWriteByte(0x44);   \/\/0x44, send command for start converting temp\r\n\r\n      \/\/wait 2 seconds, device is calculating temp\r\n      __delay_ms(2000);\r\n\r\n      \/\/Now request the result by sending the address again \r\n      owMatchRom(address);\r\n      \/\/followed by the read command 0xBE\r\n      owWriteByte(0xBE);\r\n      \r\n      \/\/9 bytes are waiting to be read\r\n      \/\/see DS18B20 datasheet\r\n      int answer&#x5B;9];\r\n      for(int i = 0; i &lt; 9; i++)\r\n      {\r\n         answer&#x5B;i] = owReadByte();\r\n      }\r\n\r\n      \/\/First 2 bytes contain temp (celsius)\r\n      \/\/LSB and MSB, google it......\r\n      int Temp = (answer&#x5B;1] &lt;&lt; 8) + answer&#x5B;0]; Temp = Temp &gt;&gt; 4; \/\/Remove data behind . or , \r\n      \/\/This will give 18 | 19 and not 18.45 or 19.5\r\n\r\n      \/\/Temp will now contains temperature in celsius\r\n\r\n      char text&#x5B;3]; \/\/space for three characters\r\n      sprintf(text,&quot;%3d&quot;,Temp); \/\/Convert Temp to ASCII characters\r\n\r\n      lcdtxt(text); \/\/show result on you lcd\r\n      serialwrite(text); \/\/write result to your computer?\r\n\r\n      __delay_ms(5000); \/\/Repeat every 5 seconds\r\n   }\r\n}\r\n<\/pre>\n<h3>ow functions<\/h3>\n<p>Update: 2016-07-07<\/p>\n<pre class=\"brush: cpp; title: additional required 1-wire Functions; notranslate\" title=\"additional required 1-wire Functions\">\r\nvoid owMatchRom(unsigned int * id)\r\n{\r\n    owReset();\r\n    owWriteByte(0x55);  \/\/Match ROM cmd\r\n    \/\/Now write device address\/id\r\n    for(int i = 0;i&lt;8;i++)\r\n    {\r\n        owWriteByte(id&#x5B;i]);\r\n    }\r\n}\r\n\r\nint owReset(void)\r\n{\r\n    char state = 0;\r\n    OW_TRIS = 0;    \/\/Set as output\r\n    OW_PORT = 0;    \/\/Drive Low\r\n    __delay_us(480);\r\n\r\n    OW_TRIS = 1;    \/\/Release, Set back as input\r\n    __delay_us(70);\r\n    state = !OW_PORT;\r\n    __delay_us(410);\r\n    return state;\r\n}\r\nvoid owWriteByte(unsigned int data)\r\n{\r\n    int loop;\r\n    for(loop = 0; loop &lt; 8; loop++) { owWriteBit(data &amp;0x01); data &gt;&gt;= 1;\r\n    }\r\n}\r\nvoid owWriteBit(unsigned int b)\r\n{\r\n    OW_TRIS = 0;    \/\/Set as output\r\n    OW_PORT = 0;    \/\/Drive low\r\n\r\n    if(b==1)\r\n    {\r\n        __delay_us(3);\r\n        OW_TRIS = 1;    \/\/Release, set as input\r\n        __delay_us(62);\r\n    }\r\n    else\r\n    {\r\n        __delay_us(57);\r\n        OW_TRIS = 1;    \/\/Release, set as input\r\n        __delay_us(7);\r\n    }\r\n}\r\nint owReadByte(void)\r\n{\r\n    int loop, result = 0;\r\n    for(loop = 0; loop &lt; 8; loop++) { result &gt;&gt;= 1;\r\n        if(owReadBit())\r\n            result |= 0x80;\r\n    }\r\n    return result;\r\n}\r\nint owReadBit(void)\r\n{\r\n    unsigned int iReadState = 0;\r\n    OW_TRIS = 0;    \/\/Set as output\r\n    OW_PORT = 0;    \/\/Drive low\r\n    __delay_us(4);\r\n    OW_TRIS = 1;    \/\/Release, set as input\r\n    __delay_us(8);\r\n    iReadState = OW_PORT;\r\n    return iReadState;\r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Measuring room or object temperatures with the DS18B20. It is a digital component wich does all the work for you. You only need to COMMAND it do the measurement. A short moment later you can request 2 bytes containing the temperature. Of course there is a protocal wich says how to communicate. See 1-Wire and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[327,13,16,1,328],"tags":[53,54,56,57,49,52,50,51,55],"class_list":["post-217","post","type-post","status-publish","format-standard","hentry","category-electronics","category-english","category-projects","category-robin","category-software","tag-1-wire","tag-ds18b20","tag-microchip","tag-microchip-pic16","tag-mplab-x","tag-onewire","tag-pic16","tag-pic16f689","tag-temperature"],"_links":{"self":[{"href":"http:\/\/www.mb200d.nl\/wordpress\/wp-json\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mb200d.nl\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mb200d.nl\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mb200d.nl\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mb200d.nl\/wordpress\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":28,"href":"http:\/\/www.mb200d.nl\/wordpress\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":660,"href":"http:\/\/www.mb200d.nl\/wordpress\/wp-json\/wp\/v2\/posts\/217\/revisions\/660"}],"wp:attachment":[{"href":"http:\/\/www.mb200d.nl\/wordpress\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mb200d.nl\/wordpress\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mb200d.nl\/wordpress\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}