<?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: HCS08 TPM &amp;quot;input capture&amp;quot;  both edges in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136741#M4466</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Hi,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Let's assume that the PWM is high = on (high always is 100%)&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;So the rising edges will always be the same distance apart.&lt;/DIV&gt;&lt;DIV&gt;So at each rising edge you load up a compare value with this time+current.&lt;/DIV&gt;&lt;DIV&gt;If the compare occurs you are at 0% or 100%, you know the last level so you know which.&lt;/DIV&gt;&lt;DIV&gt;You could also set the modulo of the counter to be a bit bigger than the period and when overflow occurs check that you had both transitions since the last overflow.&lt;/DIV&gt;&lt;DIV&gt;Running the modulo at other than $FFFF creates a little more work in handling the overflow in the width measuring part so as usual its a trade of between speed, available resources.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;/DIV&gt;&lt;DIV&gt;David&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;Message Edited by peg on &lt;SPAN class="date_text"&gt;2006-11-15&lt;/SPAN&gt;&lt;SPAN class="time_text"&gt;01:42 PM&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 15 Nov 2006 11:33:44 GMT</pubDate>
    <dc:creator>peg</dc:creator>
    <dc:date>2006-11-15T11:33:44Z</dc:date>
    <item>
      <title>HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136735#M4460</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;I have an application where I need to read an input PWM signal that can vary from 0% to 100% duty cycle.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I assume that TPM "input capture" was designed for this purpose and is the cleanest way to do it?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If I set up the TMP channel to trigger on both edges (rising and falling),&amp;nbsp; what is the best way for my interrupt routine to determine whether it was a rising or falling edge which triggered the interrupt?&amp;nbsp;&amp;nbsp; My ISR needs to be able to determine this, so it knows whether to just store the counter value (for the beginning of the pulse) or to compute the pulse width (for the end of the pulse).&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Also, what is the best way to handle 0% and 100% duty cycles, since&amp;nbsp;there would not be any interrupts generated?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I'm sure there are a gazillion ways to handle these situations, but I am wondering how experienced HCS08 programmers usually do it.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Nov 2006 02:34:03 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136735#M4460</guid>
      <dc:creator>EtherJones</dc:creator>
      <dc:date>2006-11-15T02:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136736#M4461</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Hi,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;This does not come from direct experience, just thinking out loud. (Never actually used either edge trigger)&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;To determine the edge type, just read the pin as GP input in the ISR.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Set a compare value just bigger than the PWM period at each edge and if it triggers, you already know the level and can now call it 0 or 100%.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;/DIV&gt;&lt;DIV&gt;David&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Nov 2006 05:28:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136736#M4461</guid>
      <dc:creator>peg</dc:creator>
      <dc:date>2006-11-15T05:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136737#M4462</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;BR /&gt;here's my pseudo code:&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;initialization:&lt;/DIV&gt;&lt;DIV&gt;- set up TPM2 CH0 for input capture&lt;/DIV&gt;&lt;DIV&gt;- set to interrupt on both edges&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;here's the ISR pseudocode (bare bones):&lt;/DIV&gt;&lt;DIV&gt;- determine whether interrupt was caused by leading edge or trailing edge of pulse&lt;/DIV&gt;&lt;DIV&gt;- if leading edge (beginning of pulse), grab the timer counter and store it&lt;/DIV&gt;&lt;DIV&gt;- if trailing edge, grab the timer counter, and compare it to the previously stored value from the leading edge to compute a pulse width&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The problem is, how do I determine if the interrupt was triggered by the leading edge or the trailing edge?&amp;nbsp; I don't see anywhere in the data sheet where there's a register I can read that will tell me.&amp;nbsp; Am I missing something?&amp;nbsp; Is there such a flag, but I am overlooking it?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Or, do I have to improvise?&amp;nbsp; I could try reading the pin level, but that's not robust because if the duty cycle is close to 0 or 100, the level could have changed again by the time my ISR gets around to reading it.&lt;/DIV&gt;&lt;DIV&gt;Also, even if there were a flag I could read, it still doesn't solve the problem of 0 and 100% duty cycle.&amp;nbsp; For example, if the duty cycle changes from, say, 30% to 0%, then there are no more interrupts, so my ISR stops updating the pulse width calculation, and so the calculated value gets stuck at 30% even though it's really 0%.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I can't be the first person to raise this question.&amp;nbsp; I'm sure there must be a tried-and-true method that the HCS08 gurus use.&amp;nbsp; I'd rather not re-invent the wheel.&amp;nbsp; Could someone could post a method or post a link to web page that addresses this issue?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Many many thanks.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Nov 2006 08:40:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136737#M4462</guid>
      <dc:creator>EtherJones</dc:creator>
      <dc:date>2006-11-15T08:40:51Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136738#M4463</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Hi Ether,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If you could limit the range of the PWM to say 5-95% this would make things easier!!!!&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;However my method will indeed fallover with high resolution PWM.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If you need to handle this I would suggest using two timer channels tied together with one set for falling and the other set for rising edge trigger.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;My method for handling the full 0 and 100% cases still stands. But of course relies on the PWM frequency remaining fixed and only the duty cycle changing.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If anybody has any other ideas please feel free to pipe in!&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;/DIV&gt;&lt;DIV&gt;David&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Nov 2006 09:11:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136738#M4463</guid>
      <dc:creator>peg</dc:creator>
      <dc:date>2006-11-15T09:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136739#M4464</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;EM&gt;"Set a compare value just bigger than the PWM period at each edge and if it triggers, you already know the level and can now call it 0 or 100%."&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;David, could you flesh this out just a bit more.&amp;nbsp; I'm not sure I understand what you mean by a compare value "bigger than the PWM period".&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Nov 2006 11:13:47 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136739#M4464</guid>
      <dc:creator>EtherJones</dc:creator>
      <dc:date>2006-11-15T11:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136740#M4465</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Another way of tackling this issue is to use a single timer channel, but to initially set input capture for positive edge only.&amp;nbsp; Then, within the timer channel ISR, change the status so the next input capture will occur on a negative edge only.&amp;nbsp; Then repeat for a positive edge only, etc.&amp;nbsp; This way the particular edge that caused the interrupt can be readily&amp;nbsp;determined.&amp;nbsp; Again, this method will fail for instances where the next input capture occurs before the previous input capture value can be read, and the flag cleared.&amp;nbsp; If this is a possibility, you would need to use two timer channels, as Peg suggested.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;To detect the no input capture interrupt cases, you&amp;nbsp;could also&amp;nbsp;do this using the timer overflow interrupt.&amp;nbsp; The exact process involved would depend on the PWM period relative to the timer overflow period.&amp;nbsp; Otherwise, it would seem the method described by Peg would&amp;nbsp;require yet another timer channel.&amp;nbsp; It depends on how quickly you need to detect these limit cases.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Nov 2006 11:16:39 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136740#M4465</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2006-11-15T11:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136741#M4466</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Hi,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Let's assume that the PWM is high = on (high always is 100%)&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;So the rising edges will always be the same distance apart.&lt;/DIV&gt;&lt;DIV&gt;So at each rising edge you load up a compare value with this time+current.&lt;/DIV&gt;&lt;DIV&gt;If the compare occurs you are at 0% or 100%, you know the last level so you know which.&lt;/DIV&gt;&lt;DIV&gt;You could also set the modulo of the counter to be a bit bigger than the period and when overflow occurs check that you had both transitions since the last overflow.&lt;/DIV&gt;&lt;DIV&gt;Running the modulo at other than $FFFF creates a little more work in handling the overflow in the width measuring part so as usual its a trade of between speed, available resources.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;/DIV&gt;&lt;DIV&gt;David&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;Message Edited by peg on &lt;SPAN class="date_text"&gt;2006-11-15&lt;/SPAN&gt;&lt;SPAN class="time_text"&gt;01:42 PM&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Nov 2006 11:33:44 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136741#M4466</guid>
      <dc:creator>peg</dc:creator>
      <dc:date>2006-11-15T11:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136742#M4467</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;EM&gt;&lt;FONT size="1"&gt;Let's assume that the PWM is high = on (high always is 100%)&lt;/FONT&gt;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&lt;FONT size="1"&gt;&lt;/FONT&gt;&lt;/EM&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&lt;FONT size="1"&gt;So the rising edges will always be the same distance apart.&lt;/FONT&gt;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&lt;FONT size="1"&gt;So at each rising edge you load up a compare value with this time+current.&lt;/FONT&gt;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&lt;FONT size="1"&gt;If the compare occurs you are at 0% or 100%, you know the last level so you know which.&lt;/FONT&gt;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&lt;FONT size="1"&gt;You could also set the modulo of the counter to be a bit bigger than the period and when overflow occurs check that you had both transitions since the last overflow.&lt;/FONT&gt;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&lt;FONT size="1"&gt;Running the modulo at other than $FFFF creates a little more work in handling the overflow in the width measuring part so as usual its a trade of between speed, available resources.&lt;/FONT&gt;&lt;/EM&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;Just a quick "ACK" to let you know I read your latest post.&amp;nbsp;&amp;nbsp;&amp;nbsp; I think I get the gist of what you're saying but I'll have to ponder it a bit more.&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;Thanks.&lt;/STRONG&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Nov 2006 12:56:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136742#M4467</guid>
      <dc:creator>EtherJones</dc:creator>
      <dc:date>2006-11-15T12:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136743#M4468</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;This is my reading of what I think Peg is alluding to, for detecting the 0 and 100 percent duty cycle cases.&lt;/FONT&gt;&lt;/DIV&gt;&lt;OL&gt;&lt;LI&gt;&lt;FONT size="2"&gt;Use another timer channel and set this up for &lt;U&gt;output compare&lt;/U&gt; interrupt (software interrupt only).&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;&lt;FONT size="2"&gt;Within the &lt;U&gt;input capture&lt;/U&gt; ISR, update the output compare setting so it corresponds&amp;nbsp;to the current input capture value, plus the measured total period for the PWM signal, plus a little bit extra to allow for jitter, etc.&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;&lt;FONT size="2"&gt;With normal input capture interrupt operation, the output compare&amp;nbsp;interrupt should never occur because the value is continuously updated before there is a matching output compare.&lt;/FONT&gt;&lt;/LI&gt;&lt;LI&gt;&lt;FONT size="2"&gt;It the input capture interrupts should cease, the output compare interrupt would occur, and the current state of the input signal would indicate whether 0 percent or 100 percent duty cycle.&lt;/FONT&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Nov 2006 13:21:52 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136743#M4468</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2006-11-15T13:21:52Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136744#M4469</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I do not have a spare TPM channel available.&amp;nbsp; Only one channel was allocated to the input capture option.&lt;/DIV&gt;&lt;DIV&gt;So... here's an alternative approach I've been considering.&amp;nbsp; It does use more processor time than an edge-detection interrupt, but it's simple and straightforward and easily handles the 0% and 100% cases seamlessly.&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Any thoughts?&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;Each TPM module is capable of generating an interrupt every time the module counter overflows.&lt;/DIV&gt;&lt;DIV&gt;Since we are going to use TPM1 to generate 20KHz PWM, it could also generate a 20KHz interrupt.&amp;nbsp; This is fast enough to sample the input line (input period is 2KHz); those samples could be used to calculate the pulse width.&amp;nbsp; This discussion assumes that the input period is known and fixed; the "duty cycle" is calculated by measuring the pulse width and dividing this measured width by the peiod.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;definitions for the following discussion&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;IP - input pin containing input PWM we are trying to capture. The TPM channel for this pin would be disabled and the pin handled as GPIO&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;AL - lo accumulator&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;ALM - max value of AL, a constant initialized to be slightly longer than the period of the input PWM&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;AH - hi accumulator&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;AHM - max value of AH, a constant initialized to be slightly longer than the period of the input PWM&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;DC - duty cycle calculated by this service routine, available for other processes to read&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;A service routine for the 20KHz interrupt could do the following:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;read the state of IP&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;depending on the previous and present state of IP, do the following:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;previous lo, present hi:&amp;nbsp; set AL=0.&amp;nbsp; set AH=1.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;previous hi, present lo:&amp;nbsp; use AH to calculate new value for DC (not to exceed 100%).&amp;nbsp; Then set AL=1 and set AH=0.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;previous lo, present lo:&amp;nbsp; if AL is less than ALM then increment AL; otherwise set DC to zero.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;previous hi, present hi:&amp;nbsp; if AH is less than AHM then increment AH; otherwise set DC to 100.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Nov 2006 04:12:48 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136744#M4469</guid>
      <dc:creator>EtherJones</dc:creator>
      <dc:date>2006-11-16T04:12:48Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136745#M4470</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Hi Ether,&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Before going down this route, I would compare the resolution of your proposed method with the possible errors generated at near 0/100% using the TPM. Also you would need to compare this with the resolution of the device generating the PWM.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If you are restricted to 1 channel and an overflow you can flip the edge to be detected in the ISR to keep track of which edge you are at. If you miss edges but know the period you should be able to "repair" any incorrect values. This would simply mean it might take two periods to update the value. Then use a "double period and a bit" timer modulo to detect when exactly 0/100.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;/DIV&gt;&lt;DIV&gt;David&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Nov 2006 04:46:56 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136745#M4470</guid>
      <dc:creator>peg</dc:creator>
      <dc:date>2006-11-16T04:46:56Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136746#M4471</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Hi all,&lt;/DIV&gt;&lt;DIV&gt;I am going to try to elaborate on my previous post.&lt;/DIV&gt;&lt;DIV&gt;The last method proposed by Ether is to attempt to sample a 2kHz waveform at 20kHz.&lt;/DIV&gt;&lt;DIV&gt;Now while this may keep Mr Nyquist happy it will only give a duty cycle resolution of 10%.&lt;/DIV&gt;&lt;DIV&gt;Not real flash!&lt;/DIV&gt;&lt;DIV&gt;Also to be considered but not yet revealed is the resolution of the device producing the PWM signal.&lt;/DIV&gt;&lt;DIV&gt;No use looking for 0.5% duty cycle if it will never occur.&lt;/DIV&gt;&lt;DIV&gt;Using the TPM will give a resolution of whatever you can clock the TPM module at with probably a little less due to the problems near 0/100%.&lt;/DIV&gt;&lt;DIV&gt;So now I am proposing that the input captures trigger edge is flipped after each capture. Now this should work fine except at the problematic 0/100% region. So what i am saying now is if we take too long to prepare for the next edge - no problem we will just keep on waiting - we will get it next time. All that is needed is to compare the result with the period and if bigger than the period, subtract 1 period.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The downside to this would be that:&lt;/DIV&gt;&lt;DIV&gt;1. we take two periods to update the value&lt;/DIV&gt;&lt;DIV&gt;2. any error in the period becomes an error in the value.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Now in order to allow for this too occur we need to load our compare register (or timer modulo) with a value that is two periods and a bit more to allow deetection of the 0 and 100% cases. Again the only downside is that it will take two periods to update.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;These small delays in update I feel, far outweigh the lost resolution from ignoring the TPM all together.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;/DIV&gt;&lt;DIV&gt;David&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Nov 2006 18:33:29 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136746#M4471</guid>
      <dc:creator>peg</dc:creator>
      <dc:date>2006-11-16T18:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136747#M4472</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Hello Ether,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;I am not sure of the resolution you are trying to achieve for the PWM detection, nor am I aware of the bus&amp;nbsp;frequency you&amp;nbsp;intend to use.&amp;nbsp; For the moment I will assume you need 1 percent resolution and are using a bus rate of&amp;nbsp;8 MHz&amp;nbsp;- and let's see how the figures work out.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;For a 2000 Hz incoming PWM signal (with a period of 500us), 1 percent resolution would correspond&amp;nbsp;with 5 microseconds, or 40 bus cycles.&amp;nbsp; Is it possible to enter the TPM channel ISR, read the IC value, and clear the flag, all within 40 bus cycles?&amp;nbsp; Probably yes!&amp;nbsp; This would indicate&amp;nbsp;the possibility of&amp;nbsp;achieving the assumed resolution for 1% and&amp;nbsp;99% percent duty cycle , using a single TPM channel.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;If the next instruction, after the one that cleared the flag, read the actual status of the input, and this was found to be&amp;nbsp;opposite&amp;nbsp;to that expected, i.e low after a positive edge IC, and vice versa, it can be assumed that a very short pulse had occurred (less than 1% or greater than 99% duty cycle).&amp;nbsp;&amp;nbsp;The IC edge for the next event would not be changed in this case.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;For those cases where the IC interrupt does not occur, you will need to adopt a process using the timer overflow, as previously outlined by Peg.&amp;nbsp; However, there is a caution - the timer overflow ISR processing cycles (and the processing cycles&amp;nbsp;of any other ISR) may degrade from the desired resolution as the duty cycle approaches 0% and 100%.&amp;nbsp; You would need to minimize your use of interrupts for other purposes, and quite likely you will need to re-enable interrupts &lt;U&gt;within&lt;/U&gt; these "other" ISRs, which will need very careful programming to avoid side effects.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;And now for something completely different -&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;If the incoming PWM signal changes duty cycle only&amp;nbsp;&lt;U&gt;very slowly&lt;/U&gt;, you might alternatively consider filtering the signal with a suitable analog low pass filter, and then applying to an ADC channel for the measurement process.&amp;nbsp; Multiple readings could be taken to average out any ripple component.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Regards,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;Mac&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Nov 2006 20:42:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136747#M4472</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2006-11-16T20:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08 TPM "input capture"  both edges</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136748#M4473</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I think I've got an approach that addresses the 0%, 100%, near 0%, and near 100% cases, and uses the TPM input capture (one channel only) to obtain good resolution.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The trick is to let the input capture ISR just capture and store data, and let the task which needs the duty cycle compute it from this data.&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;Set up TPM2C0 for input capture, both edges, interrupt enabled, serviced by ISR_TPM&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Set up an interrupt-driven timed task ISR_RTT executing at 2000Hz.&amp;nbsp; This is the main control task that needs the input PWM value for its computations.&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;ISR_RTT increments a counter.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;ISR_TPM reads the latched TPM counter value and the logic level of the input pin, and stores them in a ring buffer.&amp;nbsp; Call these values C1 and L1, respectively.&amp;nbsp;&amp;nbsp; Similarly, C2 and L2 are the previous values, and C3 and L3 are the values before that.&amp;nbsp;&amp;nbsp; The&amp;nbsp;ISR_RTT counter is read and stored in R1.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;BR /&gt;ISR_RTT contains the following logic to compute the duty cycle (DC):&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Let R be the present value of the ISR_RTT counter.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;if R-R1 is greater than the period of the PWM being measured, then set DC to 0% or 100% depending on the value of L1.&amp;nbsp; Otherwise, do this:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;L1=0 and L2=1?&amp;nbsp; Compute DC using C1-C2 (but not to exceed 100%)&lt;/DIV&gt;&lt;DIV&gt;L1=0 and L2=0?&amp;nbsp; this is the near-zero condition.&amp;nbsp; Set DC=0%.&amp;nbsp; Sanity check by testing C1-C2.&lt;/DIV&gt;&lt;DIV&gt;L1=1 and L2=1?&amp;nbsp; this is the near-100 condition. Set DC=100%.&amp;nbsp; Sanity check by testing C1-C2.&lt;/DIV&gt;&lt;DIV&gt;L1=1 and L2=0?&amp;nbsp; you're in the middle of a pulse. do the following:&lt;/DIV&gt;&lt;DIV&gt;L3=1?&amp;nbsp; Compute DC using C2-C3 (but not to exceed 100%)&lt;/DIV&gt;&lt;DIV&gt;L3=0?&amp;nbsp; this is the near-zero condition.&amp;nbsp; Set DC=0%.&amp;nbsp; Sanity check by testing C2-C3.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 17 Nov 2006 13:12:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-TPM-quot-input-capture-quot-both-edges/m-p/136748#M4473</guid>
      <dc:creator>EtherJones</dc:creator>
      <dc:date>2006-11-17T13:12:16Z</dc:date>
    </item>
  </channel>
</rss>

