<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>8-bit MicrocontrollersのトピックRe: ADC measures over time</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/ADC-measures-over-time/m-p/226328#M19328</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The approach taken may depend on how quickly you need to respond to an alarm state, and the sensitivity of the project to alarm "chattering".&amp;nbsp; If the alarm state needs to be based on a single ADC reading, and the analog signal is subject to significant variation due to noise, a large hysteresis value is required.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, if your project requirements will allow a longer time frame to sense an alarm state, you can take the average value from multiple readings for each channel.&amp;nbsp; This will reduce the effects of random noise, and allow a smaller hysteresis range to be used to determine when the warning is cancelled.&amp;nbsp; Ideally, you would take the average over a full mains frequency cycle to minimize the effects of mains hum.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To "pace" the multiple ADC readings, I would make use of a periodic tick interrupt associated with a timer.&amp;nbsp; For example, using a tick period of 1 millisecond, you might alternate readings for each of the two channels, giving 8 readings per channel over a period of 16 milliseconds (a total of 16 ADC readings per measurement cycle).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Using simple averaging, you do not need to store individual readings, but simply sum the readings for each channel.&amp;nbsp; In fact, it is unnecessary to divide the sum by 8, for the above example, since you can simply multiply the comparison threshold values by 8.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define MIN_VAL&amp;nbsp;&amp;nbsp;&amp;nbsp; 50*8&amp;nbsp;&amp;nbsp; // Sum of 8 readings&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define MAX_VAL&amp;nbsp;&amp;nbsp; 150*8&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define HYST&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3*8&amp;nbsp;&amp;nbsp; // Hysteresis value&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define MIN_VALH&amp;nbsp; MIN_VAL + HYST&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define MAX_VALH&amp;nbsp; MAX_VAL - HYST&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define CH_OFFSET 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Use ADC channels 0 &amp;amp; 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;// Global variables:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;byte AD_count;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;word result0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;word result1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Within the ADC initialisation, and following the ADC register initialisation, would be needed -&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; AD_count = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; result0 = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; result1 = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; ADCSC1 = CH_OFFSET;&amp;nbsp; // Commence first ADC reading&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;The following code could be placed within the timer ISR code -&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; if (AD_count &amp;amp; 1) &lt;/SPAN&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;result1 += ADCR;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; else&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result0 += ADCR;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; AD_count++;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; if (AD_count &amp;gt;= 16) { // End of measurement cycle&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; AD_count = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Start of new measurement cycle&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((result0 &amp;lt; MIN_VAL) || (result0 &amp;gt; MAX_VAL))&amp;nbsp;&amp;nbsp; show_warning0();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((result0 &amp;gt; MIN_VALH) || (result0 &amp;lt; MAX_VALH)) clear_warning0();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((result1 &amp;lt; MIN_VAL) || (result1 &amp;gt; MAX_VAL))&amp;nbsp;&amp;nbsp; show_warning1();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((result1 &amp;gt; MIN_VALH) || (result1 &amp;lt; MAX_VALH)) clear_warning1();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result0 = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result1 = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; }&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; ADCSC1 = (AD_count &amp;amp; 1) + CH_OFFSET; // Start next ADC reading&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Regards,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Mac&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 13 May 2013 11:44:19 GMT</pubDate>
    <dc:creator>bigmac</dc:creator>
    <dc:date>2013-05-13T11:44:19Z</dc:date>
    <item>
      <title>ADC measures over time</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/ADC-measures-over-time/m-p/226327#M19327</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i need to check the voltage level of 2 lines&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but i need to send a warning if the level drops or goes too high in either of them&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i'm using 8 bit ADC&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;should i make a histeresis window or a buffer to compare, or both?&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and if use a buffer, what would be the best way to compare the values stored?&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 06 May 2013 13:38:01 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/ADC-measures-over-time/m-p/226327#M19327</guid>
      <dc:creator>GMVS</dc:creator>
      <dc:date>2013-05-06T13:38:01Z</dc:date>
    </item>
    <item>
      <title>Re: ADC measures over time</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/ADC-measures-over-time/m-p/226328#M19328</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The approach taken may depend on how quickly you need to respond to an alarm state, and the sensitivity of the project to alarm "chattering".&amp;nbsp; If the alarm state needs to be based on a single ADC reading, and the analog signal is subject to significant variation due to noise, a large hysteresis value is required.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, if your project requirements will allow a longer time frame to sense an alarm state, you can take the average value from multiple readings for each channel.&amp;nbsp; This will reduce the effects of random noise, and allow a smaller hysteresis range to be used to determine when the warning is cancelled.&amp;nbsp; Ideally, you would take the average over a full mains frequency cycle to minimize the effects of mains hum.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To "pace" the multiple ADC readings, I would make use of a periodic tick interrupt associated with a timer.&amp;nbsp; For example, using a tick period of 1 millisecond, you might alternate readings for each of the two channels, giving 8 readings per channel over a period of 16 milliseconds (a total of 16 ADC readings per measurement cycle).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Using simple averaging, you do not need to store individual readings, but simply sum the readings for each channel.&amp;nbsp; In fact, it is unnecessary to divide the sum by 8, for the above example, since you can simply multiply the comparison threshold values by 8.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define MIN_VAL&amp;nbsp;&amp;nbsp;&amp;nbsp; 50*8&amp;nbsp;&amp;nbsp; // Sum of 8 readings&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define MAX_VAL&amp;nbsp;&amp;nbsp; 150*8&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define HYST&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3*8&amp;nbsp;&amp;nbsp; // Hysteresis value&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define MIN_VALH&amp;nbsp; MIN_VAL + HYST&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define MAX_VALH&amp;nbsp; MAX_VAL - HYST&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;#define CH_OFFSET 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Use ADC channels 0 &amp;amp; 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;// Global variables:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;byte AD_count;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;word result0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;word result1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Within the ADC initialisation, and following the ADC register initialisation, would be needed -&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; AD_count = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; result0 = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; result1 = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; ADCSC1 = CH_OFFSET;&amp;nbsp; // Commence first ADC reading&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;The following code could be placed within the timer ISR code -&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; if (AD_count &amp;amp; 1) &lt;/SPAN&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;result1 += ADCR;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; else&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result0 += ADCR;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; AD_count++;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; if (AD_count &amp;gt;= 16) { // End of measurement cycle&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; AD_count = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Start of new measurement cycle&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((result0 &amp;lt; MIN_VAL) || (result0 &amp;gt; MAX_VAL))&amp;nbsp;&amp;nbsp; show_warning0();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((result0 &amp;gt; MIN_VALH) || (result0 &amp;lt; MAX_VALH)) clear_warning0();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((result1 &amp;lt; MIN_VAL) || (result1 &amp;gt; MAX_VAL))&amp;nbsp;&amp;nbsp; show_warning1();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ((result1 &amp;gt; MIN_VALH) || (result1 &amp;lt; MAX_VALH)) clear_warning1();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result0 = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result1 = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; }&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; ADCSC1 = (AD_count &amp;amp; 1) + CH_OFFSET; // Start next ADC reading&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Regards,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Mac&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 May 2013 11:44:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/ADC-measures-over-time/m-p/226328#M19328</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2013-05-13T11:44:19Z</dc:date>
    </item>
  </channel>
</rss>

