{"id":1087,"date":"2020-03-19T10:21:43","date_gmt":"2020-03-19T10:21:43","guid":{"rendered":"https:\/\/ibex.tech\/embedded\/?p=1087"},"modified":"2024-07-18T15:45:54","modified_gmt":"2024-07-18T14:45:54","slug":"adc-pic18","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic18\/xc8-compiler\/atod-xc8-compiler\/adc-pic18","title":{"rendered":"ADC (using MCC)"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">ADC Settings<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">FRC<\/h5>\n\n\n\n<p>Does your uC have a FRC to use as the clock source?  Nice and simple if it does. <\/p>\n\n\n\n<h5 class=\"wp-block-heading\">FVR<\/h5>\n\n\n\n<p>Does your uC have a FVR to use as the reference voltage?  Useful if you want an accurate reference, but not useful it you want your analog readings referenced to the uC power rail of course.<\/p>\n\n\n\n<p> You need to add it as a separate peripheral and select its multiplier to give the reference voltage you want <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Using in your code<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"mcc_generated_files\/adc.h\"\n\n\/\/INITIALISE\n\t\/\/Start AtoD Conversion\n\tADC_SelectChannel(channel_AN1);\n\tADC_StartConversion();\n\n\n\/\/PROCESS ATOD\n\n\t\/\/Exit if last conversion is not complete\n\tif (!ADC_IsConversionDone())\n\t\treturn;\n\n\t\/\/Get result of last conversion\n\tatod_result = (uint16_t)ADC_GetConversionResult();\n\n\t\/\/Start next measurement\n\tADC_SelectChannel(channel_AN1);\n\tADC_StartConversion();\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">An example function to handle AtoD measurements<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/************************************\n\/\/************************************\n\/\/********** PROCESS A TO D **********\n\/\/************************************\n\/\/************************************\nvoid ProcessAtoD (void)\n{\n\tuint16_t AtodResult;\n\tuint8_t u8Ttemp;\n\n\t\/\/Exit if last conversion is not complete\n\tif (!ADC_IsConversionDone())\n\t\treturn;\n\n\t\/\/Get result of last conversion\n\tAtodResult = (uint16_t)ADC_GetConversionResult();\n\n\t\/\/0-5V = 0-1023\n\t\/\/Arduino is 0-3.3V\n\t\/\/1.65V = 337\n\t\n\tu8Ttemp = (uint8_t)((ADCON0 >> 2) &amp; 0x0f);\n\tswitch (u8Ttemp)\n\t{\n\tcase 0:\n\t\t\/\/--------------------\n\t\t\/\/----- AN0 DONE -----\n\t\t\/\/--------------------\n\t\t\/\/if (AtodResult > 562)\n\n\t\tADC_SelectChannel(channel_AN1);\n\t\tbreak;\n\t\t\n\tcase 1:\n\t\t\/\/--------------------\n\t\t\/\/----- AN1 DONE -----\n\t\t\/\/--------------------\n\t\t\/\/if (AtodResult > 562)\n\n\t\tADC_SelectChannel(channel_AN2);\n\t\tbreak;\n\n\tcase 2:\n\t\t\/\/--------------------\n\t\t\/\/----- AN2 DONE -----\n\t\t\/\/--------------------\n\t\t\/\/AtodResult\n\n\t\tADC_SelectChannel(channel_AN0);\n\t\tbreak;\n\n\n\tdefault:\n\t\tADC_SelectChannel(channel_AN0);\n\t\tbreak;\n\t} \/\/switch (u8Ttemp)\n\n\t\/\/----- START NEXT CONVERSION -----\n\tADC_StartConversion();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ADC Settings FRC Does your uC have a FRC to use as the clock source? Nice and simple if it does. FVR Does your uC have a FVR to use as the reference voltage? Useful if you want an accurate reference, but not useful it you want your analog readings referenced to the uC power [&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,131],"tags":[],"class_list":["post-1087","post","type-post","status-publish","format-standard","hentry","category-atod-xc8-compiler","category-pic18-peripherals"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1087","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=1087"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1087\/revisions"}],"predecessor-version":[{"id":1561,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/1087\/revisions\/1561"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=1087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=1087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=1087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}