{"id":1971,"date":"2024-07-12T14:00:44","date_gmt":"2024-07-12T14:00:44","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1971"},"modified":"2024-08-11T17:22:44","modified_gmt":"2024-08-11T17:22:44","slug":"12bit-adcmcp3208","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1971","title":{"rendered":"12Bit ADC(MCP3208)"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-5d690680b3060c87b6f8d073398b8ed1 wp-block-paragraph\" style=\"color:#5c5c5c\">Sometime 10bit ADC is not enough for precious calculation. In that case we have 12bit ADC i.e. MCP3208.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"590\" height=\"211\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/MCP3208.png\" alt=\"\" class=\"wp-image-1972\" style=\"width:755px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/MCP3208.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/MCP3208-300x107.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-1-1024x512.jpg\" alt=\"\" class=\"wp-image-1974\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-1-1024x512.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-1-300x150.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-1-768x384.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-1.jpg 1351w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-b8371a6d428ad7eaaa82bef7f3e08cc4 wp-block-paragraph\" style=\"color:#6c8a97\">SPI Configuration<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"480\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-2-1024x480.jpg\" alt=\"\" class=\"wp-image-1976\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-2-1024x480.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-2-300x141.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-2-768x360.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-2.jpg 1464w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"407\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-11-1024x407.jpg\" alt=\"\" class=\"wp-image-1979\" style=\"width:799px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-11-1024x407.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-11-300x119.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-11-768x305.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-11.jpg 1384w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3c14d796eb985d82227275ad631b7344 wp-block-paragraph\" style=\"color:#5c5c5c\">Here B11-B0 is the 12bit ADC value. Thus the operating logic consist of MCP3208 consists of following steps-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"252\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-12-1024x252.jpg\" alt=\"\" class=\"wp-image-1983\" style=\"width:723px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-12-1024x252.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-12-300x74.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-12-768x189.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-12.jpg 1385w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6b540b769acd0949fbdcad69608774ea wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s use two function one is only write and other is read-write.<\/p>\n\n\n\n<ol style=\"color:#252525\" class=\"wp-block-list has-text-color has-link-color wp-elements-7fbd665a9a54a4b3a435f4a615bd2d0c\">\n<li class=\"has-upper-heading-font-size\">void SPIWrite(uint8_t input) -&gt; Only SPI write<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid SPIWrite(uint8_t input)\n{\nSPDR=input;\nwhile(!(SPSR&amp;(1&lt;&lt;SPIF)))\n\t{\n\t;\/\/wait until transmission complete\n\t}\n}\n<\/pre><\/div>\n\n\n<ol style=\"color:#252525\" class=\"wp-block-list has-text-color has-link-color wp-elements-dad1dfc11a5588c9e84fe9810e70cbd5\">\n<li class=\"has-upper-heading-font-size\">uint8_t SPIReadWrite(uint8_t input) -&gt; Both SPI write and read function<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t SPIReadWrite(uint8_t input)\n{\nSPDR=input;\nwhile(!(SPSR&amp;(1&lt;&lt;SPIF)))\n\t{\n\t;\/\/wait until transmission complete\n\t}\nreturn(SPDR);\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-8210263dec692751ea12920e2b1c49fe wp-block-paragraph\" style=\"color:#5c5c5c\">So the full user-defined function for ADC read as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint16_t READAdc(uint8_t ch)\n{\n\tuint8_t dataHIGH,dataLOW,bytestream;\n\tSPI_PORT&amp;=~(1&lt;&lt;SPI_CS);     \/\/enable latch\n\tbytestream=0b00000110;\n\tif(ch&gt;3)\n\t\t{\n\t\tbytestream=0b00000111;\n\t\t}\n\tSPIWrite(bytestream);\n\tbytestream=(ch&lt;&lt;6);\n\tdataHIGH=SPIReadWrite(bytestream); \/\/send any bit to receved data\n\tdataHIGH&amp;=0b00001111;              \/\/4bit high value\n\tdataLOW=SPIReadWrite(0xff);        \/\/send any bit to receved data\n\tSPI_PORT|=(1&lt;&lt;SPI_CS);             \/\/disable latch\n\treturn((dataHIGH&lt;&lt;8)|(dataLOW));\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-472a2126a2d677eb9ca2780d8be74b69 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s make a Temperature meter with 12 bit ADC. If we use LM35 then it gives 10millivolts per degree centigrade. Since we use 12bit ADC than the resolution should be-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6822d6b46c95969ef3931b0a4b93efb9 wp-block-paragraph\" style=\"color:#5c5c5c\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Resolution=5\/2<sup>12 <\/sup>=1.2207mV<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-57342cb680453b49e8538bd471f22f4c wp-block-paragraph\" style=\"color:#5c5c5c\">So the Temperature = (12bit ADC return value) x 0.12207<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-d3ab88b67ac352267143d0b55d7c35eb wp-block-paragraph\" style=\"color:#5c5c5c\">Download the mcp3208.c &amp; mcp3208.h file and the main program as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#include&lt;avr\/io.h&gt;\n#include&lt;stdio.h&gt;\n#include&quot;mcp3208.h&quot;\n#include&quot;lcd.h&quot;\nint main(void)\n{\nLCD_INIT();\nADCInitx();\nfloat temp;\nuint16_t ADCValue;\nchar LCD&#x5B;20];\nLCD_write_string(1,1,&quot;ADC result Display&quot;);\n\twhile(1)\n\t\t{\n\t\t\tADCValue=READAdc(3);\n\t\t\ttemp=ADCValue*0.12207;\n\t\t\tsprintf(LCD,&quot;Temperature=%.2f&quot;,(double)temp);\n\t\t\tLCD_write_string(1,2,LCD);\n\t\t}\nreturn 0;\n}\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/MCP3208-12bitADC.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-4928934ef85234b7be6bc89651ecf325 wp-block-paragraph\" style=\"color:#6c8a97\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/MCP3208-12bitADC.rar\">12-bit ADC(MCP3208).rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometime 10bit ADC is not enough for precious calculation. In that case we have 12bit ADC i.e. MCP3208. SPI Configuration Here B11-B0 is the 12bit ADC value. Thus the operating logic consist of MCP3208 consists of following steps- Let\u2019s use two function one is only write and other is read-write. So the full user-defined function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-1971","post","type-post","status-publish","format-standard","hentry","category-spi"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1971"}],"version-history":[{"count":15,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1971\/revisions"}],"predecessor-version":[{"id":2631,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1971\/revisions\/2631"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}