{"id":379,"date":"2010-10-26T21:28:43","date_gmt":"2010-10-27T01:28:43","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=379"},"modified":"2012-07-30T09:29:32","modified_gmt":"2012-07-30T13:29:32","slug":"adc-channels-in-pic16f688","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/adc-channels-in-pic16f688\/","title":{"rendered":"ADC channels in PIC16F688"},"content":{"rendered":"<p>The PIC16F688 microcontroller has a built-in 10-bit ADC with eight input channels. The eight channels are available at RA0, RA1, RA2, RA4, RC0, RC1, RC2, and RC3. They have alternate labels, AN0-AN7, for this function, and are multiplexed into a single sample and Hold circuit. The\u00a0output of the sample and hold is\u00a0connected to the input of the A\/D converter. The A\/D conversion is successive approximation based and the 10-bit result is stored\u00a0\u00a0into the ADC result registers ADRESH (A\/D Result Higher byte) and ADRESL (A\/D Result Lower byte). Each of these registers is 8-bit.<\/p>\n<p style=\"text-align: center;\"><strong>Pin diagram of the PIC16F688 microcontroller<\/strong><\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-173 aligncenter\" title=\"PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688-500x158.png\" alt=\"\" width=\"500\" height=\"158\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688-500x158.png 500w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688.png 720w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<p style=\"text-align: center;\">\n<p style=\"text-align: center;\"><strong>Functional block diagram of ADC<\/strong><\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADC.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-388 aligncenter\" title=\"PIC16F688_ADC\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADC.png\" alt=\"\" width=\"563\" height=\"359\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADC.png 563w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADC-550x350.png 550w\" sizes=\"auto, (max-width: 563px) 100vw, 563px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">\n<p style=\"text-align: left;\">The functionality of the A\/D module is controlled by three registers: ANSEL, ADCON0, and ADCON1.<\/p>\n<p style=\"text-align: left;\">The ANSEL register is used to configure the input mode of an I\/O pin to analog. Setting the appropriate ANSEL bit High will cause all digital reads on the pin to be read as 0, and allow analog function on the pin to operate correctly. The state of ANSEL bits has no effect on digital O\/P function. A pin with TRIS bit clear and ANSEL bit set will operate as digital O\/P but the input mode will be analog.<\/p>\n<p style=\"text-align: center;\"><strong>ANSEL register bits<\/strong><\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ANSEL.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-389\" title=\"PIC16F688_ANSEL\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ANSEL-550x311.png\" alt=\"\" width=\"550\" height=\"311\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ANSEL-550x311.png 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ANSEL.png 609w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">\n<p>The ADCON0 register selects which analog input is to be measured. This is required, since there are several channels for\u00a0analog input but only one A\/D converter circuitry.\u00a0The channel select bits CHS0-CHS2 of ADCON0 register controls which analog channel is connected to the internal Sample and Hold circuit. For example, if you want to measure input analog voltage at AN2 (RA2) pin, you should set CHS1, and clear CHS0 and CHS2. The reference voltage used in the A\/D conversion process is software selectable to either Vdd or a voltage applied to the Vref pin (12). This is controlled by VCFG bit of ADCON0 register. If VCFG is set, the reference voltage for the A\/D conversion will be the voltage applied at the Vref pin. Bit 0 (ADON) of ADCON0 is the On\/Off switch for the A\/D converter. When it is set, the ADC is On, and the PIC microcontroller consumes extra current. GO\/DONE bit of ADCON0 is set to start an A\/D conversion cycle. It is automatically cleared by hardware when the conversion is done. This bit can, therefore, be tested to see if the A\/D conversion is completed or in progress.<\/p>\n<p style=\"text-align: center;\"><strong>ADCON0 register bits<\/strong><\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADCON0.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-390 aligncenter\" title=\"PIC16F688_ADCON0\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADCON0-532x450.png\" alt=\"\" width=\"532\" height=\"450\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADCON0-532x450.png 532w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADCON0.png 749w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/a><\/p>\n<p>As mentioned earlier, the 10-bit digital output is held over two registers, ADRESH and ADRESL. You have a choice in how you want the 10-bit number stored into the two 8-bit registers. It can be right-justified by setting ADFM bit of ADCON0 so that bits 0:7 of the 10-bit result are held in ADRESL and bits 8:9 are stored in bits 0:1 of ADRESH. Similarly, clearing the ADFM bit shift the 10-bit output to the left. This is illustrated below.<\/p>\n<p style=\"text-align: center;\"><strong>Shifting the ADC result left or right with ADFM bit<\/strong><\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/ADFM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-475 aligncenter\" title=\"ADFM\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/ADFM.png\" alt=\"\" width=\"590\" height=\"187\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/ADFM.png 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/ADFM-550x174.png 550w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/ADFM.png\"><\/a><\/p>\n<p>The ADCON1 register also plays an important role in programming the A\/D module.\u00a0An ADC requires a clock source to operate. ADCS (A\/D conversion Clock Select) bits of ADCON1 register are the selection bits for the A\/D conversion clock. The conversion time per bit is defined as T<sub>AD<\/sub> in PIC documentation from Microchip.\u00a0The A\/D conversion cycle requires 11 T<sub>AD<\/sub>.\u00a0For correct conversion, the A\/D conversion clock\u00a0(1\/T<sub>AD<\/sub>) must be selected to ensure a minimum T<sub>AD<\/sub> of\u00a01.6 ?s. In PIC16F688, there are\u00a0seven possible clock options for A\/D conversion that are software selected, as shown below.<\/p>\n<p style=\"text-align: center;\"><strong>Software selectable clock sources for A\/D conversion<\/strong><\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADCON1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-385 aligncenter\" title=\"PIC16F688_ADCON1\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADCON1-550x290.png\" alt=\"\" width=\"550\" height=\"290\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADCON1-550x290.png 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADCON1.png 748w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">\n<p style=\"text-align: center;\"><strong>T<sub>AD<\/sub> calculations for selected frequencies.<\/strong><\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADC_Clk.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-386 aligncenter\" title=\"PIC16F688_ADC_Clk\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADC_Clk-550x231.png\" alt=\"\" width=\"550\" height=\"231\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADC_Clk-550x231.png 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/PIC16F688_ADC_Clk.png 644w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">\n<p><strong>Acquisition Time<\/strong><br \/>\nThe input of the ADC has a sample and hold circuit, to ensure that the voltage\u00a0sampled is constant during the conversion process. This contains a holding capacitor that stores the sampled input voltage.\u00a0Acquisition time is the amount time required to charge the holding capacitor on the<\/p>\n<div id=\"_mcePaste\">front end of an analog-to-digital converter. The holding capacitor must be given\u00a0sufficient time to settle to the analog input voltage level before the actual\u00a0conversion is initiated, otherwise\u00a0the\u00a0conversion will be inaccurate. The source impedance of the input voltage and the impedance of the internal sampling switch are the two major factors to determine the required acquisition time. An increase in the source impedance\u00a0will increase the required acquisition time. Therefore, the maximum recommended source impedance for analog sources is 10K. \u00a0A typical value of acquisition time is 20 \u00b5s.<\/div>\n<p>Programming the A\/D module consists of the following steps:<\/p>\n<p>1. Configure the analog\/digital I\/O using ANSEL register. All analog lines are initialized<br \/>\nas input in the corresponding TRIS registers.<br \/>\n2. Select the A\/D input channel by setting the CHSx bits in the ADCON0\u00a0register. Also, select right- or left-justification (ADFM bit), and the reference voltage (VCFG bit).<br \/>\n3. Select the A\/D conversion clock (ADCS bits), and enable the A\/D module (ADON bit).<br \/>\n4. Wait the acquisition time.<br \/>\n5. Initiate the conversion by setting the GO\/DONE bit in the ADCON0 register.<br \/>\n6. Wait for the conversion to complete.<br \/>\n7. Read and store the digital result.<\/p>\n<p>A minimum wait of 2 T<sub>AD<\/sub> is\u00a0required before the next acquisition starts.<\/p>\n<p>Reference: <strong><a href=\"http:\/\/ww1.microchip.com\/downloads\/en\/DeviceDoc\/41203D.pdf\" target=\"_blank\">Datasheet for PIC16F688 from Microchip<\/a><\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The PIC16F688 microcontroller has a built-in 10-bit ADC with eight input channels. The eight channels are available at RA0, RA1, RA2, RA4, RC0, RC1, RC2, and RC3. They have alternate labels, AN0-AN7, for this function, and are multiplexed into a single sample and Hold circuit. The\u00a0output of the sample and hold is\u00a0connected to the input of the A\/D converter. The A\/D conversion is successive approximation based and the 10-bit result is stored\u00a0\u00a0into the ADC result registers ADRESH (A\/D Result Higher byte) and ADRESL (A\/D Result Lower byte). Each of these registers is 8-bit. Pin diagram of the PIC16F688 microcontroller<\/p>\n","protected":false},"author":1,"featured_media":385,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,38],"tags":[44,50,49,20],"class_list":["post-379","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-lessons","category-pic-tutorials","tag-adc","tag-adcon0","tag-ansel","tag-pic16f688"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=379"}],"version-history":[{"count":15,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/379\/revisions"}],"predecessor-version":[{"id":5849,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/379\/revisions\/5849"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/385"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}