<?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: Timer problem with 9S08AW</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151800#M8317</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;bigmac-&lt;BR /&gt;No, actually I was not incremmenting tpm2_overflows inside the ISR but after a blocking statement in the main loop waiting for the overflow ISR:&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;// Loop continously, do not leave this for loop&lt;BR /&gt;&amp;nbsp; for(;&lt;IMG alt=":smileywink:" class="emoticon emoticon-smileywink" id="smileywink" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif" title="Smiley Wink" /&gt; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Wait for 10ms timer flag from ISR&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(!timer10ms) {}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // reset 10ms timer flag&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; timer10ms= 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; tpm2_overflows++;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .....&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&lt;BR /&gt;The ISR is as follows:&lt;BR /&gt;&lt;BR /&gt;__interrupt void isrVtpm2ovf(void)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; /* Write your interrupt code here ... */&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; TPM2SC_TOF = 0x4E; /* ACK channel interrupt */&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Read flag, then write a 0 to the bit. */&lt;BR /&gt;&amp;nbsp; timer10ms = 1;&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 12 Feb 2009 15:04:51 GMT</pubDate>
    <dc:creator>plaiming</dc:creator>
    <dc:date>2009-02-12T15:04:51Z</dc:date>
    <item>
      <title>Timer problem with 9S08AW</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151796#M8313</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've run into a problem using timer triggers and Im not sure what Im doing wrong.&lt;BR /&gt;&lt;BR /&gt;I am using MC9S08AW16 for a motor control application. MCU is running off an external clock and setup to use TPM1 to PWM a motor control chip. (Prescaler=2, Modulo=1025, 256.25uS period)&amp;nbsp;&lt;BR /&gt;TPM2&amp;nbsp; is setup at 10ms with overflow interupt enabled. I am using the overflow interrupt to&lt;BR /&gt;trigger execution of my main control loop so I can track time-based statistics.&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;// Loop continously, do not leave this for loop&lt;BR /&gt;&amp;nbsp; for(;&lt;A href="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif"&gt;&lt;IMG alt=":smileywink:" class="emoticon emoticon-smileywink" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif" title="Smiley Wink" /&gt;&lt;/A&gt; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Wait for 10ms timer flag from ISR&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(!timer10ms) {}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; main routine....&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&lt;BR /&gt;The issue I am having is that I need to calculate motor speed by&amp;nbsp; measuring time between 2 tachometer pulses. I do not have any more timers so I am trying to use TPM2 channel to capture the timer on a falling edge of the tachometer input signal,&amp;nbsp; save to a variable, capture the time on the next pulse, and calculate the time between the two. I am also trying to count overflows so I can add them back to the total.&lt;BR /&gt;My code is as follows:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;if(calculate_speed) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(hall_switch &amp;lt; 2) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //if we didnt get the halls to switch then the motor isnt spinning&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; motor_period = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // calculate the motor rpm&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;&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;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; motor_period = (hall_tpm2_overflows * tpm2_modulo) + hall_time0 - hall_time1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_switch = 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_time0 = 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_time1 = 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //enable timer&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2C0SC = 0x44;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;__interrupt void isrVtpm2ch0(void)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; /* Write your interrupt code here ... */&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; if(hall_switch == 0) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // store timer value&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_time0 = TPM2C0V;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // reset channel&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2C0SC = TPM2C0SC &amp;amp; 0x7F;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // clear overflow counter&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; tpm2_overflows = 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //increment hall switch counter&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_switch++;&lt;BR /&gt;&amp;nbsp; } else {&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_time1 = TPM2C0V;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_tpm2_overflows = tpm2_overflows;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //disable ISR&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2C0SC = 0x04;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_switch++;&lt;BR /&gt;&amp;nbsp; }&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;What I am seeing is very inconsistent values for motor_period even when the motor is spinning at a constant speed.&amp;nbsp; Can you see anything blaringly wrong with my setup or approach?&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Paul&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Fwww.freescale.com%2Ffiles%2Fcommunity_files%2F8BITCOMM%2Fmsg13316_MCUinit.c" rel="nofollow" target="_blank"&gt;MCUinit.c&lt;/A&gt;&lt;/P&gt;&lt;DIV class="message-edit-history"&gt;&lt;SPAN class="edit-author"&gt;Message Edited by t.dowe on&lt;/SPAN&gt; &lt;SPAN class="local-date"&gt;2009-09-04&lt;/SPAN&gt; &lt;SPAN class="local-time"&gt;02:09 PM&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Feb 2009 14:42:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151796#M8313</guid>
      <dc:creator>plaiming</dc:creator>
      <dc:date>2009-02-11T14:42:20Z</dc:date>
    </item>
    <item>
      <title>Re: Timer problem with 9S08AW</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151797#M8314</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;to capture 2 falling edges you need something like&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; if(hall_switch == 0) {&lt;BR /&gt;&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp; hall_time1 = hall_time0;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // store timer value&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_time0 = TPM2C0V;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // reset channel&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;so you save the previous capture value before reading the current one..&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Make sure you disable interupts when calculating the motor period.&amp;nbsp; so the values don't change in the middle of the calculation.&amp;nbsp; Or at least,&amp;nbsp; disable interrupts, copy the vaules to temp variables, then reenable interrupts, then perform the math.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Hope this helps.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Bob&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Feb 2009 22:20:29 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151797#M8314</guid>
      <dc:creator>TurboBob</dc:creator>
      <dc:date>2009-02-11T22:20:29Z</dc:date>
    </item>
    <item>
      <title>Re: Timer problem with 9S08AW</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151798#M8315</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;Hello Paul,&lt;BR /&gt;&lt;BR /&gt;Can I assume that tpm2_overflows is incremented within the TPM2 overflow ISR?&amp;nbsp; An uncertainty is possible when the Hall timing edge is very close to the TPM overflow point.&amp;nbsp; The TPM overflow interrupt has lower priority than the TPM channel interrupts.&lt;BR /&gt;&lt;BR /&gt;Consider that the input capture should occur slightly later than TPM overflow, but interrupts are currently disabled, perhaps due to servicing of another interrupt source (maybe TPM1).&amp;nbsp; When interrupts are eventually re-enabled, the channel ISR would be serviced first.&amp;nbsp; This would mean that the overflow count would not yet have been updated (here assuming the timing calculation is done within the channel ISR).&lt;BR /&gt;&lt;BR /&gt;Additionally, since the overflow count is cleared within the channel ISR, when the overflow ISR is subsequently serviced, the count will be incremented, which will cause error for the next reading.&lt;BR /&gt;&lt;BR /&gt;If the timing calculation is done within main(), there would be further uncertainty.should the input capture occur just prior to overflow.&amp;nbsp; By the time that the calculation is commenced, both ISRs would have been processed, and the overflow count would be too high in this case.&lt;BR /&gt;&lt;BR /&gt;The solution will possibly be to apply a correction to the overflow count based on the current input capture value relative to the modulo value (whether the input capture value is low or high).&amp;nbsp; Maybe there is some advantage in doing the period calculation within the overflow ISR.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Mac&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;Message Edited by bigmac on &lt;SPAN class="date_text"&gt;2009-02-12&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;05:09 AM&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Feb 2009 02:03:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151798#M8315</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2009-02-12T02:03:18Z</dc:date>
    </item>
    <item>
      <title>Re: Timer problem with 9S08AW</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151799#M8316</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Thanks for the response TurboBob.&lt;BR /&gt;I think both of your concerns are covered. In my code, hall_switch is being used as a counter&lt;BR /&gt;to determine which edge has been triggered. The main loop starts the sequence by resetting the&lt;BR /&gt;hall switch counter and enables the ISR. At the first ISR, the timer value stored in the channel register &lt;BR /&gt;is written to hall_time0. The channel interupt is reset for the next edge, the tpm2_overflow counter is cleared so that I can count how many overflows may occur before the next edge, and the hall_switch counter is incremented so that the ISR routine knows the next edge is edge #2.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; if(hall_switch == 0) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // store timer value&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_time0 = TPM2C0V;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // reset channel&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2C0SC = TPM2C0SC &amp;amp; 0x7F;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // clear overflow counter&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; tpm2_overflows = 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //increment hall switch counter&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_switch++;&lt;BR /&gt;&amp;nbsp; } &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;On the second edge, hall_switch should now be 1, so the ISR will execute the other branch to store the timer channel value into hall_time1. It also stores away the tpm2_overflows into a temporay variable and then disables the channel interupt so that the hall_time variables are not overwritten.&lt;BR /&gt;&lt;BR /&gt;else {&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_time1 = TPM2C0V;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_tpm2_overflows = tpm2_overflows;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //disable ISR&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2C0SC = 0x04;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hall_switch++;&lt;BR /&gt;&amp;nbsp; }&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Back in the main loop, the period is calculated using the hall_time0 and hall_time1 variables before re-enabling the channel interrupt. The timing is such that the main loop should not run until after the &lt;BR /&gt;halls have had 2 edges, but just in case, the code verifies that hall_switch was incremented twice.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;-paul&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Feb 2009 14:57:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151799#M8316</guid>
      <dc:creator>plaiming</dc:creator>
      <dc:date>2009-02-12T14:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: Timer problem with 9S08AW</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151800#M8317</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;bigmac-&lt;BR /&gt;No, actually I was not incremmenting tpm2_overflows inside the ISR but after a blocking statement in the main loop waiting for the overflow ISR:&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;// Loop continously, do not leave this for loop&lt;BR /&gt;&amp;nbsp; for(;&lt;IMG alt=":smileywink:" class="emoticon emoticon-smileywink" id="smileywink" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif" title="Smiley Wink" /&gt; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Wait for 10ms timer flag from ISR&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(!timer10ms) {}&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // reset 10ms timer flag&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; timer10ms= 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; tpm2_overflows++;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .....&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&lt;BR /&gt;The ISR is as follows:&lt;BR /&gt;&lt;BR /&gt;__interrupt void isrVtpm2ovf(void)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; /* Write your interrupt code here ... */&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; TPM2SC_TOF = 0x4E; /* ACK channel interrupt */&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Read flag, then write a 0 to the bit. */&lt;BR /&gt;&amp;nbsp; timer10ms = 1;&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Feb 2009 15:04:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151800#M8317</guid>
      <dc:creator>plaiming</dc:creator>
      <dc:date>2009-02-12T15:04:51Z</dc:date>
    </item>
    <item>
      <title>Re: Timer problem with 9S08AW</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151801#M8318</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;After struggling in vain, I have attempted a completely new solution. In this method, we have a motor hall input also connected to a KBI pin.&amp;nbsp; What I am doing is simply enabling the KBI and counting the number of interupts during a fixed time period. (namely 1s) This allows for an easy RPM motor calculation. So far this method seems to be giving reliably consistent results (although not yet accurate), more testing/debug is needed.&lt;BR /&gt;&lt;BR /&gt;Just another idea for future readers...&lt;BR /&gt;&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Feb 2009 15:07:21 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151801#M8318</guid>
      <dc:creator>plaiming</dc:creator>
      <dc:date>2009-02-12T15:07:21Z</dc:date>
    </item>
    <item>
      <title>Re: Timer problem with 9S08AW</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151802#M8319</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;Firstly, the &lt;FONT face="Courier New"&gt;hall_switch&lt;/FONT&gt; variable might be used as a simple flag to signal to the main loop that an input capture event has occurred.&amp;nbsp; Otherwise I would use the code snippet that Bob presented, where the current input capture value and the previous capture value are updated on each input capture event, so that a period calculation is possible after each capture event.&lt;BR /&gt;&lt;BR /&gt;Your main loop seems to wait until your 10ms overflow flag is set before doing anything, including always incrementing the overflow counter - you do not appear to also test for an input capture event whilst waiting.&amp;nbsp; It is therefore possible that the counter has at least one count too many because the input capture event will have occurred some time prior to overflow.&amp;nbsp; This may account for some of the errors you are observing.&amp;nbsp; &lt;BR /&gt;&lt;BR /&gt;For instance, if the&amp;nbsp; motor speed is sufficiently high that two consecutive capture events occur within the 10ms overflow period, your current coding would suggest that the overflow counter value would be either 1 or 2, depending on whether an overflow condition occurred between the two events, or not.&amp;nbsp; For the purpose of the period calculation, the number of intervening overflow periods should be zero in this case.&lt;BR /&gt;&lt;BR /&gt;Yes, the counting of KBI events could be an alternative method, but keep in mind that speed resolution will be limited at low speeds.&amp;nbsp; Conversely, the period measurement method provides good speed resolution at low speeds, with reduced resolution as the speed increases.&amp;nbsp; It will depend on the specific requirements of your application.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Mac&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Feb 2009 22:07:54 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151802#M8319</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2009-02-12T22:07:54Z</dc:date>
    </item>
    <item>
      <title>Re: Timer problem with 9S08AW</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151803#M8320</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;gt;For instance, if the&amp;nbsp; motor speed is sufficiently high that two consecutive capture events occur &lt;BR /&gt;&amp;gt; within the 10ms overflow period, your current coding would suggest that the overflow counter &lt;BR /&gt;&amp;gt; value would be either 1or 2, depending on whether an overflow condition occurred between &lt;BR /&gt;&amp;gt; the two events, or not.&amp;nbsp; For the&amp;nbsp; purpose of the period calculation, the number of intervening &lt;BR /&gt;&amp;gt; overflow periods should be zero in this case.&lt;BR /&gt;&lt;BR /&gt;The way the current code is structured (and intended to work), if two motor edges come during 10ms, the overflow count would be 0.&amp;nbsp; This is because the ISR will record tmr on first edge, increment hall_switch, and reset tpm_overflow counter. On the next edge, the ISR will record new timer value, store the tpm_overflow counter (which in this case should be 0), and disable the ISR for the channel. &lt;BR /&gt;&lt;BR /&gt;At the next 10ms overflow, the main loop will see that the hall switch had been triggered and calculate the period.&amp;nbsp; I am not understanding what I am missing.&lt;BR /&gt;&lt;BR /&gt;-paul&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 14 Feb 2009 05:35:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Timer-problem-with-9S08AW/m-p/151803#M8320</guid>
      <dc:creator>plaiming</dc:creator>
      <dc:date>2009-02-14T05:35:16Z</dc:date>
    </item>
  </channel>
</rss>

