{"id":779,"date":"2017-10-11T19:17:45","date_gmt":"2017-10-11T19:17:45","guid":{"rendered":"https:\/\/ibex.tech\/embedded\/?p=779"},"modified":"2022-02-18T15:37:48","modified_gmt":"2022-02-18T15:37:48","slug":"pic18-atod","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic18\/xc8-compiler\/atod-xc8-compiler\/pic18-atod","title":{"rendered":"PIC18 AtoD"},"content":{"rendered":"<h4>\nNot using AtoD<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/----- SETUP THE A TO D PINS -----\r\n\tADCON0 = 0b00000000;\t\t\t\/\/AtoD disabled\r\n\t\/\/ADCON1 = 0b00000000;\r\n\t\/\/ADCON2 = 0b10111110;\t\t\t\/\/Right justified\r\n\t\r\n\tANSELA = 0b00000000;\t\t\t\/\/0 = digital\r\n\tANSELB = 0b00000000;\r\n\tANSELC = 0b00000000;\r\n<\/code><\/pre>\n<h4>\nUsing AtoD<br \/>\n<\/h4>\n<h5>\nInitialise<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t\/\/----- SETUP THE A TO D PINS -----\r\n\tVREFCON0 = 0b10110000;\t\t\t\/\/FVR enabled, set to 4.096V\r\n\tADCON0 = 0b00000001;\t\t\t\/\/AtoD enabled\r\n\tADCON1 = 0b00001000;\t\t\t\/\/Vref connected to FVR BUF2\r\n\tADCON2 = 0b10111010;\t\t\t\/\/Right justified, ADCS=010 (Needs setting for osc)\r\n\t\r\n\tANSELA = 0b00000011;\t\t\t\/\/0 = digital\r\n\tANSELB = 0b00000000;\r\n\tANSELC = 0b00000000;\r\n\t\r\n\tADCON0bits.GO = 1;\t\t\t\t\/\/Start a conversion\r\n<\/code><\/pre>\n<h5>\nRead Values<br \/>\n<\/h5>\n<pre>\r\n<code>\r\nWORD atod_result_0;\r\nWORD atod_result_1;\r\n\r\n\/\/************************************\r\n\/\/************************************\r\n\/\/********** PROCESS A TO D **********\r\n\/\/************************************\r\n\/\/************************************\r\nvoid process_atod (void)\r\n{\r\n\tWORD atod_result;\r\n\tBYTE b_temp;\r\n\tstatic BYTE result_count = 0;\r\n\r\n\r\n\r\n\t\/\/Exit if last conversion is not complete\r\n\tif (BusyADC())\r\n\t\treturn;\r\n\r\n\t\/\/Get result of last conversion\r\n\tatod_result = ReadADC();\r\n\r\n\tb_temp = (ADCON0 &gt;&gt; 2) &amp; 0x0f;\r\n\r\n\tswitch (b_temp)\r\n\t{\r\n\tcase 0:\r\n\t\t\/\/--------------------\r\n\t\t\/\/----- AN0 DONE -----\r\n\t\t\/\/--------------------\r\n\t\tif (result_count == 0)\r\n\t\t{\r\n\t\t\tatod_result_0 = 0;\r\n\t\t\tatod_result_1 = 0;\r\n\t\t}\r\n\r\n\t\tatod_result_0 += atod_result;\r\n\r\n\t\tADCON0 = 0x01 | (1 &lt;&lt; 2);\t\/\/Set AN1 input\r\n\t\tbreak;\r\n\r\n\tcase 1:\r\n\t\t\/\/--------------------\r\n\t\t\/\/----- AN1 DONE -----\r\n\t\t\/\/--------------------\r\n\t\tatod_result_1 += atod_result;\r\n\r\n\t\tresult_count++;\t\t\t\/\/&lt;&lt;&lt;&lt;DO ON LAST ONE ONLY\r\n\r\n\t\tADCON0 = 0x01 | (0 &lt;&lt; 2);\t\/\/Set AN0 input\r\n\t\tbreak;\r\n\r\n\r\n\tdefault:\r\n\t\tADCON0 = 0x01 | (0 &lt;&lt; 2);\t\/\/Set AN0 input\r\n\t\tbreak;\r\n\t} \/\/switch (b_temp)\r\n\r\n\r\n\t\/\/----- START NEXT CONVERSION -----\r\n\tADCON0bits.GO = 1;\t\t\t\t\/\/Start a conversion\r\n\r\n\r\n\r\n\tif (result_count &gt;= 16)\t\t\/\/&lt;&lt;&lt;Set number of readings to average (adjust the &gt;&gt;= below to match)\r\n\t{\r\n\t\t\/\/------------------------------------\r\n\t\t\/\/------------------------------------\r\n\t\t\/\/----- DONE NEW SET OF READINGS -----\r\n\t\t\/\/------------------------------------\r\n\t\t\/\/------------------------------------\r\n\t\tresult_count = 0;\r\n\t\tatod_result_0 &gt;&gt;= 4;\r\n\t\tatod_result_1 &gt;&gt;= 4;\r\n\r\n\t\tcomms_tx_current_sense = atod_result_0;\r\n\t\tcomms_tx_voltage_sense = atod_result_1;\r\n\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Not using AtoD \/\/&#8212;&#8211; SETUP THE A TO D PINS &#8212;&#8211; ADCON0 = 0b00000000; \/\/AtoD disabled \/\/ADCON1 = 0b00000000; \/\/ADCON2 = 0b10111110; \/\/Right justified ANSELA = 0b00000000; \/\/0 = digital ANSELB = 0b00000000; ANSELC = 0b00000000; Using AtoD Initialise \/\/&#8212;&#8211; SETUP THE A TO D PINS &#8212;&#8211; VREFCON0 = 0b10110000; \/\/FVR enabled, set to 4.096V [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-779","post","type-post","status-publish","format-standard","hentry","category-atod-xc8-compiler"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/comments?post=779"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/779\/revisions"}],"predecessor-version":[{"id":783,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/779\/revisions\/783"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}