{"id":3045,"date":"2011-07-04T10:13:46","date_gmt":"2011-07-04T14:13:46","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=3045"},"modified":"2012-07-30T09:29:17","modified_gmt":"2012-07-30T13:29:17","slug":"using-fixed-voltage-reference-fvr-for-ad-conversion-in-enhanced-mid-range-pic-microcontrollers","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/using-fixed-voltage-reference-fvr-for-ad-conversion-in-enhanced-mid-range-pic-microcontrollers\/","title":{"rendered":"Using Fixed Voltage Reference (FVR) for A\/D conversion in enhanced mid-range PIC microcontrollers"},"content":{"rendered":"<p>The reference voltage plays a very important role in any A\/D conversion. It determines both the range and the resolution (Volt\/Count) of the A\/D conversion. Besides, the accuracy of the conversion also depends upon how stable the reference voltage is. Usually in PIC microcontrollers, the reference voltage for A\/D conversion can be selected as the supply voltage itself or provided externally through one or more I\/O pins. But the new enhanced mid-range family of 8-bit PIC microcontrollers have a built-in module that generates a stable reference voltage internally. It is called Fixed Voltage Reference (FVR) where the output is very stable and independent of the supply voltage (V<sub>DD<\/sub>). The output of the FVR can be configured to supply a reference voltage for A\/D conversion internally. This article describes how to configure the FVR module to derive the reference voltage of 2.048 V for A\/D conversion in PIC16F1827 microcontroller. The analog signal for this experiment is taken from the output of a LM34DZ temperature sensor. After the A\/D conversion, the PIC16F1827 displays the temperature on a 2&#215;8 character LCD.<\/p>\n<div id=\"attachment_3055\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVR_LM34_Title.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3055\" class=\"size-full wp-image-3055\" title=\"FVR_LM34_Title\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVR_LM34_Title.jpg\" alt=\"\" width=\"560\" height=\"331\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVR_LM34_Title.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVR_LM34_Title-550x325.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3055\" class=\"wp-caption-text\">Using internal FVR for A\/D conversion of LM34 analog output<\/p><\/div>\n<p><!--more--><\/p>\n<p><strong>Theory<\/strong><\/p>\n<p><em>LM34DZ temperature sensor<\/em><\/p>\n<div>The LM34 series are precision integrated-circuit temperature\u00a0sensors from National Semiconductors, whose output voltage is linearly proportional to the\u00a0Fahrenheit temperature.\u00a0They do not require any external calibration to\u00a0provide typical accuracies of \u00b11?2?F at room\u00a0temperature and \u00b11.5?F over a full ?50 to +300?F temperature range.\u00a0The LM34DZ is available in a TO-92 case and the relationship between the linear output voltage and the temperature is 10 millivolts per \u00b0F. That is, at 75\u00b0F it&#8217;s output reads 75 * 10 mV = 750 mV. For full range of measurement, the output of LM34DZ goes from -0.50V (-50\u00a0?F) to 3.0V (300\u00a0?F). We are not using any negative voltage source in this experiment, and therefore LM34DZ won&#8217;t be able to measure temperature below 0\u00a0?F. Similarly, on the upper side, the measurement could go up to 300\u00a0?F or less if the\u00a0\u00a0positive reference voltage for the A\/D conversion process is less than 3.0 V. Find more details about LM34 in its <strong><a href=\"www.national.com\/ds\/LM\/LM34.pdf\" target=\"_blank\">datasheet<\/a><\/strong>.<\/div>\n<div id=\"attachment_3048\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/LM34DZ.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3048\" class=\"size-full wp-image-3048\" title=\"LM34DZ\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/LM34DZ.png\" alt=\"\" width=\"550\" height=\"225\" \/><\/a><p id=\"caption-attachment-3048\" class=\"wp-caption-text\">LM34DZ pin diagrams<\/p><\/div>\n<p><em>Fixed Voltage Reference (FVR) in PIC16F1827<\/em><\/p>\n<p>PIC16F1827 is a member of Microchip&#8217;s enhanced mid-range 8-bit microcontroller family. It is pin-compatible with the\u00a0popular\u00a018-pin predecessors such as PIC16F628A and PIC16F88, but is equipped with lot more peripherals and other features.\u00a0The Fixed Voltage Reference (FVR) module in PIC16F1827 generates a stable\u00a0voltage reference internally. The FVR output provides three software selectable voltage levels, 1.024V, 2.048V and 4.096V.\u00a0The output can be configured to supply a reference\u00a0voltage to the following:<\/p>\n<div id=\"_mcePaste\">\u2022 ADC input channel<\/div>\n<div id=\"_mcePaste\">\u2022 ADC positive reference<\/div>\n<div id=\"_mcePaste\">\u2022 Comparator positive input<\/div>\n<div id=\"_mcePaste\">\u2022 Digital-to-Analog Converter (DAC)<\/div>\n<div id=\"_mcePaste\">\u2022 Capacitive Sensing (CPS) module<\/div>\n<div>The actual generated reference voltage is 1.024 V, but with the help of programmable gain amplifiers, it can be amplified by 1x (1.024 V), 2x (2.048 V), or 4x (4.096 V), to produce the three possible voltage levels. The FVRCON register (shown below) is used to configure the settings for the fixed voltage reference. The bit values of FVRCON register to generate 2.048 V as the positive voltage reference for A\/D conversion are shown in blue color font.<\/div>\n<div>\n<div id=\"attachment_3050\" style=\"width: 585px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVRCON.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3050\" class=\"size-full wp-image-3050\" title=\"FVRCON\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVRCON.png\" alt=\"\" width=\"575\" height=\"469\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVRCON.png 575w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVRCON-550x448.png 550w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/a><p id=\"caption-attachment-3050\" class=\"wp-caption-text\">FVRCON register (the bit values shown configures FVR to 2.048 V for ADC purpose)<\/p><\/div>\n<p>Once the FVRCON register is configured, the choice of reference voltage for A\/D conversion is made through ADCON1 control register. By setting ADPREF&lt;1:0&gt; bits to &#8216;1&#8217;, the positive reference voltage for A\/D conversion is derived from the internal FVR module. Clearing the ADNREF bit connects the A\/D negative reference voltage pin to the ground (V<sub>SS<\/sub>).<\/p>\n<div id=\"attachment_3057\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ADCON1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3057\" class=\"size-full wp-image-3057\" title=\"ADCON1\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ADCON1.png\" alt=\"\" width=\"570\" height=\"494\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ADCON1.png 570w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ADCON1-550x476.png 550w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><p id=\"caption-attachment-3057\" class=\"wp-caption-text\">Use ADCON1 register to select the reference voltage source for ADC<\/p><\/div>\n<p>Once the reference voltage is selected, the rest of the A\/D conversion process is similar to any other PIC microcontroller.<\/p>\n<p><strong><em><span style=\"color: #003366;\">Why the reference voltages are 1.024, 2.048, or 4.096 V?<\/span><\/em><\/strong><\/p>\n<p><span style=\"color: #003366;\">The reason for the reference voltage to be non-integer numbers like these is to provide a precise resolution in A\/D conversion. For example, if the positive A\/D reference is 2.048 V and the negative reference is 0 V, then the resolution of 10-bit A\/D conversion is 2.048 V\/ 1024 = 2 mV\/Count. This means, in order to get the analog value (in mV) back from the digital count, you just need to multiply the count by 2. Similarly, the positive reference voltage of 4.096 V gives the conversion resolution of 4 mV\/Count. This makes the calculations easier and accurate.<\/span><\/p>\n<p><strong>Circuit diagram<\/strong><\/p>\n<p>The circuit diagram for this experiment is simple. The LM34DZ and the microcontroller circuit are powered with a +5V supply. The output voltage of LM34DZ sensor is fed to the AN0 ADC channel of PIC16F1827. The microcontroller runs at 500 KHz internal clock. The LCD display is operating in 4-bit data mode. The D4-D7 data pins are driven through RB4-RB7 port pins, respectively. The RB2 (8) and RB3 (9) I\/O pins control the RS and E signals of LCD display. A 5K potentiometer is used for adjusting the contrast of the LCD display.<\/p>\n<div id=\"attachment_3703\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVR_LM34Ckt1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3703\" class=\"size-full wp-image-3703\" title=\"FVR_LM34Ckt\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVR_LM34Ckt1.jpg\" alt=\"\" width=\"570\" height=\"300\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVR_LM34Ckt1.jpg 570w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVR_LM34Ckt1-550x289.jpg 550w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><p id=\"caption-attachment-3703\" class=\"wp-caption-text\">Circuit diagram for reading the analog output from LM34DZ<\/p><\/div>\n<div>This circuit can be easily constructed on a solder-less breadboard. I am demonstrating this on my self-made <strong><a href=\"http:\/\/embedded-lab.com\/blog\/?p=2889\" target=\"_blank\">development board for PIC16F1827\/47 microcontrollers<\/a><\/strong>.<\/div>\n<div>\n<div id=\"attachment_3054\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVR_LM34_Setup.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3054\" class=\"size-full wp-image-3054\" title=\"FVR_LM34_Setup\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/FVR_LM34_Setup.jpg\" alt=\"\" width=\"550\" height=\"414\" \/><\/a><p id=\"caption-attachment-3054\" class=\"wp-caption-text\">Displaying temperature on LCD screen<\/p><\/div>\n<\/div>\n<p><strong>Software<\/strong><\/p>\n<p>The use of internal reference voltage for A\/D conversion requires configuration of FVRCON and ADCON1 registers. The <a href=\"http:\/\/www.mikroe.com\/eng\/products\/view\/7\/mikroc-pro-for-pic\/\" target=\"_blank\">mikroC Pro for PIC<\/a> compiler provides a library for A\/D conversion, but that uses the supply voltage, V<sub>DD<\/sub> , by default as the positive reference for the conversion. So the built-in ADC library of mikroC Pro for PIC is not useful for our case. The following program is written for PIC16F1827 without using the built-in ADC library and it allows to use the stable FVR output as the positive voltage reference for A\/D conversion. We will use 2.048 V reference voltage from the FVR module for the A\/D conversion. This will limit the maximum value of the input analog signal to 2.048 V. Since we are using the LM34DZ output for the analog signal, this limits the maximum value of the measurable temperature to 204.8?F. For measuring temperatures higher than that, the reference voltage of 4.096 may be used.<\/p>\n<p><strong><em>ADC Math<\/em><\/strong><\/p>\n<p><span style=\"color: #ff0000;\">ADC Resolution = 2.048 V\/1024 count = 2 mV\/count<\/span><\/p>\n<p><span style=\"color: #ff0000;\">=&gt; LM34DZ output voltage (mV) = 2 x count<\/span><\/p>\n<p><span style=\"color: #ff0000;\">Temperature (?F) = LM34 output voltage (mV)\/10<\/span><\/p>\n<p><span style=\"color: #ff0000;\">=&gt; <strong>Temperature (?F) = 2 x count\/10 = count\/5<\/strong><\/span><\/p>\n<pre>\/*\r\n  Description: Use internal 2.048 V FVR for A\/D conversion\r\n  of LM34DZ output\r\n  MCU: PIC16F1827, 500 KHz internal clock, MCLR enabled\r\n  Jul 2, 2011\r\n*\/\r\n\r\n\/\/ Define LCD connections\r\nsbit LCD_RS at RB2_bit;\r\nsbit LCD_EN at RB3_bit;\r\nsbit LCD_D4 at RB4_bit;\r\nsbit LCD_D5 at RB5_bit;\r\nsbit LCD_D6 at RB6_bit;\r\nsbit LCD_D7 at RB7_bit;\r\nsbit LCD_RS_Direction at TRISB2_bit;\r\nsbit LCD_EN_Direction at TRISB3_bit;\r\nsbit LCD_D4_Direction at TRISB4_bit;\r\nsbit LCD_D5_Direction at TRISB5_bit;\r\nsbit LCD_D6_Direction at TRISB6_bit;\r\nsbit LCD_D7_Direction at TRISB7_bit;\r\n\/\/ End LCD module connections\r\n\r\n\/\/ Define Messages\r\n char message1[] = \"Temp.\";\r\n char *Temp = \"000.0 F\";\r\n unsigned ADC_Value, Temp_F;\r\n\r\nvoid main() {\r\n\r\n ANSELA = 0b00000001;  \/\/ RA0 analog input\r\n TRISA  = 0b00100001;  \/\/ RA0, RA5 inputs\r\n ANSELB = 0b00000000;  \/\/ PORTB all digital outputs\r\n TRISB  = 0b00000000;  \/\/ PORTB all outputs\r\n\r\n \/\/ Configure FVR to 2.048 V for ADC\r\n  FVRCON = 0b11000010 ;\r\n\r\n  \/\/ Configure ADCON1\r\n  ADCON1.ADNREF  = 0;  \/\/ Vref- is connected to ground\r\n  ADCON1.ADPREF0 = 1;  \/\/ Vref+ is connected to internal FVR\r\n  ADCON1.ADPREF1 = 1;\r\n  ADCON1.ADCS0   = 0;  \/\/ Use conversion clock, Fosc\/2\r\n  ADCON1.ADCS1   = 0;  \/\/ Fosc = 500 KHz\r\n  ADCON1.ADCS2   = 0;\r\n  ADCON1.ADFM    = 1;  \/\/ result is right Justified\r\n\r\n  \/\/ Configure ADCON0 for channel AN0\r\n  ADCON0.CHS0 = 0;\r\n  ADCON0.CHS1 = 0;\r\n  ADCON0.CHS2 = 0;\r\n  ADCON0.CHS3 = 0;\r\n  ADCON0.CHS4 = 0;\r\n  ADCON0.ADON = 1;  \/\/ enable A\/D converter\r\n\r\n  \/\/ Initialize LCD\r\n  Lcd_Init();                  \/\/ Initialize LCD\r\n  Lcd_Cmd(_LCD_CLEAR);         \/\/ CLEAR display\r\n  Lcd_Cmd(_LCD_CURSOR_OFF);    \/\/ Cursor off\r\n  Lcd_Out(1,2,message1);       \/\/ Write message1 in 1st row\r\n\r\n do {\r\n\r\n  ADCON0.F1 = 1;        \/\/ start conversion, GO\/DONE = 1\r\n  while (ADCON0.F1);    \/\/ wait for conversion\r\n  ADC_Value = (ADRESH &lt;&lt; 8 ) + ADRESL;\r\n  Temp_F = ADC_Value * 10\/5;\r\n  Temp[0] = Temp_F\/1000 + 48;\r\n  Temp[1] = (Temp_F\/100)%10 + 48;\r\n  Temp[2] = (Temp_F\/10)%10 + 48;\r\n  Temp[4] = Temp_F%10 + 48;\r\n  if (Temp[0] == '0')                       \/\/ if Temp[0] = '0' then\r\n      Temp[0] = ' ';                        \/\/ insert blank character to Temp[0]\r\n  if (Temp[0] == ' ' &amp;&amp; Temp[1] == '0')     \/\/ if Temp[0] is blank and Temp[1] =\r\n      Temp[1] = ' ';                        \/\/ '0' then\r\n  Temp[5] = 223;\r\n  LCD_Out(2,2, Temp);\r\n  Delay_ms(1000);\r\n } while(1);\r\n}<\/pre>\n<p style=\"text-align: center;\"><strong><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC16F1827\/UsingInternalFVRforLM34.zip\" target=\"_blank\">Download the source code and HEX files<\/a><\/strong><\/p>\n<p><strong>Conclusions<\/strong><\/p>\n<p>A stable reference voltage is required in A\/D conversion systems for accurate and repeatable data conversion. While this can be achieved externally by using precision voltage regulators or zener diodes, the enhanced mid-range 8-bit PIC microcontrollers\u00a0facilitate this feature internally through the FVR module, thus avoiding the use of any external components. The use of internal FVR module also frees the external Vref pin, which means an extra GPIO pin. The microcontroller&#8217;s power supply voltage (typically +5V) itself can be used as the reference voltage for A\/D conversion. While this allows the maximum range for A\/D conversion, it could possible introduce errors in the conversion if the supply voltage is not stable (usually the supply voltage drifts with load conditions). Besides, if the range of the analog signal is much lesser than the supply voltage, it is always good to use a lower reference voltage for achieving higher resolution. The FVR module inside the 8-bit PIC microcontrollers of the enhanced mid-range family provides three stable and configurable reference voltages internally, which makes the design of data conversion systems more flexible and easier.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The reference voltage plays a very important role in any A\/D conversion. It determines both the range and the resolution (Volt\/Count) of the A\/D conversion. Besides, the accuracy of the conversion also depends upon how stable the reference voltage is. Usually in PIC microcontrollers, the reference voltage for A\/D conversion can be selected as the supply voltage itself or provided externally through one or more I\/O pins. But the new enhanced mid-range family of 8-bit PIC microcontrollers have a built-in module that generates a stable reference voltage internally. It is called Fixed Voltage Reference (FVR) where the output is very<\/p>\n","protected":false},"author":1,"featured_media":3048,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,183],"tags":[296,297,298,285],"class_list":["post-3045","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pic-tutorials","category-tips-and-tricks","tag-fixed-voltage-reference","tag-fvr","tag-lm35dz","tag-pic16f1827"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3045","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=3045"}],"version-history":[{"count":18,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3045\/revisions"}],"predecessor-version":[{"id":3066,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3045\/revisions\/3066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/3048"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=3045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=3045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=3045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}