<?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: Cannot read an ADC in a timer ISR in Kinetis Microcontrollers</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316173#M13919</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ping,&lt;/P&gt;&lt;P&gt;I have been through the RM many times. I have only got as far as I have by reading it. Maybe I'm being a bit dumb, but to me it is not obvious or clear from the RM on how I setup the next channel for HW triggering. For SW it is clear. You set with the next channel. E.g.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ADC0_SC1A = ADC_SC1_AIEN_MASK | (ADC_SC1_ADCH(channel );&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So I'm stuck on this point. I have everything else working. I just need to know what I should do to setup the next channel for HW triggering.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 15 Jul 2014 07:52:33 GMT</pubDate>
    <dc:creator>clivepalmer</dc:creator>
    <dc:date>2014-07-15T07:52:33Z</dc:date>
    <item>
      <title>Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316151#M13897</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am trying to read a few ADCs channels on a 4KHZ ish update rate from a timed interrupt.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I can read the ADC using my command line parser. I have also checked my TPM timer ISR and it works fine. &lt;/P&gt;&lt;P&gt;and hits the ISR every 244us (4KHZ = 250us)&lt;/P&gt;&lt;P&gt;The ADC has been set for a conversion time of 30uS. I have checked my calculation with the freescale ADC calculator and it should be correct.&lt;/P&gt;&lt;P&gt;I want to read 4 ADC channels (30us * 4 = 120us)&lt;/P&gt;&lt;P&gt;My ADC init routine is:-&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;void adc_init(void)&lt;BR /&gt;{&lt;/P&gt;&lt;P&gt; SIM_SCGC6 |= SIM_SCGC6_ADC0_MASK; //&amp;nbsp; enable clock&lt;/P&gt;&lt;P&gt; ADC0_CFG1 = ADC_CFG1_ADICLK(0)&amp;nbsp;&amp;nbsp; // bus clock 24MHZ&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; | ADC_CFG1_ADIV(1)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //ADCK = input clock/2&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; | ADC_CFG1_MODE(1);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //12-bit mode&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ADC0_CFG2 &amp;amp;= ~(ADC_CFG2_MUXSEL_MASK&amp;nbsp; // channel A&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; | ADC_CFG2_ADACKEN_MASK);&amp;nbsp; //asynchronous clock output disabled&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ADC0_CFG2 |= ADC_CFG2_ADHSC_MASK; //HI speed mode&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ADC0_SC2 &amp;amp;= ~ADC_SC2_ADTRG_MASK; // SW trigger&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ADC0_SC2 |= ADC_SC2_REFSEL(0); //voltage reference source VREFH&amp;amp;L&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ADC0_SC3 = ADC_SC3_ADCO_MASK&amp;nbsp; //continuous conversion&lt;BR /&gt;&amp;nbsp;&amp;nbsp; | ADC_SC3_AVGE_MASK&amp;nbsp; //hardware average is enabled&lt;BR /&gt;&amp;nbsp;&amp;nbsp; | ADC_SC3_AVGS(2);&amp;nbsp;&amp;nbsp; //16 averages&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ADC0_SC1A = ADC_SC1_ADCH(31); // disable module&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;At the moment Im just calling a simple ADC read function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;unsigned int adc_read(uint8_t channel)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //for CHANNEL-A&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp; CRITICAL_SECTION_BEGIN;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; ADC0_SC1A = ADC_SC1_ADCH(31);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //disable adc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; ADC0_SC1A = channel;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; while((ADC0_SC1A &amp;amp; ADC_SC1_COCO_MASK) == 0){}; // wait for conversion complete int to clear&lt;BR /&gt;&amp;nbsp;&amp;nbsp; CRITICAL_SECTION_END;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; return ADC0_RA;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Am I being dumb. Is there some good reason why I cannot read an ADC channel from my TPM ISR?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Jul 2014 14:09:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316151#M13897</guid>
      <dc:creator>clivepalmer</dc:creator>
      <dc:date>2014-07-04T14:09:09Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316152#M13898</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;oh forgot to add my timer init routine:-&lt;/P&gt;&lt;P&gt;This timer seems to work fine. I am using a Kinetis FRDM-KL02 PCBA&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;void timer1Init()&lt;BR /&gt;{&lt;BR /&gt; disable_irq(18); // TPM1 vector is 34&lt;BR /&gt; SIM_SCGC6 |= SIM_SCGC6_TPM1_MASK;&amp;nbsp; // enable clock for timer&lt;/P&gt;&lt;P&gt;SIM_SOPT7 |= SIM_SOPT7_ADC0TRGSEL(0x09); // TPM1 oveflow trigger source&lt;BR /&gt; /* ADC update rate */&lt;BR /&gt; TPM1_SC = 0;&lt;BR /&gt; TPM1_SC = TPM_SC_PS(0);&lt;BR /&gt; TPM1_MOD = 0x0008; // for 244us interrupt&lt;BR /&gt; TPM1_CNT = 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; TPM1_SC |= TPM_SC_TOIE_MASK|TPM_SC_CMOD(1); // NB Default to 'up-counting' mode.&lt;BR /&gt;&amp;nbsp;&amp;nbsp; TPM1_C0SC = TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK; // MSB:MSA 1:0 Edge Aligned mode (A defaults to 0)&lt;BR /&gt;&amp;nbsp;&amp;nbsp; TPM1_C0SC |= TPM_CnSC_CHIE_MASK; //channel interrupt enable&lt;BR /&gt;&amp;nbsp;&amp;nbsp; TPM1_C0V = 0x00;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set_irq_priority (18, 3);&lt;BR /&gt; enable_irq(18); // TPM1 vector is 34&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; color: #000000; font-size: 11pt; mso-hansi-theme-font: minor-latin; mso-ansi-language: EN-GB; font-family: 'Calibri','sans-serif'; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-bidi-theme-font: minor-bidi;"&gt;I have measured my ADC conversion time and now verified that it is 30uS. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; color: #000000; font-size: 11pt; mso-hansi-theme-font: minor-latin; mso-ansi-language: EN-GB; font-family: 'Calibri','sans-serif'; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-bidi-theme-font: minor-bidi;"&gt;So why will my timer ISR not perform an ADC conversion? I am trying to use SW trigger.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; color: #000000; font-size: 11pt; mso-hansi-theme-font: minor-latin; mso-ansi-language: EN-GB; font-family: 'Calibri','sans-serif'; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-bidi-theme-font: minor-bidi;"&gt;Help!!!!!!!!!!!!&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 07 Jul 2014 09:53:28 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316152#M13898</guid>
      <dc:creator>clivepalmer</dc:creator>
      <dc:date>2014-07-07T09:53:28Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316153#M13899</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Clive,&lt;/P&gt;&lt;P&gt;There's a contradication between your purpose and code design. As you mentioned before, you want read 4 ADC channels per 250us, then you configured the TPM overflow period to be 244us and read ead 4 ADC channels in TPM interrupt routine.&lt;/P&gt;&lt;P&gt;Because read one ADC channel only cost 30us, then read 4 ADC channel will cost about 120 us. It means it's available to read 4 ADC channels in in TPM interrupt routine.&lt;/P&gt;&lt;P&gt;However in timer1Init() function, you set TPM1 overflow as ADC hardware trigger and in fact, you also didn't set ADC0ALTTRGEN to 1 to enable the ADC0 trigger selection.&lt;/P&gt;&lt;P&gt;In my opinion, you needn't to set any hardware trigger sources of ADC, you could execute adc_read four times to read 4 ADC channels is fine.&lt;/P&gt;&lt;P&gt;You should give a shot.&lt;BR /&gt;Have a great day,&lt;BR /&gt;Ping&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jul 2014 02:38:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316153#M13899</guid>
      <dc:creator>jeremyzhou</dc:creator>
      <dc:date>2014-07-08T02:38:42Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316154#M13900</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Thanks for the response. Yes I have designed it such that I can read all 4 in 120us. At the moment I'm keeping it simple and trying to use one. I have tried both HW and SW trigger but neither work when I read the ADC in the timer ISR. I was hoping to use just SW trigger and this was what I tried first but this does not seem to work. The setting for SOPT7 (timer overflow) shown in my timer init function was what I had when I was trying to see if I could get the code to work with HW trigger. When I was trying SW trigger this was not set. As you correctly pointed out my code snippet I pasted in is wrong. I have however tried both HW and SW trigger with what I believe was the correct settings, so I don't believe this is the issue. &lt;/P&gt;&lt;P&gt;As soon as I perform an ADC read the code just locks up and crashes. This is my issue.&lt;/P&gt;&lt;P&gt;If I am not mistaken TPM1 triggers selected ADC0 out of reset and should be set to 0 and I should not need to set ADC0ALTTRGEN to 1?&lt;/P&gt;&lt;P&gt;Any more thoughts? Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jul 2014 06:08:04 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316154#M13900</guid>
      <dc:creator>clivepalmer</dc:creator>
      <dc:date>2014-07-08T06:08:04Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316155#M13901</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Clive,&lt;/P&gt;&lt;P&gt;I'd like to suggest that you should achieve the design step by step.&lt;/P&gt;&lt;P&gt;First, you create a demo to read on ADC channel by use software trigger, then add a TPM routine to make interrupe happen per 244us and move the ADC read function in TPM interrupe routine.&lt;/P&gt;&lt;P&gt;There's no ADC demo code in FRDM-KL02 sample code, however the ADC module in Kinetis L series is same, so you can refer to FRDM_KL26ZDemo demo&amp;nbsp; in FRDM-KL26 sample code which include ADC sample operation.&lt;/P&gt;&lt;P&gt;Hope this help.&lt;/P&gt;&lt;P&gt;The link of FRDM-KL26 page as follow, then you can download the FRDM-KL26 sample code through.&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=FRDM-KL26Z&amp;amp;uc=true&amp;amp;lang_cd=en" title="http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=FRDM-KL26Z&amp;amp;uc=true&amp;amp;lang_cd=en"&gt;FRDM-KL26Z: Freescale Freedom Development Platform for Kinetis KL16 and KL26 MCUs (up to 128 KB Flash)&lt;/A&gt;&lt;BR /&gt;Have a great day,&lt;BR /&gt;Ping&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jul 2014 07:56:14 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316155#M13901</guid>
      <dc:creator>jeremyzhou</dc:creator>
      <dc:date>2014-07-08T07:56:14Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316156#M13902</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ping,&lt;/P&gt;&lt;P&gt;I have already done what you suggested. I first got the TPM1 timer working correctly at 250us. Verified on a CRO by togging a GPIO line.&lt;/P&gt;&lt;P&gt;I then performed a successful ADC conversion on a channel. When I knew they both worked I then placed the ADC read into the timer ISR.&lt;/P&gt;&lt;P&gt;The moment I did this the code stopped working. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jul 2014 08:00:39 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316156#M13902</guid>
      <dc:creator>clivepalmer</dc:creator>
      <dc:date>2014-07-08T08:00:39Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316157#M13903</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;To be more specific. Both the ADC and timer works fine independently in the same application. The moment I add just one line of code to the timer ISR to start a conversion the code stops running.&lt;/P&gt;&lt;P&gt;ADC0_SC1A = 0;&lt;/P&gt;&lt;P&gt;I don't see how the looking at the demo code will help me if I already have both the ADC reads and the timer working.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jul 2014 08:26:05 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316157#M13903</guid>
      <dc:creator>clivepalmer</dc:creator>
      <dc:date>2014-07-08T08:26:05Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316158#M13904</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Clive,&lt;/P&gt;&lt;P&gt;It's so weird.&lt;/P&gt;&lt;P&gt;Could you upload the TPM interrupe routine which inclue ADC read function please?&lt;BR /&gt;Have a great day,&lt;BR /&gt;Ping&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jul 2014 08:54:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316158#M13904</guid>
      <dc:creator>jeremyzhou</dc:creator>
      <dc:date>2014-07-08T08:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316159#M13905</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ping,&lt;/P&gt;&lt;P&gt;Sure. Here it is. I can read the ADC using adc_read in the main loop or from a command parser and it works fine. The moment I add the adc_read to the ISR as shown below it breaks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;void FTM1_IRQHandler (void)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM1_SC |= TPM_SC_TOF_MASK;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM1_C0V = 0x000F;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM1_C0SC |= TPM_STATUS_CH0F_MASK;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val = adc_read(0);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // This line breaks the code!&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //printf_always("\n ADC data: %x.\n", val);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; toggle_pps();&lt;BR /&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jul 2014 09:00:15 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316159#M13905</guid>
      <dc:creator>clivepalmer</dc:creator>
      <dc:date>2014-07-08T09:00:15Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316160#M13906</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It is unlikely printf is reentrant, it should never be used inside an interrupt.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It is usually unwise to call functions from inside IRQs as well.&lt;/P&gt;&lt;P&gt;I did not see your adc_read() code, it may have problems being called when inside an IRQ as printf has.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;If your adc_read() code takes longer than to turn that the FTM1 IRQ happens.&amp;nbsp; This code will always crash due to a stack overflow.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jul 2014 18:57:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316160#M13906</guid>
      <dc:creator>bobpaddock</dc:creator>
      <dc:date>2014-07-08T18:57:06Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316161#M13907</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Bob,&lt;/P&gt;&lt;P&gt;Thanks for your response. The printf is commented out and not used. I take on board your comments about functions; although in all my years of coding I have and everyone else I have worked with seems to have broken this rule. :smileyhappy:&lt;/P&gt;&lt;P&gt;The adc_read code is posted at the top of the posting. I have timed this function on a CRO and it takes 30us (which was the design) so it can never come close to exceeding the ISR time period. I had removed the code from the function at one point in time and had it located directly in the ISR. The outcome was the same. &lt;/P&gt;&lt;P&gt;If I remove the adc_read function, It only takes one line of code in the ISR to start the ADC transfer to break the code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; ADC0_SC1A = channel;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jul 2014 06:17:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316161#M13907</guid>
      <dc:creator>clivepalmer</dc:creator>
      <dc:date>2014-07-09T06:17:09Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316162#M13908</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Clive,&lt;/P&gt;&lt;P&gt;In the your last reply, you mentioned just add ADC0_SC1A = channel; in TPM interrupt routine, the code would also break, is itn't really?&lt;/P&gt;&lt;P&gt;I will create a demo to test this issue with &lt;A href="http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=FRDM-KL05Z"&gt;FRDMKL05&lt;/A&gt; and update you after I get result.&lt;BR /&gt;Have a great day,&lt;BR /&gt;Ping&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jul 2014 09:14:08 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316162#M13908</guid>
      <dc:creator>jeremyzhou</dc:creator>
      <dc:date>2014-07-09T09:14:08Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316163#M13909</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ping,&lt;/P&gt;&lt;P&gt;Yes that correct. I look forward to your findings.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Clive&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jul 2014 09:28:01 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316163#M13909</guid>
      <dc:creator>clivepalmer</dc:creator>
      <dc:date>2014-07-09T09:28:01Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316164#M13910</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Clive, Have you looked into the possibility that you are suffering from interrupt starvation or interrupt priority blocking.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The way your code is structured you will be spending 13 percent of the CPUs time in an interrupt ISR waiting for the ADC done bit to toggle. This is with just reading 1 ADC channel in the ISR. When reading 4 channels in the interrupt service routine, You will be spending 50% of the CPUs bandwidth in an ISR, waiting for the ADC done bit to toggle 4 times. I'm sure your program has other things it needs to get done in that time. Since you are in the interrupt, anything non-interrupt or at a lower interrupt priority will be blocked. Spending 50% or your time in 1 interrupt is a usually a problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You current code is&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Every 244 uS read (timer ISR) &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;EM&gt;Read 4 ADC channels. Each read takes 30uS Leading to 120uS spent in ISR&lt;/EM&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A better algorithm would be to also use the ADC done interrupt as well as the timer interrupt&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Every 244 uS&amp;nbsp; (Timer ISR)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;EM&gt;Start Read of ADC channel 1&lt;/EM&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Every time the ADC done interrupt occurs (ADC done ISR)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Read Data from previously started ADC read&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (not last ADC channel)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Start Next ADC channel read&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With This improved algorithm you will not be stuck in an ISR for 50% of your CPUs available time. The interrupt switching time will be lower than the 120uS wasted waiting for the ADC done bit toggles.&amp;nbsp; A further improvement would be to use a hardware trigger to the ADC. The timer trigger would then use no CPU cycles. The ADC reads could also be DMA transfers also using no CPU cycles to take the reading. I use the K60 parts so I don't know what DMA sources are available on the KL02.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;At 48Mhhz, That 30uS ADC wait loop has blocked the program for 1440 CPU clock cycles that&amp;nbsp; could have been used for other processing. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Norm&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jul 2014 13:52:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316164#M13910</guid>
      <dc:creator>ndavies</dc:creator>
      <dc:date>2014-07-09T13:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316165#M13911</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Clive,&lt;/P&gt;&lt;P&gt;I've created a demo to read channel 26、channel 29、channel 30 and the read function is set in TPM interrupt routine.&lt;/P&gt;&lt;P&gt;These ADC sample results are transfered to PC(Fig 1).&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="1.jpg"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/44889i797C43ABD7CADF41/image-size/large?v=v2&amp;amp;px=999" role="button" title="1.jpg" alt="1.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&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;&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;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp; Fig 1&lt;/P&gt;&lt;P&gt;So please check the attachment and if you have any futher questions about it, just feel free to contact with me.&lt;BR /&gt;Have a great day,&lt;BR /&gt;(my name)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Jul 2014 09:40:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316165#M13911</guid>
      <dc:creator>jeremyzhou</dc:creator>
      <dc:date>2014-07-10T09:40:06Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316166#M13912</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Norm,&lt;/P&gt;&lt;P&gt;Thanks for taking time to reply. I have another timer on a 1ms tick running. I was starting to draw the conclusion that it was stack related. I think you are right and appreciate your solution. I don't have any free GPIO resource to use a HW ADC interrupt.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Jul 2014 10:48:58 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316166#M13912</guid>
      <dc:creator>clivepalmer</dc:creator>
      <dc:date>2014-07-10T10:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316167#M13913</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ping,&lt;/P&gt;&lt;P&gt;Thanks for taking the time to verify this for me. I think you have proved to me that there is no issue doing this and that my issue is elsewhere. I think Norm has hit the nail on the head.&lt;/P&gt;&lt;P&gt;I will report back the solution when I have it solved.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Jul 2014 10:50:39 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316167#M13913</guid>
      <dc:creator>clivepalmer</dc:creator>
      <dc:date>2014-07-10T10:50:39Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316168#M13914</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Clive,&lt;/P&gt;&lt;P&gt;I had assumed you were using the internal ADC. You shouldn't need to use any GPIO to access the ADCs interrupt bit, if you are using the internal ADC. The internal ADCs conversion complete interrupt is routed internally directly to the NVIC. All you need to do is write and register the ISR. Turn on the interrupt enable bit in the ADC and enable the correct interrupt bit in the NVIC.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Jul 2014 12:53:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316168#M13914</guid>
      <dc:creator>ndavies</dc:creator>
      <dc:date>2014-07-10T12:53:06Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316169#M13915</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Norm,&lt;/P&gt;&lt;P&gt;Thanks. I now have HW trigger using TPM1.&lt;/P&gt;&lt;P&gt;I would like to use DMA. Unfortunately the Kinetis L series &lt;A href="http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=KL0"&gt;KL02&lt;/A&gt; has no DMA.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Jul 2014 07:36:25 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316169#M13915</guid>
      <dc:creator>clivepalmer</dc:creator>
      <dc:date>2014-07-11T07:36:25Z</dc:date>
    </item>
    <item>
      <title>Re: Re: Cannot read an ADC in a timer ISR</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316170#M13916</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Clive,&lt;/P&gt;&lt;P&gt;It's supported to use HW interrupt to trigger ADC operation.&lt;/P&gt;&lt;P&gt;I've attached a KL25 demo about using TPM as the hardware trigger source of ADC and I think it's worth to refer to even not based on KL02.&lt;/P&gt;&lt;P&gt;Hope this help.&lt;BR /&gt;Have a great day,&lt;BR /&gt;Ping&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Jul 2014 08:37:58 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Cannot-read-an-ADC-in-a-timer-ISR/m-p/316170#M13916</guid>
      <dc:creator>jeremyzhou</dc:creator>
      <dc:date>2014-07-11T08:37:58Z</dc:date>
    </item>
  </channel>
</rss>

