<?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: S08QE32 Won't Exit Math Function in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/S08QE32-Won-t-Exit-Math-Function/m-p/268262#M20103</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Guys,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It was the COP timeout. I found and fixed the problem. Thanks for your help&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BR&amp;lt;&lt;/P&gt;&lt;P&gt;Vu&lt;/P&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 01 Mar 2013 16:12:13 GMT</pubDate>
    <dc:creator>Designer11</dc:creator>
    <dc:date>2013-03-01T16:12:13Z</dc:date>
    <item>
      <title>S08QE32 Won't Exit Math Function</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/S08QE32-Won-t-Exit-Math-Function/m-p/268258#M20099</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Everyone,&lt;/P&gt;&lt;P&gt;This is a really odd problem and i don't know where the problem comes from. I'm working on a piece of code that is a little math intense and i don't know whether this is an issue with stack size or the uC does not have enough RAM (2048 bytes). The problem is the program won't exit this function, Update Table (see attached file for more details). Initially, i thought it was the stack size so i increased the stack size to 0x350 in the .prm file&amp;nbsp; (If i go any higher i will get memory allocation error) and the problem is still there.&lt;/P&gt;&lt;P style="min- padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm totally lost and don't know where to tackle the problem.&lt;/P&gt;&lt;P style="min- padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P style="min- padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P style="min- padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;V&lt;/P&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;P&gt;&lt;STRONG&gt;Original Attachment has been moved to: &lt;A _jive_internal="true" href="https://community.nxp.com/docs/DOC-337736"&gt;Calculation.c.zip&lt;/A&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 Feb 2013 21:11:02 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/S08QE32-Won-t-Exit-Math-Function/m-p/268258#M20099</guid>
      <dc:creator>Designer11</dc:creator>
      <dc:date>2013-02-21T21:11:02Z</dc:date>
    </item>
    <item>
      <title>Re: S08QE32 Won't Exit Math Function</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/S08QE32-Won-t-Exit-Math-Function/m-p/268259#M20100</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;I do not think your problem is in the code you posted. I created a tiny CodeWarrior project around it...&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_13614984016213527" jivemacro_uid="_13614984016213527" modifiedtitle="true"&gt;
&lt;P&gt;void main(void) &lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;MCU_Init();&lt;/P&gt;
&lt;P&gt;counter=0;&lt;/P&gt;
&lt;P&gt;for (;;) &lt;/P&gt;
&lt;P&gt;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp; UpdateTable();&lt;/P&gt;
&lt;P&gt;&amp;nbsp; counter++;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;SPAN class="mce_paste_marker"&gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;and counter increments correctly. I increased the stack like you did: STACKSIZE 0x350&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Things I would check:&lt;/P&gt;&lt;P&gt;&amp;nbsp; - can the debugger tell you where you get stuck?&lt;/P&gt;&lt;P&gt;&amp;nbsp; - effect of Delay(10)&lt;/P&gt;&lt;P&gt;&amp;nbsp; - not sure why you declared x as static&lt;/P&gt;&lt;P&gt;&amp;nbsp; - are you sure you are not encountering a reset due to memory overflow, COP timeout, division by zero, etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp; - I would make the variables all global, and track them in real time using the debugger&lt;/P&gt;&lt;P&gt;&amp;nbsp; - also maybe put in a few breakpoints, and see if the math is correct, or gets corrupted, along the way&lt;/P&gt;&lt;P&gt;&amp;nbsp; - try the float=64-bit and float=32-bit models&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Rod.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Feb 2013 02:09:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/S08QE32-Won-t-Exit-Math-Function/m-p/268259#M20100</guid>
      <dc:creator>RodBorras</dc:creator>
      <dc:date>2013-02-22T02:09:42Z</dc:date>
    </item>
    <item>
      <title>Re: S08QE32 Won't Exit Math Function</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/S08QE32-Won-t-Exit-Math-Function/m-p/268260#M20101</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Vu,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With a stack size of over 800 bytes, I would suspect this is not the problem.&amp;nbsp; The most likely contender is a problematic Delay() function.&amp;nbsp; It is unusual to have a delay within a function that is purely computational.&amp;nbsp; You might alter the position of the delay to outside the function, and see whether the problem shifts.&amp;nbsp; Single stepping through the function should also identify precisely where the problem occurs within the function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Having said that, I feel that your UpdateTable() function can be significantly simplified as an attempt to reduce code size.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1.&amp;nbsp; Firstly, I would assume that the global static arrays GC[], GL[] and GF[] are actually meant to be constant look-up tables.&amp;nbsp; If so, replace 'static' modifier with 'const', so that this data will be programmed to flash memory, rather than initialised to RAM.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2.&amp;nbsp; Within the UpdateTable() function, the variable x should be a normal local variable, and not static.&amp;nbsp; Since you are cycling x from 0 to 255 within the function, it would appear unnecessary to retain the last value between function calls.&amp;nbsp; I presume that this function would not be called very often - perhaps during initialisation only.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;3.&amp;nbsp; The section of code used to calculate the value of i can be considerably simplified.&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; for (i = 7; i &amp;gt; 1; i--) {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (x &amp;gt; GL[i])&amp;nbsp; break;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; }&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Note that &lt;SPAN style="font-family: courier new,courier;"&gt;GL[i]&lt;/SPAN&gt; may alternatively be calculated using the expression &lt;SPAN style="font-family: courier new,courier;"&gt;(1 &amp;lt;&amp;lt; i)&lt;/SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;4.&amp;nbsp; The following statement would never be executed, and may be omitted from your code -&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; else if ((x == GL[i]) &amp;amp;&amp;amp; (x &amp;gt; 4))&amp;nbsp; tmpVal = GC[i+1];&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;This is because, if x &amp;gt; 4 (and i &amp;gt; 1), x will always exceed the value &lt;SPAN style="font-family: courier new,courier;"&gt;GL[i]&lt;/SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;5.&amp;nbsp; Consider the following section within your code -&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; pdiff = (GC[i+1] - GC[i]);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; ratio = (float)(x - GL[i])/(GL[i+1] - GL[i]);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; tmpVal = (int)(GC[i] + (float)(pdiff * ratio)); &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker" style="font-family: arial,helvetica,sans-serif;"&gt;For the calculation of &lt;EM&gt;ratio,&lt;/EM&gt; in general GL[i+1] is equal to 2*GL[i], which would give the simplified expression -&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker" style="font-family: courier new,courier;"&gt;(x - GL[i])/GL[i]&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;The one exception within your code is when i = 7, where the expression becomes &lt;SPAN style="font-family: courier new,courier;"&gt;(x - 128)/127&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt; instead of&lt;/SPAN&gt; (x - 128)/128&lt;/SPAN&gt;.&amp;nbsp; However, I suspect that this may possibly be an error within your look-up table for GL (which would actually need to be a 16-bit table, rather than the current 8-bit one for the expected value 256 for the final value).&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN class="mce_paste_marker"&gt;There may also be a problem with your cast for the calculation of &lt;EM&gt;tmpVal&lt;/EM&gt; since &lt;EM&gt;pdiff&lt;/EM&gt; and &lt;EM&gt;ratio&lt;/EM&gt; are already floats, and the result of the multiplication should be cast to an int.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;6.&amp;nbsp; With what you are attempting to do, I do not believe anything is to be gained using floating point calculations.&amp;nbsp; A similar degree of precision for the integer &lt;/SPAN&gt;&lt;SPAN class="mce_paste_marker"&gt;result should occur for a correctly sequenced integer calculation, giving potential reduction of code size.&amp;nbsp; Assuming that &lt;EM&gt;tmpVal&lt;/EM&gt; is changed to a long integer (to simplify any casting, and for compatibility with Convert2Gray() function parameter) perhaps you could try the following statement -&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker" style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp; tmpVal = ((long)(GC[i+1] - GC[i]) * (x - GL[i]) / GL[i]) + GC[i];&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;or&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;SPAN class="mce_paste_marker" style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp; tmpVal = ((long)(GC[i+1] - GC[i]) * (x - (1 &amp;lt;&amp;lt; i)) / (1 &amp;lt;&amp;lt; i)) + GC[i];&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;The first expression does not rely on the final (ninth) entry in the GL table, so this could remain an 8-bit table.&amp;nbsp; For the second expression, the GL table is not used. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;Regards,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;Mac&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Feb 2013 14:23:45 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/S08QE32-Won-t-Exit-Math-Function/m-p/268260#M20101</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2013-02-22T14:23:45Z</dc:date>
    </item>
    <item>
      <title>Re: S08QE32 Won't Exit Math Function</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/S08QE32-Won-t-Exit-Math-Function/m-p/268261#M20102</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Rod and Mac,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you very much your suggestions. I will reexamining my codes and include your recommendations.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Vu&lt;/P&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Feb 2013 15:52:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/S08QE32-Won-t-Exit-Math-Function/m-p/268261#M20102</guid>
      <dc:creator>Designer11</dc:creator>
      <dc:date>2013-02-22T15:52:59Z</dc:date>
    </item>
    <item>
      <title>Re: S08QE32 Won't Exit Math Function</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/S08QE32-Won-t-Exit-Math-Function/m-p/268262#M20103</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Guys,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It was the COP timeout. I found and fixed the problem. Thanks for your help&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BR&amp;lt;&lt;/P&gt;&lt;P&gt;Vu&lt;/P&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Mar 2013 16:12:13 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/S08QE32-Won-t-Exit-Math-Function/m-p/268262#M20103</guid>
      <dc:creator>Designer11</dc:creator>
      <dc:date>2013-03-01T16:12:13Z</dc:date>
    </item>
  </channel>
</rss>

