<?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 Calculating PWM duty cycle from input pin in Kinetis Microcontrollers</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Calculating-PWM-duty-cycle-from-input-pin/m-p/772177#M46949</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I thought I will make in a short time a function that calculates the PWM duty cycle of a fixed PWM signal.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following steps I made to implement this:&lt;/P&gt;&lt;P&gt;-Start an interrupt for the right port and pin&lt;/P&gt;&lt;P&gt;-If rising edge occurs start a LPTMR timer and set the interrupt to falling edge&lt;/P&gt;&lt;P&gt;-If falling edge stop the timer and calculate the duty cycle&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My problem is that I use whole PORTA as interrupt. Therefore I tried "&lt;SPAN&gt;PORT_SetPinInterruptConfig&lt;/SPAN&gt;" and "&lt;SPAN&gt;FGPIO_PortGetInterruptFlags&lt;/SPAN&gt;" to read out the right pin. This code beneath doesn't work at all. It will not enter the IRQ. In "fsl_GPIO" you have functions with PORT and functions with PINS that is really confusing. When do you use what?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My setup is:&lt;/P&gt;&lt;P&gt;-NXP MKE14F512&lt;/P&gt;&lt;P&gt;-FreeRTOS operating system&lt;/P&gt;&lt;P&gt;-MCUXpresso 10.1.1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The used code is below and I also added some output data as attached file.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The function "Read_Pump" is called every 5 seconds&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Read in the PWM duty cycle of the pump and give back the duty cycle in %&lt;BR /&gt;/*-Set the interrupt of the pump pin to rising edge&lt;BR /&gt; *-Enable the interrupt of port A&lt;BR /&gt; *-In interrupt handler save timer value&lt;BR /&gt; *-If pulse was not started start timer and set pin interrupt to falling edge, remember timer has started&lt;BR /&gt; *-If pulse was started calculate duty cycle and store value&lt;BR /&gt; */&lt;BR /&gt;void Read_Pump(void)&lt;BR /&gt;{&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;//Set the pump port pin to interrupt rising edge&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;PORT_SetPinInterruptConfig(GPIOA, 37U, kPORT_InterruptRisingEdge);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;//Enable the interrupt&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;EnableIRQ(PORTA_IRQn);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;void IRQ_HANDLER_PORT_A(void)&lt;BR /&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;//uint32_t Interrept_Flags_A = FGPIO_PortGetInterruptFlags(PORTA_BASE_ADDRESS);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;uint32_t Timer_Value = LPTMR_GetCurrentTimerCount(DEMO_LPTMR_BASE);&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;uint32_t flags = GPIO_GetPinsInterruptFlags(GPIOA);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;DisableIRQ(PORTA_IRQn);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Clear external interrupt flag. */&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;GPIO_ClearPinsInterruptFlags(GPIOA, 37U &amp;lt;&amp;lt; 7U);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;if(Pulse_Started)&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Stop the timer&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LPTMR_StopTimer(DEMO_LPTMR_BASE); //Stop the time&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;//Calculate the PWM duty cycle in %&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;Timer_Value = Timer_Value * 78125; //LPO clock is 128Khz 1/128Khz = 7,8125 us&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;Timer_Value = Timer_Value/1333333; //75Hz period time 1/75 = 13333,33 us&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;SUS_Data.Pump_In_Duty = Timer_Value; //Store PWM duty to local variable&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;Pulse_Started = false;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//start the timer and switch the interrupt flag to failing edge and make Pulse_Started True&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LPTMR_StartTimer(DEMO_LPTMR_BASE); //Start the timer&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PORT_SetPinInterruptConfig(GPIOA, 37U, kPORT_InterruptFallingEdge);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Pulse_Started = true;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;EnableIRQ(PORTA_IRQn);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 12 Jun 2018 09:12:50 GMT</pubDate>
    <dc:creator>rudycoppens</dc:creator>
    <dc:date>2018-06-12T09:12:50Z</dc:date>
    <item>
      <title>Calculating PWM duty cycle from input pin</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Calculating-PWM-duty-cycle-from-input-pin/m-p/772177#M46949</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hey,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I thought I will make in a short time a function that calculates the PWM duty cycle of a fixed PWM signal.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following steps I made to implement this:&lt;/P&gt;&lt;P&gt;-Start an interrupt for the right port and pin&lt;/P&gt;&lt;P&gt;-If rising edge occurs start a LPTMR timer and set the interrupt to falling edge&lt;/P&gt;&lt;P&gt;-If falling edge stop the timer and calculate the duty cycle&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My problem is that I use whole PORTA as interrupt. Therefore I tried "&lt;SPAN&gt;PORT_SetPinInterruptConfig&lt;/SPAN&gt;" and "&lt;SPAN&gt;FGPIO_PortGetInterruptFlags&lt;/SPAN&gt;" to read out the right pin. This code beneath doesn't work at all. It will not enter the IRQ. In "fsl_GPIO" you have functions with PORT and functions with PINS that is really confusing. When do you use what?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My setup is:&lt;/P&gt;&lt;P&gt;-NXP MKE14F512&lt;/P&gt;&lt;P&gt;-FreeRTOS operating system&lt;/P&gt;&lt;P&gt;-MCUXpresso 10.1.1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The used code is below and I also added some output data as attached file.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The function "Read_Pump" is called every 5 seconds&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Read in the PWM duty cycle of the pump and give back the duty cycle in %&lt;BR /&gt;/*-Set the interrupt of the pump pin to rising edge&lt;BR /&gt; *-Enable the interrupt of port A&lt;BR /&gt; *-In interrupt handler save timer value&lt;BR /&gt; *-If pulse was not started start timer and set pin interrupt to falling edge, remember timer has started&lt;BR /&gt; *-If pulse was started calculate duty cycle and store value&lt;BR /&gt; */&lt;BR /&gt;void Read_Pump(void)&lt;BR /&gt;{&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;//Set the pump port pin to interrupt rising edge&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;PORT_SetPinInterruptConfig(GPIOA, 37U, kPORT_InterruptRisingEdge);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;//Enable the interrupt&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;EnableIRQ(PORTA_IRQn);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;void IRQ_HANDLER_PORT_A(void)&lt;BR /&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;//uint32_t Interrept_Flags_A = FGPIO_PortGetInterruptFlags(PORTA_BASE_ADDRESS);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;uint32_t Timer_Value = LPTMR_GetCurrentTimerCount(DEMO_LPTMR_BASE);&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;uint32_t flags = GPIO_GetPinsInterruptFlags(GPIOA);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;DisableIRQ(PORTA_IRQn);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* Clear external interrupt flag. */&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;GPIO_ClearPinsInterruptFlags(GPIOA, 37U &amp;lt;&amp;lt; 7U);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;if(Pulse_Started)&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Stop the timer&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LPTMR_StopTimer(DEMO_LPTMR_BASE); //Stop the time&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;//Calculate the PWM duty cycle in %&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;Timer_Value = Timer_Value * 78125; //LPO clock is 128Khz 1/128Khz = 7,8125 us&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;Timer_Value = Timer_Value/1333333; //75Hz period time 1/75 = 13333,33 us&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;SUS_Data.Pump_In_Duty = Timer_Value; //Store PWM duty to local variable&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;Pulse_Started = false;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//start the timer and switch the interrupt flag to failing edge and make Pulse_Started True&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LPTMR_StartTimer(DEMO_LPTMR_BASE); //Start the timer&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PORT_SetPinInterruptConfig(GPIOA, 37U, kPORT_InterruptFallingEdge);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Pulse_Started = true;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;EnableIRQ(PORTA_IRQn);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jun 2018 09:12:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Calculating-PWM-duty-cycle-from-input-pin/m-p/772177#M46949</guid>
      <dc:creator>rudycoppens</dc:creator>
      <dc:date>2018-06-12T09:12:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating PWM duty cycle from input pin</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Calculating-PWM-duty-cycle-from-input-pin/m-p/772178#M46950</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;See chapter 7 of the following for methods of measuring PWM &lt;/SPAN&gt;&lt;A _jive_internal="true" href="https://community.nxp.com/www.utasker.com/docs/uTasker/uTaskerHWTimers.PDF" rel="nofollow" target="_blank"&gt;http://www.utasker.com/docs/uTasker/uTaskerHWTimers.PDF&lt;/A&gt;&lt;BR /&gt;An optional technique using port DMA (possible on the KE14F) is also shown which achieves very high precision measurements of high frequency signals with no CPU overhead.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Kinetis: &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Fwww.utasker.com%2Fkinetis.html" rel="nofollow" target="_blank"&gt;http://www.utasker.com/kinetis.html&lt;/A&gt;&lt;BR /&gt;Kinetis KE1xF:&lt;BR /&gt;&lt;SPAN&gt;- &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Fwww.utasker.com%2Fkinetis%2FFRDM-KE15Z.html" rel="nofollow" target="_blank"&gt;http://www.utasker.com/kinetis/FRDM-KE15Z.html&lt;/A&gt;&lt;BR /&gt;&lt;SPAN&gt;- &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Fwww.utasker.com%2Fkinetis%2FTWR-KE18F.html" rel="nofollow" target="_blank"&gt;http://www.utasker.com/kinetis/TWR-KE18F.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Free Open Source solution: &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.nxp.com/external-link.jspa?url=https%3A%2F%2Fgithub.com%2FuTasker%2FuTasker-Kinetis" rel="nofollow" target="_blank"&gt;https://github.com/uTasker/uTasker-Kinetis&lt;/A&gt;&lt;BR /&gt;&lt;SPAN&gt;Working project in 15 minutes video: &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.nxp.com/external-link.jspa?url=https%3A%2F%2Fyoutu.be%2FK8ScSgpgQ6M" rel="nofollow" target="_blank"&gt;https://youtu.be/K8ScSgpgQ6M&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;For better, faster, cheaper product developments consider the uTasker developer's version, professional Kinetis support, one-on-one training and complete fast-track project solutions to set you apart from the herd (with turn-key MCUXpresso/FreeRTOS solution) : &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Fwww.utasker.com%2Fsupport.html" rel="nofollow" target="_blank"&gt;http://www.utasker.com/support.html&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jun 2018 17:19:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Calculating-PWM-duty-cycle-from-input-pin/m-p/772178#M46950</guid>
      <dc:creator>mjbcswitzerland</dc:creator>
      <dc:date>2018-06-12T17:19:18Z</dc:date>
    </item>
  </channel>
</rss>

