{"id":9488,"date":"2014-12-30T13:12:59","date_gmt":"2014-12-30T18:12:59","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=9488"},"modified":"2016-12-18T19:44:01","modified_gmt":"2016-12-19T00:44:01","slug":"xmega-adc","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/xmega-adc\/","title":{"rendered":"XMega ADC"},"content":{"rendered":"<p>For users of advance MCUs like the XMega it is not necessary to tell what an analog-to-digital converter (ADC) is or what it does. I assume this is not the first family of microcontroller they are dealing with. Unlike the ADCs of other microcontroller the ADC of XMega devices is a highly complex tool. The level of complexity is so much that without understanding every bits-and-pieces of this piece of hardware a user won\u2019t enjoy its absolute power. XMega ADC is also the most confusing hardware as it is not like other MCU ADCs. We will be dealing with ATXMega32A4U and it has only one ADC block, named <b><i>ADCA<\/i><\/b> but some other XMega devices like the XMega128A1 have more than one ADC block \u2013 <b><i>ADCA<\/i><\/b> and <b><i>ADCB<\/i><\/b>. By the way the XMega reference manual provides a long literature on the ADC and I\u2019m not willing to state everything.<\/p>\n<p>A quick view of the ADC block diagram shows most of the internal arrangement.<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/adc-internal\/\" rel=\"attachment wp-att-9529\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9529\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/ADC-internal.png\" alt=\"ADC internal\" width=\"590\" height=\"295\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/ADC-internal.png 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/ADC-internal-550x275.png 550w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/p>\n<p><!--more-->To begin with, there are some prerequisites. The very first thing is to realize the inputs of the ADC block (VINP and VINN) as like the two inputs of a regular op-amp. The ADC is differential in nature. Secondly there are several areas that need more than a few lines of \u201cATMEL\u201d explanation and several assumptions. We can\u2019t see the internal circuitry after all. Take things initially as if they are in black boxes and then slowly but systematically probe them until fully unveiled. This is somewhat a my-kind of learning technique. Thirdly a word of caution, an ADC channel doesn\u2019t necessarily mean a physical ADC pin as in traditional MCUs. In the XMega32A4U there\u2019s one physical ADC &#8211;<b><i> ADCA<\/i><\/b> comprising of four virtual channels that can be connected to a number of signal sources both internal and external (physical pins). Lastly we won\u2019t be using 8 bit ADC conversions in our discussion anywhere as it is less precise and seldom used. However 8 bit conversions are faster than 12 bit ones.<\/p>\n<h4><span style=\"color: #222222;font-size: 1em;font-weight: bold;line-height: 1em\"><br \/>\nKey Pros and Cons of XMega ADCs<\/span><\/h4>\n<p><b><i><br \/>\nPros:<\/i><\/b><\/p>\n<ul>\n<li>Pipeline Architecture \u2013 Simply this feature allows one sample to be processed while another is being acquired.\u00a0 Here\u2019s a conceptual view of how this stuff works.<\/li>\n<\/ul>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/pipeline\/\" rel=\"attachment wp-att-9496\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9496\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/pipeline.png\" alt=\"pipeline\" width=\"578\" height=\"672\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/pipeline.png 578w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/pipeline-550x639.png 550w\" sizes=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/a><\/p>\n<ul>\n<li>ADC\u2019s power consumption can be controlled.<\/li>\n<li>High sampling rate (2Msps) and high resolution (12 bits).<\/li>\n<li>Several options for reference source.<\/li>\n<li>Conversion frequency can be set by pre-scaling peripheral clock.<\/li>\n<li>Signed\/unsigned modes.<\/li>\n<li>Differential and single-end measurements.<\/li>\n<li>Front-end gain stage for differential modes, allowing amplification of small signals.<\/li>\n<li>ADC conversion can be triggered by events, timer interrupts, DMA requests and so on.<\/li>\n<\/ul>\n<p><b><i>Cons:<\/i><\/b><\/p>\n<ul>\n<li>Unlike ordinary MCUs like the traditional AVR ATMega MCUs, ADC reference can\u2019t be more than (VDD \u2013 0.6) volts even if the reference source is externally connected.<\/li>\n<li>Calibration values are stored in signature row and these must be manually read and written to ADC. Again this feature is not common in the most popular microcontrollers.<\/li>\n<li>In terms of volts per ADC count and general perception, the ADC is not really 12bit but rather somewhat 11 bits or some less than 12 bit. More explanations later.<\/li>\n<li>In the unsigned single-ended mode of operation a small amount of positive offset is internally added to detect zero-cross. This is the reason why people will not get zero count at zero voltage. You will rather get a count of about 200 or so at zero voltage. This offset should be compensated in the software end and in the process the ADC is no long a 12 bit ADC for general purpose uses. Owing to this offset the maximum value of voltage the ADC can measure is also below the reference voltage. However a tiny amount of negative voltage can be detected in this mode.<\/li>\n<\/ul>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/adc-characteristics-2\/\" rel=\"attachment wp-att-9530\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9530\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/ADC-Characteristics1.png\" alt=\"ADC Characteristics\" width=\"590\" height=\"226\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/ADC-Characteristics1.png 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/ADC-Characteristics1-550x210.png 550w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><br \/>\n<\/a><\/p>\n<h4>Differential Mode<\/h4>\n<p>People who have dealt with instrumentations or analog electronics will certainly know what a differential amplifier is. Well basically it just amplifies\/passes the difference in signal between its two input terminals. Simply <b><i>Vout <\/i><\/b>= <b><i>m<\/i><\/b> x (<b><i>Vinp<\/i><\/b> \u2013 <b><i>Vinn<\/i><\/b>), where <b><i>m<\/i><\/b> is the gain, <b><i>Vinp<\/i><\/b> is the positive input and <b><i>Vinn<\/i><\/b> is the negative input. In the XMega ADC block this differential mode is technically the same stuff. You have two ADC pins as inputs in differential mode and an optional gain stage. Typically use of\u00a0 differential mode is that for measuring current flowing through a shunt. However a few things are to be strictly followed:<\/p>\n<ul>\n<li>Only signed conversion is possible in differential mode. Thus the 12bit ADC count has a range from -2048 to 2047, centering at 0.<\/li>\n<li>All pins can be positive inputs but only a few specified and be negative inputs. Only ADC0 \u2013 ADC3 pins can be negative input pins in differential mode without gain while only ADC4 \u2013 ADC7 pins can be negative input pins in differential mode with gain. Other than these pins, negative inputs can also be tied with either the internal or pad (external) ground. When the negative input is tied to ground and an ADC input is used, the result is a signed single-ended ADC.<\/li>\n<li>Gain can be set according to need but the value of gain is a multiple of 2, ranging 0.5x to 64x.<\/li>\n<li>Additional gain stage adds additional propagation delays.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/differential-without-gain\/\" rel=\"attachment wp-att-9494\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9494\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/differential-without-gain.png\" alt=\"differential without gain\" width=\"595\" height=\"361\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/differential-without-gain.png 595w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/differential-without-gain-550x333.png 550w\" sizes=\"auto, (max-width: 595px) 100vw, 595px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/differential-with-gain\/\" rel=\"attachment wp-att-9493\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9493\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/differential-with-gain.png\" alt=\"differential with gain\" width=\"727\" height=\"355\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/differential-with-gain.png 727w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/differential-with-gain-550x268.png 550w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h4>Single-Ended Mode<\/h4>\n<p>This is what people use mostly. Unlike the differential mode it has both signed and unsigned conversion mode of operation. The only difference between these modes is to what the negative terminal of the ADC block is connected to. Apparently single-ended mode seems to be the easiest and simple mode of operation but trust me this mode is where people get their head blown off. It took several days for me to understand this mode completely. Before going on further please note all of the figures, tables and formulae are taken from Atmel\u2019s official docs and so nothing is based on imagination.<\/p>\n<p>The key region of confusion of unsigned single-ended conversion mode is the <b><i>\u2206V<\/i><\/b> offset. Its physical value is about 5% of the reference and so if the reference is 1.0V then its value is 0.05V. Remember what I said about differential amp earlier? If so then check the block diagram shown below.<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/single_ended_unsigned\/\" rel=\"attachment wp-att-9498\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9498\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/single_ended_unsigned.png\" alt=\"single_ended_unsigned\" width=\"627\" height=\"217\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/single_ended_unsigned.png 627w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/single_ended_unsigned-550x190.png 550w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The negative input is connected to some sort of reference. This is the first piece of confusion and things get more complicated to realize when you go through the formula for unsigned single-ended ADC conversion, which is:<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/formula-1\/\" rel=\"attachment wp-att-9495\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9495\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Formula-1.png\" alt=\"Formula 1\" width=\"398\" height=\"133\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Note that the formula and the block diagram don\u2019t technically agree with each other. Throughout all Atmel\u2019s technical docs I found the same stuff over and over again with no elaborate explanation. However what I found during my research is that the formula is consistent. I didn\u2019t find any answer to the missing <b><i>VREF \/ 2<\/i><\/b> part. My first question was why it is connected to some sort of offset rather than zero. The answer is somewhat both easy and complicated. By having the ADC configured internally this way an XMega ADC pin connected to ground (0.0V) does not give 0 ADC count but about 200 ADC counts. I thought it was a hardware bug and that the ADC is not truly a 12 bits ADC but somewhat less than that. The more I researched the more I found it to be useful. If 0V in an ADC pin results about 200 counts then definitely a small negative voltage at that ADC pin will result 0 ADC count. According to Atmel this offset helps to detect zero-crossing or true 0V rather than something near to 0V like \u00b10.001. I found that the XMega ADC can, in fact, detect a tiny quantity of negative potential. Usually we don\u2019t use this feature with common ADC uses but in certain precision instrumentations we need to know this and there this offset plays its part. If you check XMega datasheet then you\u2019ll notice that ADC input voltage range spans from -0.1V to (AVCC + 0.1) V. Though this feature has its advantage in some certain areas it comes at a cost of reduced top range which about 95% of the reference source\u2019s value. If, for example, the internal 1.0V reference is used then the ADC can measure voltages ranging from about -0.05V to about 0.95V. Thus it is still measuring a 1.0V span with 12 bits precision though apparently it may not look like that at first experience. Fortunately MikroC PRO for AVR compiler\u2019s ADC library is based on this mode of operation and so be aware not to curse yourself.<\/p>\n<p>In the signed single-ended mode VINN is connected to ground and it is quite a simple story. In the signed mode the result of an ADC conversion is always 11bits. The MSB is used for sign. However you cannot apply negative voltage to an ADC pin with respect to ground under any circumstances except what your device\u2019s datasheet states. You\u2019ll get 0 \u2013 2047 counts for 0V \u2013 VREF range.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/single_ended_signed\/\" rel=\"attachment wp-att-9497\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9497\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/single_ended_signed.png\" alt=\"single_ended_signed\" width=\"577\" height=\"213\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/single_ended_signed.png 577w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/single_ended_signed-550x203.png 550w\" sizes=\"auto, (max-width: 577px) 100vw, 577px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h4>My Experiences and Findings<\/h4>\n<p>I found a number of stuffs that I feel needs to be shared apart from whatever I already stated. In my experience the internal 1.0V source is not good enough as a reference source for ADC because it needs a lot of caution and even some people claim that Atmel said that this source is technically too low for an ADC reference. I did try to use it but I found it to be too sensitive to noise. At this point Atmel recommended to shut down all operations of the MCU or go to sleep mode during capturing an ADC sample. This may not be always possible. There are other options for reference source but I found out that the best one is the <b><i>VCC \/ 1.6<\/i><\/b> reference source. With 3.3V VCC, it is about 2.06V. However a lot of care is needed to ensure a good stable VCC. Since a MCU is a digital device there will be switching noise across the power lines and to reduce them we need to follow basic EMI and PCB routing principles. AVCC pins should be connected to VCC via a ferrite bead and a 100nF capacitor. All other VDD pins should have 100nF capacitors connected between them and ground. Another good solution for ADC reference source is to use external reference sources like TL431 and similar.<\/p>\n<p>Since the ADC is of 12 bit resolution and the reference source is less than 3.3V, the ADC block will tend to pick up noises and tiny fluctuations. The ADC count may vary as much as 60 counts. To avoid these we need to follow recommendations from Atmel and apply some basic tips. ADC values should be averaged in the software end. If possible ADC pins should be buffered and filtered externally with proper low pass LC filter. XMega ADCs offer high impedance and low impedance modes along with other options to control ADC power consumption. Temperature compensation can also be used to improve ADC accuracy. If properly configured a lot of problem is dealt with easily.<\/p>\n<p>&nbsp;<\/p>\n<p>A must do process is to get rid of any offset before using the ADC for actual measurements. This can be done in two ways. First is to ground any ADC pin that will be used for taking measurements later, get the ADC offset with that pin and then use it normally as it was intended. This offset is common and so it is not needed to measure the offset for all ADC pins. The other method is to dedicate an ADC pin to ground and it will never be disconnected from ground. This will measure the offset only. In terms of pin count this method is expensive but it is the most hassle-free process. Actually I prefer this method.<\/p>\n<p>&nbsp;<\/p>\n<p>In most applications the signed single-ended mode is the best to use. The other is to use differential mode without gain and negative input tied to ground. When using differential mode with gain be aware of noise because noise also gets amplified along with signal. Be sure to select proper ADC sampling frequency. For general purpose uses something from 60 \u2013 150kHz as sampling rate is good enough. Please also note that Atmel recommends that the user must access and load factory calibration bytes for\u00a0 proper operation of the ADC. Frankly speaking in all of the XMega MCUs I have at my disposal, I found that the calibration bytes for ADC in them are 0x00. I don\u2019t find it necessary to load these meaningless values but I still used them in my coding to go by Atmel\u2019s recommended way.<\/p>\n<p>&nbsp;<\/p>\n<p>Before using a ADC pin make sure that it is a floating pin. If you have read my previous post on XMega I\/O ports then you must know how to do it.<\/p>\n<p>&nbsp;<\/p>\n<h4>Code Examples<\/h4>\n<p>Just as before, I added a header file for ADC along with the header files for clock and I\/O ports. This will greatly reduce coding time and efforts. In my coding I followed a systematic procedure to configure the MCU. I recommend following this method. First configure the XMega clock, then I\/O port pins and finally the ADC itself. In all of my example codes the XMega CPU is configured to run at 8MHz clock speed and only PORTA pins are used for ADC inputs. A LCD with the help of MikroC\u2019s library provided a mean to see ADC data during tests.. As always using MikroC\u2019s libraries are fun, fast and easy but they come at the cost of reduced customizations and relatively larger code sizes. Shown below is the basic setup:<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/schematic\/\" rel=\"attachment wp-att-9522\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9522\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Schematic.png\" alt=\"Schematic\" width=\"590\" height=\"365\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Schematic.png 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Schematic-550x340.png 550w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h6><strong><em>XMega ADC with MikroC\u2019s Library<\/em><\/strong><\/h6>\n<p><i>\u00a0<\/i><\/p>\n<pre><span style=\"color: #0000ff\"><i>#include &lt;io.h&gt;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include &lt;clock.h&gt;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_RS at PORTC_OUT.B2;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_EN at PORTC_OUT.B3;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D4 at PORTC_OUT.B4;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D5 at PORTC_OUT.B5;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D6 at PORTC_OUT.B6;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D7 at PORTC_OUT.B7;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_RS_Direction at PORTC_DIR.B2;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_EN_Direction at PORTC_DIR.B3;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D4_Direction at PORTC_DIR.B4;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D5_Direction at PORTC_DIR.B5;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D6_Direction at PORTC_DIR.B6;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D7_Direction at PORTC_DIR.B7;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int adc_avg(unsigned char no_of_samples, unsigned char channel);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void lcd_print(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char cnt_volt);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>float map(float v, float x_min, float x_max, float y_min, float y_max);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void main()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 signed int adc = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 signed int offset = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 float v = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 offset = adc_avg(100, 0);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_out(1, 1, \"V:\");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_out(2, 1, \"ADC:\");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(1)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 adc = adc_avg(50, 1);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 v = map(adc, offset, 4095.0, 0, 1959.375);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_print(4, 1, v, 0);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_print(5, 2, adc, 1);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 delay_ms(600);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 };<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32KEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32KRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32MEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CLK_PSCTRL = ((CLK_PSCTRL &amp; (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm | CLK_PSBCDIV0_bm)))<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | CLK_PSADIV_1_gc | CLK_PSBCDIV_2_2_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_DFLLCTRL = ((OSC_DFLLCTRL &amp; (~(OSC_RC32MCREF_gm | OSC_RC2MCREF_bm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 OSC_RC32MCREF_RC32K_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 DFLLRC32M_CTRL |= DFLL_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32MRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CLK_CTRL = ((CLK_CTRL &amp; (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_RC32M_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL &amp;= (~(OSC_RC2MEN_bm | OSC_XOSCEN_bm | OSC_PLLEN_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTCFG_CLKEVOUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTD_OUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTD_DIR = 0x02;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTD_PIN0CTRL = (PORT_OPC_BUSKEEPER_gc | PORT_ISC_BOTHEDGES_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTD_PIN1CTRL = (PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_Init_Advanced(_ADC_12bit, _ADC_INTERNAL_REF_VCC);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 Lcd_Init();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 Lcd_Cmd(_LCD_CLEAR);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 Lcd_Cmd(_LCD_CURSOR_OFF);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int adc_avg(unsigned char no_of_samples, unsigned char channel)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 signed long avg = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 unsigned char samples = no_of_samples;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 while(samples &gt; 0)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 avg += ADCA_Get_Sample(channel);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 samples--;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 avg \/= no_of_samples;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 return avg;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void lcd_print(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char cnt_volt)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 unsigned char tmp = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 if(value &gt; 0)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_out(y_pos, x_pos, \" \");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 else<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_out(y_pos, x_pos, \"-\");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 value *= -1;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 tmp = (value \/ 1000);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp(tmp + 48);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 switch(cnt_volt)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 1:<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 default:<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp(46);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 tmp = ((value \/ 100) % 10);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 tmp = ((value \/ 10) % 10);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 tmp = (value % 10);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>} <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>float map(float v, float x_min, float x_max, float y_min, float y_max)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 float m = 0.0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 m = ((y_max - y_min)\/(x_max - x_min));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 return<\/i> (y_min + (m * (v - x_min)));<\/span><\/pre>\n<pre><span style=\"color: #0000ff\">}<\/span><\/pre>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/mikroc-library-2\/\" rel=\"attachment wp-att-9521\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9521\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/MikroC-Library-2.jpg\" alt=\"MikroC Library (2)\" width=\"590\" height=\"597\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/MikroC-Library-2.jpg 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/MikroC-Library-2-550x556.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/MikroC-Library-2-90x90.jpg 90w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/mikroc-library-1\/\" rel=\"attachment wp-att-9520\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9520\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/MikroC-Library-1.jpg\" alt=\"MikroC Library (1)\" width=\"590\" height=\"650\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/MikroC-Library-1.jpg 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/MikroC-Library-1-550x605.jpg 550w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/p>\n<p>Video Link:\u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=xB7MZZ9FFw4\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=xB7MZZ9FFw4<\/a><\/p>\n<p>&nbsp;<\/p>\n<h6><strong><em>XMega ADC Unsigned Single-Ended<\/em><\/strong><\/h6>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><i>#include &lt;io.h&gt;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include &lt;adc.h&gt;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include &lt;clock.h&gt;\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_RS at PORTC_OUT.B2;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_EN at PORTC_OUT.B3;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D4 at PORTC_OUT.B4;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D5 at PORTC_OUT.B5;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D6 at PORTC_OUT.B6;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D7 at PORTC_OUT.B7;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_RS_Direction at PORTC_DIR.B2;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_EN_Direction at PORTC_DIR.B3;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D4_Direction at PORTC_DIR.B4;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D5_Direction at PORTC_DIR.B5;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D6_Direction at PORTC_DIR.B6;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D7_Direction at PORTC_DIR.B7;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int offset = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void clock_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void io_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void adc_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int ADCA_read_ext_pins(unsigned char pin);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int adc_avg(unsigned char no_of_samples, unsigned char pin);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void lcd_print(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char cnt_volt);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>float map(float v, float x_min, float x_max, float y_min, float y_max);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void main()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 signed int adc = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 float v = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_out(1, 1, \"V:\");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_out(2, 1, \"ADC:\");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(1)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 adc = adc_avg(25, ADC_CH_MUXPOS_PIN1_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 v = map(adc, offset, 4095.0, 0, 1959.375);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_print(4, 1, v, 0);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_print(5, 2, adc, 1);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 delay_ms(600);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 };<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void clock_setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32KEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0 \u00a0\u00a0while(!(OSC_STATUS &amp; OSC_RC32KRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32MEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CLK_PSCTRL = ((CLK_PSCTRL &amp; (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm | CLK_PSBCDIV0_bm)))<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | CLK_PSADIV_1_gc | CLK_PSBCDIV_2_2_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_DFLLCTRL = ((OSC_DFLLCTRL &amp; (~(OSC_RC32MCREF_gm | OSC_RC2MCREF_bm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 OSC_RC32MCREF_RC32K_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 DFLLRC32M_CTRL |= DFLL_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32MRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CLK_CTRL = ((CLK_CTRL &amp; (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_RC32M_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL &amp;= (~(OSC_RC2MEN_bm | OSC_XOSCEN_bm | OSC_PLLEN_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTCFG_CLKEVOUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void io_setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_OUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_DIR = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTCFG_MPCMASK = 0xFF;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_PIN0CTRL = (PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_INTCTRL = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_INT0MASK = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_INT1MASK = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void adc_setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 unsigned char samples = 16;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CAL = (0x0FFF &amp; ((PROD_SIGNATURES_ADCACAL1 &lt;&lt; 8) | PROD_SIGNATURES_ADCACAL0));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLB = ((1 &lt;&lt; ADC_IMPMODE_bp) | ADC_CURRLIMIT_NO_gc | (0 &lt;&lt; ADC_CONMODE_bp) | ADC_RESOLUTION_12BIT_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_PRESCALER = ADC_PRESCALER_DIV64_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_REFCTRL = (ADC_REFSEL_VCC_gc | (0 &lt;&lt; ADC_TEMPREF_bp) | (0 &lt;&lt; ADC_BANDGAP_bp));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_CTRL = ((0 &lt;&lt; ADC_CH_START_bp) | ADC_CH_INPUTMODE_SINGLEENDED_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_MUXCTRL = ADC_CH_MUXPOS_PIN0_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLA |= ADC_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 delay_ms(4);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(samples &gt; 0)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_CTRL |= (1 &lt;&lt; ADC_CH_START_bp);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 while(!(ADCA_CH0_INTFLAGS &amp; ADC_CH_CHIF_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_INTFLAGS = ADC_CH_CHIF_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 offset += ADCA_CH0RES;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 samples--;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 }\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLA &amp;= ~ADC_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 offset &gt;&gt;= 4;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CMP = 0x0000;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_CTRL = ((0 &lt;&lt; ADC_CH_START_bp) | ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_MUXCTRL = ADC_CH_MUXPOS_PIN1_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_EVCTRL = (ADC_SWEEP_0_gc | ADC_EVACT_NONE_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_INTCTRL = (ADC_CH_INTMODE_COMPLETE_gc | ADC_CH_INTLVL_OFF_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH1_INTCTRL = ADC_CH_INTLVL_OFF_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH2_INTCTRL = ADC_CH_INTLVL_OFF_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH3_INTCTRL = ADC_CH_INTLVL_OFF_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLB |= ADC_FREERUN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLA |= ADC_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 delay_ms(9);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 clock_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 io_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 adc_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 Lcd_Init();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 Lcd_Cmd(_LCD_CLEAR);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 Lcd_Cmd(_LCD_CURSOR_OFF);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int ADCA_read_ext_pins(unsigned char pin)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 signed int val = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ADCA_CH0_MUXCTRL = pin;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 while(!(ADCA_CH0_INTFLAGS &amp; ADC_CH_CHIF_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ADCA_CH0_INTFLAGS = ADC_CH_CHIF_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 val = ADCA_CH0_RES;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 return val;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int adc_avg(unsigned char no_of_samples, unsigned char pin)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 signed long avg = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 unsigned char samples = no_of_samples;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 while(samples &gt; 0)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 avg += ADCA_read_ext_pins(pin);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 samples--;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 avg \/= no_of_samples;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 return avg;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void lcd_print(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char cnt_volt)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 unsigned char tmp = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 if(value &gt; 0)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_out(y_pos, x_pos, \" \");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 else<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_out(y_pos, x_pos, \"-\");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 value *= -1;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 tmp = (value \/ 1000);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp(tmp + 48);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 switch(cnt_volt)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 1:<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 default:<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp(46);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 tmp = ((value \/ 100) % 10);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 tmp = ((value \/ 10) % 10);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 tmp = (value % 10);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>float map(float v, float x_min, float x_max, float y_min, float y_max)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 float m = 0.0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 m = ((y_max - y_min)\/(x_max - x_min));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 return (y_min + (m * (v - x_min)));<\/i><\/span><\/pre>\n<p><span style=\"color: #0000ff\"><i>}<\/i><\/span><br \/>\n<a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/unsigned-single-ended-2\/\" rel=\"attachment wp-att-9526\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9526\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Unsigned-single-ended-2.jpg\" alt=\"Unsigned single -ended (2)\" width=\"590\" height=\"596\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Unsigned-single-ended-2.jpg 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Unsigned-single-ended-2-550x555.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Unsigned-single-ended-2-90x90.jpg 90w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/unsigned-single-ended-1\/\" rel=\"attachment wp-att-9525\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9525\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Unsigned-single-ended-1.jpg\" alt=\"Unsigned single -ended (1)\" width=\"590\" height=\"664\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Unsigned-single-ended-1.jpg 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Unsigned-single-ended-1-550x618.jpg 550w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/p>\n<p><span style=\"color: #0000ff\"><span style=\"color: #000000\">Video Link:\u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=4v042tw5IQY\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=4v042tw5IQY<\/a><br \/>\n<\/span><br \/>\n<\/span><\/p>\n<h6><em><strong>XMega ADC Signed Single-Ended<\/strong><\/em><\/h6>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><em>#include &lt;io.h&gt;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>#include &lt;adc.h&gt;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>#include &lt;clock.h&gt;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_RS at PORTC_OUT.B2;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_EN at PORTC_OUT.B3;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_D4 at PORTC_OUT.B4;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_D5 at PORTC_OUT.B5;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_D6 at PORTC_OUT.B6;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_D7 at PORTC_OUT.B7;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_RS_Direction at PORTC_DIR.B2;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_EN_Direction at PORTC_DIR.B3;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_D4_Direction at PORTC_DIR.B4;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_D5_Direction at PORTC_DIR.B5;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_D6_Direction at PORTC_DIR.B6;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>sbit LCD_D7_Direction at PORTC_DIR.B7; <\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>signed int offset = 0;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>void clock_setup();<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>void io_setup();<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>void adc_setup();<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>void setup();<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>signed int ADCA_read_ext_pins(unsigned char pin);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>signed int adc_avg(unsigned char no_of_samples, unsigned char pin);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>void lcd_print(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char cnt_volt);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>void main()<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>{<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 signed int adc = 0;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 float v = 0;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 setup();<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 <\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 lcd_out(1, 1, \"V:\");<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 lcd_out(2, 1, \"ADC:\");<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 while(1)<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 {<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 adc = adc_avg(25, ADC_CH_MUXPOS_PIN7_gc);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 v = ((adc * 2062.5) \/ 2047);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_print(6, 1, v, 0);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_print(6, 2, adc, 1);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0delay_ms(600);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 };<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>}<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>void clock_setup()<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>{<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32KEN_bm;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32KRDY_bm));<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32MEN_bm;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 CLK_PSCTRL = ((CLK_PSCTRL &amp; (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm | CLK_PSBCDIV0_bm)))<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | CLK_PSADIV_1_gc | CLK_PSBCDIV_2_2_gc);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 OSC_DFLLCTRL = ((OSC_DFLLCTRL &amp; (~(OSC_RC32MCREF_gm | OSC_RC2MCREF_bm))) |<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 OSC_RC32MCREF_RC32K_gc);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 DFLLRC32M_CTRL |= DFLL_ENABLE_bm;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32MRDY_bm));<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 CLK_CTRL = ((CLK_CTRL &amp; (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_RC32M_gc);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL &amp;= (~(OSC_RC2MEN_bm | OSC_XOSCEN_bm | OSC_PLLEN_bm));<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 PORTCFG_CLKEVOUT = 0x00;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>}<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>void io_setup()<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>{<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 PORTA_OUT = 0x00;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 PORTA_DIR = 0x00;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 PORTCFG_MPCMASK = 0xFF;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 PORTA_PIN0CTRL = (PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 PORTA_INTCTRL = 0x00;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 PORTA_INT0MASK = 0x00;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 PORTA_INT1MASK = 0x00;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>}<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>void adc_setup()<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>{<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 unsigned char samples = 16;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CAL = (0x0FFF &amp; ((PROD_SIGNATURES_ADCACAL1 &lt;&lt; 8) | PROD_SIGNATURES_ADCACAL0));<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 <\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLB = ((0 &lt;&lt; ADC_IMPMODE_bp) | ADC_CURRLIMIT_NO_gc | (1 &lt;&lt; ADC_CONMODE_bp) | ADC_RESOLUTION_12BIT_gc);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_PRESCALER = ADC_PRESCALER_DIV64_gc;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_REFCTRL = (ADC_REFSEL_VCC_gc | (0 &lt;&lt; ADC_TEMPREF_bp) | (0 &lt;&lt; ADC_BANDGAP_bp));<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_CTRL = ((0 &lt;&lt; ADC_CH_START_bp) | ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_DIFF_gc);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_MUXCTRL = (ADC_CH_MUXPOS_PIN0_gc | ADC_CH_MUXNEG_PIN0_gc);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLA |= ADC_ENABLE_bm;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 delay_ms(4);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 while(samples &gt; 0)<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 {<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_CTRL |= (1 &lt;&lt; ADC_CH_START_bp);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 while(!(ADCA_CH0_INTFLAGS &amp; ADC_CH_CHIF_bm));<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_INTFLAGS = ADC_CH_CHIF_bm;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 offset += ADCA_CH0RES;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 samples--;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 }<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLA &amp;= ~ADC_ENABLE_bm;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 offset &gt;&gt;= 4;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CMP = 0x0000;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_CTRL = ((0 &lt;&lt; ADC_CH_START_bp) | ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_MUXCTRL = ADC_CH_MUXPOS_PIN7_gc;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_EVCTRL = (ADC_SWEEP_0_gc | ADC_EVACT_NONE_gc);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_INTCTRL = (ADC_CH_INTMODE_COMPLETE_gc | ADC_CH_INTLVL_OFF_gc);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CH1_INTCTRL = ADC_CH_INTLVL_OFF_gc;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CH2_INTCTRL = ADC_CH_INTLVL_OFF_gc;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CH3_INTCTRL = ADC_CH_INTLVL_OFF_gc;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLB |= ADC_FREERUN_bm;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLA |= ADC_ENABLE_bm;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 delay_ms(9);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>}<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>void setup()<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>{<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 clock_setup();<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 io_setup();<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 adc_setup();<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 Lcd_Init();<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 Lcd_Cmd(_LCD_CLEAR);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 Lcd_Cmd(_LCD_CURSOR_OFF);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>}<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>signed int ADCA_read_ext_pins(unsigned char pin)<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>{<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 signed int val = 0;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 ADCA_CH0_MUXCTRL = pin;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 while (!(ADCA_CH0_INTFLAGS &amp; ADC_CH_CHIF_bm));<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 ADCA_CH0_INTFLAGS = ADC_CH_CHIF_bm;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 val = ADCA_CH0_RES;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 <\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 if (val &gt; offset)<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 {<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 val -= offset;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 }<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 else<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 {<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 val = 0;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 }<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 <\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0 return val;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>}<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>signed int adc_avg(unsigned char no_of_samples, unsigned char pin)<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>{<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0 signed long avg = 0;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0 unsigned char samples = no_of_samples;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0 while(samples &gt; 0)<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 avg += ADCA_read_ext_pins(pin);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 samples--;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0 avg \/= no_of_samples;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0 <\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0 return avg;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>}<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>void lcd_print(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char cnt_volt)<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>{<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 unsigned char tmp = 0;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 if(value &gt; 0)<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 {<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_out(y_pos, x_pos, \" \");<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 }<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 else<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 {<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_out(y_pos, x_pos, \"-\");<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 value *= -1;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 }<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 tmp = (value \/ 1000);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp(tmp + 48);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 <\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 switch(cnt_volt)<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 {<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 1:<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 default:<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp(46);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 }<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 <\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 tmp = ((value \/ 100) % 10);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 tmp = ((value \/ 10) % 10);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 tmp = (value % 10);<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));<\/em><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><em>}\r\n\r\n<span style=\"color: #000000\"><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/signed-single-ended-1\/\" rel=\"attachment wp-att-9523\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9523\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Signed-single-ended-1.jpg\" alt=\"Signed single ended (1)\" width=\"590\" height=\"736\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Signed-single-ended-1.jpg 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Signed-single-ended-1-521x650.jpg 521w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/signed-single-ended-2\/\" rel=\"attachment wp-att-9524\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9524\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Signed-single-ended-2.jpg\" alt=\"Signed single ended (2)\" width=\"590\" height=\"755\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Signed-single-ended-2.jpg 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Signed-single-ended-2-507x650.jpg 507w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/span> <\/em><\/span><\/pre>\n<p>Video Link:\u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=xC43v_35zRA\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=xC43v_35zRA<\/a><\/p>\n<p>&nbsp;<\/p>\n<h6><em><strong>XMega ADC Differential Conversion Mode<\/strong><\/em><\/h6>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><i>#include &lt;io.h&gt;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include &lt;adc.h&gt;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include &lt;clock.h&gt;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_RS at PORTC_OUT.B2;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_EN at PORTC_OUT.B3;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D4 at PORTC_OUT.B4;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D5 at PORTC_OUT.B5;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D6 at PORTC_OUT.B6;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D7 at PORTC_OUT.B7;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_RS_Direction at PORTC_DIR.B2;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_EN_Direction at PORTC_DIR.B3;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D4_Direction at PORTC_DIR.B4;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D5_Direction at PORTC_DIR.B5;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D6_Direction at PORTC_DIR.B6;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>sbit LCD_D7_Direction at PORTC_DIR.B7;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int offset = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void clock_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void io_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void adc_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int ADCA_read_ext_pins(unsigned char pos_pin, unsigned char neg_pin);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int adc_avg(unsigned char no_of_samples, unsigned char pos_pin, unsigned char neg_pin);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void lcd_print(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char cnt_volt);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void main()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 signed int adc = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 float v = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_out(1, 1, \"V:\");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 lcd_out(2, 1, \"ADC:\");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(1)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 adc = adc_avg(20, ADC_CH_MUXPOS_PIN1_gc, ADC_CH_MUXNEG_PIN2_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 v = ((adc * 2062.5) \/ 2048);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_print(6, 1, v, 0);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_print(6, 2, adc, 1);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 delay_ms(600);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 };<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void clock_setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32KEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32KRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32MEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CLK_PSCTRL = ((CLK_PSCTRL &amp; (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm | CLK_PSBCDIV0_bm)))<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | CLK_PSADIV_1_gc | CLK_PSBCDIV_2_2_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_DFLLCTRL = ((OSC_DFLLCTRL &amp; (~(OSC_RC32MCREF_gm | OSC_RC2MCREF_bm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 OSC_RC32MCREF_RC32K_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 DFLLRC32M_CTRL |= DFLL_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32MRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 CLK_CTRL = ((CLK_CTRL &amp; (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_RC32M_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 OSC_CTRL &amp;= (~(OSC_RC2MEN_bm | OSC_XOSCEN_bm | OSC_PLLEN_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTCFG_CLKEVOUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void io_setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_OUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_DIR = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTCFG_MPCMASK = 0xFF;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_PIN0CTRL = (PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_INTCTRL = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_INT0MASK = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 PORTA_INT1MASK = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void adc_setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 unsigned char samples = 16;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CAL = (0x0FFF &amp; ((PROD_SIGNATURES_ADCACAL1\u00a0 &lt;&lt; 8) | PROD_SIGNATURES_ADCACAL0));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLB = ((1 &lt;&lt; ADC_IMPMODE_bp) | ADC_CURRLIMIT_NO_gc | (1 &lt;&lt; ADC_CONMODE_bp) | ADC_RESOLUTION_12BIT_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_PRESCALER = ADC_PRESCALER_DIV64_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_REFCTRL = (ADC_REFSEL_VCC_gc | (0 &lt;&lt; ADC_TEMPREF_bp) | (0 &lt;&lt; ADC_BANDGAP_bp));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_CTRL = ((0 &lt;&lt; ADC_CH_START_bp) | ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_DIFF_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_MUXCTRL = (ADC_CH_MUXPOS_PIN0_gc | ADC_CH_MUXNEG_PIN0_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLA |= ADC_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 delay_ms(4);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(samples &gt; 0)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_CTRL |= (1 &lt;&lt; ADC_CH_START_bp);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 while(!(ADCA_CH0_INTFLAGS &amp; ADC_CH_CHIF_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_INTFLAGS = ADC_CH_CHIF_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 offset += ADCA_CH0RES;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 samples--;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLA &amp;= ~ADC_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 offset &gt;&gt;= 4;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_CTRL = ((0 &lt;&lt; ADC_CH_START_bp) | ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_DIFF_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_MUXCTRL = (ADC_CH_MUXPOS_PIN1_gc | ADC_CH_MUXNEG_PIN2_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_EVCTRL = (ADC_SWEEP_0_gc | ADC_EVACT_NONE_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH0_INTCTRL = (ADC_CH_INTMODE_COMPLETE_gc | ADC_CH_INTLVL_OFF_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH1_INTCTRL = ADC_CH_INTLVL_OFF_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH2_INTCTRL = ADC_CH_INTLVL_OFF_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CH3_INTCTRL = ADC_CH_INTLVL_OFF_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 ADCA_CTRLB |= ADC_FREERUN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 \u00a0ADCA_CTRLA |= ADC_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 delay_ms(9);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 clock_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 io_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 adc_setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 Lcd_Init();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 Lcd_Cmd(_LCD_CLEAR);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 Lcd_Cmd(_LCD_CURSOR_OFF);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int ADCA_read_ext_pins(unsigned char pos_pin, unsigned char neg_pin)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 signed int val = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ADCA_CH0_MUXCTRL = (pos_pin | neg_pin);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 while (!(ADCA_CH0_INTFLAGS &amp; ADC_CH_CHIF_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ADCA_CH0_INTFLAGS = ADC_CH_CHIF_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 val = ADCA_CH0_RES;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 val -= offset;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 return val;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>signed int adc_avg(unsigned char no_of_samples, unsigned char pos_pin, unsigned char neg_pin)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 signed long avg = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 unsigned char samples = no_of_samples;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 while(samples &gt; 0)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 avg += ADCA_read_ext_pins(pos_pin, neg_pin);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 samples--;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 avg \/= no_of_samples;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0 return avg;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void lcd_print(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char cnt_volt)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 unsigned char tmp = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(value &gt; 0)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0lcd_out(y_pos, x_pos, \" \");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_out(y_pos, x_pos, \"-\");<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 value *= -1;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tmp = (value \/ 1000);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp(tmp + 48);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 switch(cnt_volt)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 1:<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 default:<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp(46);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tmp = ((value \/ 100) % 10);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tmp = ((value \/ 10) % 10);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tmp = (value % 10);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lcd_chr_cp((tmp + 48));\r\n}<\/i><\/span><\/pre>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/differental-2\/\" rel=\"attachment wp-att-9519\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9519\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Differental-2.jpg\" alt=\"Differental (2)\" width=\"590\" height=\"527\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Differental-2.jpg 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Differental-2-550x491.jpg 550w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/differental-1\/\" rel=\"attachment wp-att-9518\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9518\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Differental-1.jpg\" alt=\"Differental (1)\" width=\"590\" height=\"514\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Differental-1.jpg 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/10\/Differental-1-550x479.jpg 550w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/p>\n<p>Video Link:\u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=eiordbwrI0s\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=eiordbwrI0s<\/a><\/p>\n<p>As always it is not always possible to show everything of a sophisticated MCU like the XMega in such a short discussion and so I leave some things for the reader. I didn\u2019t show the following things in this post:<\/p>\n<ul>\n<li>ADC + DMA use<\/li>\n<li>ADC + event system<\/li>\n<li>ADC\u2019s temperature compensation functionality<\/li>\n<li>ADC sweeping<\/li>\n<li>How virtual channels work<\/li>\n<li>ADC\u2019s compare functionality<\/li>\n<li>ADC interrupt<\/li>\n<li>Differential ADC with gain as it is similar to differential ADC mode.<\/li>\n<\/ul>\n<p>I believe if you understood the basic of XMega ADC then these are not die-hard issues. ADC of such a MCU is really both robust and sophisticated. Manufacturers try to give literally everything that users imagine and there sophistication emerges. I recommend going through Atmel\u2019s official docs, especially the reference manual as they will help a lot. To be honest, I read several blogs and even visited sites like AVR Freaks beside Atmel\u2019s docs only to see that people are way too lazy to think beyond the boundaries of Atmel\u2019s ASF. Though the ASF has made life easier, it is doesn\u2019t help raw level coders like me in many ways.<\/p>\n<p>&nbsp;<\/p>\n<p>Files:\u00a0<a href=\"http:\/\/embedded-lab.com\/blog\/xmega-adc\/xmega-adc\/\" rel=\"attachment wp-att-9489\">XMega ADC<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>Happy coding.<\/p>\n<p><i>Author: Shawon M. Shahryiar <\/i><\/p>\n<p><a href=\"https:\/\/www.facebook.com\/groups\/microarena\/\"><i>https:\/\/www.facebook.com\/groups\/microarena\/<\/i><\/a><i>\u00a0\u00a0 <\/i><\/p>\n<p><a href=\"https:\/\/www.facebook.com\/MicroArena\"><i>https:\/\/www.facebook.com\/MicroArena<\/i><\/a><i>\u00a0<\/i><\/p>\n<p><i>+8801970046495 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a030.10.2014<\/i><\/p>\n<p><i>\u00a0<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For users of advance MCUs like the XMega it is not necessary to tell what an analog-to-digital converter (ADC) is or what it does. I assume this is not the first family of microcontroller they are dealing with. Unlike the ADCs of other microcontroller the ADC of XMega devices is a highly complex tool. The level of complexity is so much that without understanding every bits-and-pieces of this piece of hardware a user won\u2019t enjoy its absolute power. XMega ADC is also the most confusing hardware as it is not like other MCU ADCs. We will be dealing with ATXMega32A4U<\/p>\n","protected":false},"author":5,"featured_media":9490,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1466,792],"tags":[44,644,807,800,11,806],"class_list":["post-9488","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","category-xmega","tag-adc","tag-atmel","tag-atxmega","tag-atxmega32a4u","tag-avr","tag-mikroc-pro-for-avr"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/9488","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=9488"}],"version-history":[{"count":14,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/9488\/revisions"}],"predecessor-version":[{"id":12855,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/9488\/revisions\/12855"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/9490"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=9488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=9488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=9488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}