<?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>topic Re: ADC-PWM in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/ADC-PWM/m-p/156918#M9094</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Visu,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For compatibility with 12-bit ADC conversion, your TPM modulo setting will need to be 1023.&amp;nbsp; This will give a PWM period of 1024 TPM clock cycles.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code snippet represents the ISR function associated with TPM1 overflow interrupt.&amp;nbsp; The PWM duty is updated with the result of the &lt;U&gt;previous&lt;/U&gt; ADC conversion, and then a new (single) conversion is then started.&amp;nbsp; Since the PWM period will exceed the ADC conversion period, there is no need to test that the conversion is complete.&amp;nbsp; The ADC interrupt is not used.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;// Global variable:volatile byte chan = 0;     // Conversion for ADC ch 0// TPM1 overflow ISR:interrupt void TPM1_ovf( void){   TPM1SC_TOF = 0;          // Clear overflow flag   TPM1C0V = ADCR &amp;amp; 0x0FFF; // Update PWM duty (TPM ch 0)   ADCSC1 = chan &amp;amp; 0x1F;    // Start ADC conversion}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Of course, prior to globally enabling interrupts you will need to initialise both ADC and TPM modules.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that, for this simple example, the PWM duty is the result of a single ADC conversion.&amp;nbsp; With 12-bit conversion, this will likely be subject to fluctuations due to noise.&amp;nbsp; With more complex code, it would be possible to reduce the noise fluctuation by setting&amp;nbsp;the PWM duty value&amp;nbsp;to the average of multiple ADC conversions, either with a simple arithmetic average, or by&amp;nbsp;digital filtering of the ADC results (equivalent to single order low-pass filter).&amp;nbsp; There have been earlier threads on this forum that describe the latter approach.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Oct 2020 09:01:08 GMT</pubDate>
    <dc:creator>bigmac</dc:creator>
    <dc:date>2020-10-29T09:01:08Z</dc:date>
    <item>
      <title>ADC-PWM</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/ADC-PWM/m-p/156917#M9093</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;Anyone who can help me out with an example C source code for 12-bit ADC count to (source from potentiometer) PWM generation for pulses??&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thankyou in advance.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 08 Aug 2011 15:22:25 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/ADC-PWM/m-p/156917#M9093</guid>
      <dc:creator>vishaka_maithil</dc:creator>
      <dc:date>2011-08-08T15:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: ADC-PWM</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/ADC-PWM/m-p/156918#M9094</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Visu,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For compatibility with 12-bit ADC conversion, your TPM modulo setting will need to be 1023.&amp;nbsp; This will give a PWM period of 1024 TPM clock cycles.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code snippet represents the ISR function associated with TPM1 overflow interrupt.&amp;nbsp; The PWM duty is updated with the result of the &lt;U&gt;previous&lt;/U&gt; ADC conversion, and then a new (single) conversion is then started.&amp;nbsp; Since the PWM period will exceed the ADC conversion period, there is no need to test that the conversion is complete.&amp;nbsp; The ADC interrupt is not used.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;// Global variable:volatile byte chan = 0;     // Conversion for ADC ch 0// TPM1 overflow ISR:interrupt void TPM1_ovf( void){   TPM1SC_TOF = 0;          // Clear overflow flag   TPM1C0V = ADCR &amp;amp; 0x0FFF; // Update PWM duty (TPM ch 0)   ADCSC1 = chan &amp;amp; 0x1F;    // Start ADC conversion}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Of course, prior to globally enabling interrupts you will need to initialise both ADC and TPM modules.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that, for this simple example, the PWM duty is the result of a single ADC conversion.&amp;nbsp; With 12-bit conversion, this will likely be subject to fluctuations due to noise.&amp;nbsp; With more complex code, it would be possible to reduce the noise fluctuation by setting&amp;nbsp;the PWM duty value&amp;nbsp;to the average of multiple ADC conversions, either with a simple arithmetic average, or by&amp;nbsp;digital filtering of the ADC results (equivalent to single order low-pass filter).&amp;nbsp; There have been earlier threads on this forum that describe the latter approach.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:01:08 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/ADC-PWM/m-p/156918#M9094</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2020-10-29T09:01:08Z</dc:date>
    </item>
  </channel>
</rss>

