<?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: Delay in ms function</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Delay-in-ms-function/m-p/164486#M10265</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I cannot understand your reasoning for changing the modulo value from free-running mode, or for the use of a TPM prescale setting of 128.&amp;nbsp; Both would seem quite unnecessary.&amp;nbsp; Additionally, you seem to have a channel setting to toggle on output compare.&amp;nbsp; You actually require software compare mode for this application.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically you require that a channel interrupt occurs every millisecond, by using software output compare mode for the TPM channel.&amp;nbsp; When each interrupt occurs, I prefer to decrement the 'dmsec' counter, so that when timeout is reached the counter value will be zero, and further counting is inhibited.&amp;nbsp; No other timeout flags&amp;nbsp;would be&amp;nbsp;necessary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#define INCRVAL  4000           // Value for 1ms increment    // assuming 4MHz bus, prescale 1// Global variable:volatile word dmsec;/*************************************************************************/// Initialisation for TPM2 modulevoid TPM2_init( void){   TPM2MOD  = 0;                // Free-running mode   TPM2SC   = 0x08;             // Bus clock source, prescale 1   TPM2C0SC = 0x00;  // Channel 0 initially disabled}/*************************************************************************/// Set delay timeout period (multiples of 1ms)void setdelay_ms( word time){   __asm sei;                   // Disable interrupts   TPM2C0V = TPM2CNT + INCRVAL; // First compare value   dmsec = time;   TPM2C0SC = 0x50;             // Software compare mode, interrupt enabled   TPM2C0SC_CH0F = 0;           // Ensure flag is clear   __asm cli;                   // Enable interrupts}/*************************************************************************/// Wait for delay timeout periodvoid waitdelay_ms( word time){   setdelay_ms( time);   while (dmsec)           // Wait for timeout      __RESET_WATCHDOG();}/*************************************************************************/// ISR for TPM2 channel 0interrupt void ISR_TPM2C0( void){   TPM2C0SC_CH0F = 0;           // Clear flag      TPM2C0V += INCRVAL;          // Next compare value   if (dmsec) {      dmsec--;      if (dmsec == 0)         TPM2C0SC = 0x00;       // Disable further TPM2C0 interrupts   }}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Oct 2020 09:11:05 GMT</pubDate>
    <dc:creator>bigmac</dc:creator>
    <dc:date>2020-10-29T09:11:05Z</dc:date>
    <item>
      <title>Delay in ms function</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Delay-in-ms-function/m-p/164485#M10264</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I´m developing a delay function that works in milliseconds.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this is my code so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;void delayms(unsigned int time)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; result1 = max_value / 1000;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; final_result = result1 * time;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; d_enabled = FALSE;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; if(!d_enabled)&lt;BR /&gt;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dmsec&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2MOD&amp;nbsp;&amp;nbsp; = final_result;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2SC&amp;nbsp;&amp;nbsp;&amp;nbsp; = 0x0F;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2C0SC&amp;nbsp; = 0x54;&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; TPM2C1SC&amp;nbsp; = 0x10;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; d_enabled = TRUE;&lt;BR /&gt;&amp;nbsp; }&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; while(!delay_done);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here´s the TPM_ISR&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;interrupt VectorNumber_Vtpm2ch0 void timer2_CH0(void)&lt;BR /&gt;{&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; // Clear CH0F flag&lt;BR /&gt;&amp;nbsp; TPM2C0SC &amp;amp;=0x7F;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dmsec++;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(dmsec == final_result)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2MOD&amp;nbsp; = 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2SC&amp;nbsp;&amp;nbsp; = 0x00;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPM2C0SC = 0x00;&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; TPM2C1SC = 0x00;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; delay_done&amp;nbsp; = TRUE;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&amp;nbsp;&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;&lt;BR /&gt;&amp;nbsp; //Clear the 16-bit timer1 counter&lt;BR /&gt;&amp;nbsp; //TPM1CNTH = 0x00;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;}&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My timer counts at 1 second with a MOD equal to 0xFF ot 65535 so basically assigns the MOD value to a fixed one depending on the function call.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My problem is, with that while it seems the timer ISR is not working, so I´m looking for a way to loop inside the function until the timer returns a flag?.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 03 Sep 2011 22:59:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Delay-in-ms-function/m-p/164485#M10264</guid>
      <dc:creator>Skaptor</dc:creator>
      <dc:date>2011-09-03T22:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: Delay in ms function</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Delay-in-ms-function/m-p/164486#M10265</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I cannot understand your reasoning for changing the modulo value from free-running mode, or for the use of a TPM prescale setting of 128.&amp;nbsp; Both would seem quite unnecessary.&amp;nbsp; Additionally, you seem to have a channel setting to toggle on output compare.&amp;nbsp; You actually require software compare mode for this application.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically you require that a channel interrupt occurs every millisecond, by using software output compare mode for the TPM channel.&amp;nbsp; When each interrupt occurs, I prefer to decrement the 'dmsec' counter, so that when timeout is reached the counter value will be zero, and further counting is inhibited.&amp;nbsp; No other timeout flags&amp;nbsp;would be&amp;nbsp;necessary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#define INCRVAL  4000           // Value for 1ms increment    // assuming 4MHz bus, prescale 1// Global variable:volatile word dmsec;/*************************************************************************/// Initialisation for TPM2 modulevoid TPM2_init( void){   TPM2MOD  = 0;                // Free-running mode   TPM2SC   = 0x08;             // Bus clock source, prescale 1   TPM2C0SC = 0x00;  // Channel 0 initially disabled}/*************************************************************************/// Set delay timeout period (multiples of 1ms)void setdelay_ms( word time){   __asm sei;                   // Disable interrupts   TPM2C0V = TPM2CNT + INCRVAL; // First compare value   dmsec = time;   TPM2C0SC = 0x50;             // Software compare mode, interrupt enabled   TPM2C0SC_CH0F = 0;           // Ensure flag is clear   __asm cli;                   // Enable interrupts}/*************************************************************************/// Wait for delay timeout periodvoid waitdelay_ms( word time){   setdelay_ms( time);   while (dmsec)           // Wait for timeout      __RESET_WATCHDOG();}/*************************************************************************/// ISR for TPM2 channel 0interrupt void ISR_TPM2C0( void){   TPM2C0SC_CH0F = 0;           // Clear flag      TPM2C0V += INCRVAL;          // Next compare value   if (dmsec) {      dmsec--;      if (dmsec == 0)         TPM2C0SC = 0x00;       // Disable further TPM2C0 interrupts   }}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:11:05 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Delay-in-ms-function/m-p/164486#M10265</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2020-10-29T09:11:05Z</dc:date>
    </item>
    <item>
      <title>Re: Delay in ms function</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Delay-in-ms-function/m-p/164487#M10266</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you very much, the only thing I had to do was, the incrval was 4000 so it was giving me a 336ms pulse, so I just multiplied, divided and changed the INCRVAL to a fixed value. Now it gives me the correct amount of ms.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 04 Sep 2011 05:34:46 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Delay-in-ms-function/m-p/164487#M10266</guid>
      <dc:creator>Skaptor</dc:creator>
      <dc:date>2011-09-04T05:34:46Z</dc:date>
    </item>
  </channel>
</rss>

