<?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: Floating point division problems in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Floating-point-division-problems/m-p/203443#M16836</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Thanks for the reply. You were correct, some parts of the problem were due to the stack problems.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There were two major errors. First one was that I had created the project using different allocation sizes for float and double. Then, I changed them back to be the same in project setting. That was basically the source of the memory spill overs.&lt;/P&gt;&lt;P&gt;To solve it, I created a new clean project from scratch, added my source files to it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The second problem was that I was running out of RAM in my HCS08QG microcontroller. For some reason, the compiler and linker didn't give me errors, however I believe there were problems with that. So, I just removed some parts unrelated code, and now its working like a charm.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 09 Jun 2010 00:42:37 GMT</pubDate>
    <dc:creator>Ashkan</dc:creator>
    <dc:date>2010-06-09T00:42:37Z</dc:date>
    <item>
      <title>Floating point division problems</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Floating-point-division-problems/m-p/203441#M16834</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;In the following code, the simple division creat problems with the counter. It seems there is a spill over of the division into other parts of memory.&amp;nbsp; In other terms, there is a memory leak, and memory spill over.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have had this problem many other times with the Codewarrior for HCS08 compiler. If you are lucky you can see the other non related variables change when a division is executed. (Spill over to other parts of memory)&lt;/P&gt;&lt;P&gt;In the following specific example, the line of &amp;nbsp;&amp;nbsp; filteredI = 0.996*(lastFilteredI+sampleI-lastSampleI);&amp;nbsp; , changes the counter&amp;nbsp; n to zero ) So the loop never ends &lt;SPAN aria-label="Happy" class="emoticon_happy emoticon-inline" style="height:16px;width:16px;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I played with the code a bit, and increased the stack size. There is no spill over anymore, however the following line reset the MCU:&amp;nbsp; Vrms = VCAL*sqrt(sumV / numberOfSamples);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code is a&amp;nbsp; good sample for ADC conversions...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me know if you guys have any insight to the problem....&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;//Calibration coeficients&lt;BR /&gt;//These need to be set in order to obtain accurate results&lt;BR /&gt;double VCAL = 1.0;&lt;BR /&gt;double ICAL = 1.0;&lt;BR /&gt;double PHASECAL = 2.3;&lt;BR /&gt;&lt;BR /&gt;//Sample variables&lt;BR /&gt;int lastSampleV,lastSampleI,sampleV,sampleI;&lt;BR /&gt;&lt;BR /&gt;//Filter variables&lt;BR /&gt;double lastFilteredV, lastFilteredI, filteredV, filteredI;&lt;BR /&gt;double filterTemp;&lt;BR /&gt;&lt;BR /&gt;//Stores the phase calibrated instantaneous voltage.&lt;BR /&gt;double calibratedV;&lt;BR /&gt;&lt;BR /&gt;//Power calculation variables&lt;BR /&gt;double sqI,sqV,instP,sumI,sumV,sumP;&lt;BR /&gt;&lt;BR /&gt;//Useful value variables&lt;BR /&gt;double realPower,&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; apparentPower,&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; powerFactor,&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Vrms,&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Irms;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;int n;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;//Setup variables&lt;BR /&gt;#define numberOfSamples&amp;nbsp; 3000&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;void main(void)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; /* Write your local variable definition here */&lt;BR /&gt;&amp;nbsp; unsigned char i;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/&lt;BR /&gt;&amp;nbsp; PE_low_level_init();&lt;BR /&gt;&amp;nbsp; /*** End of Processor Expert internal initialization.&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;&lt;BR /&gt;&amp;nbsp; /* Write your code here */&lt;BR /&gt;&amp;nbsp; /* For example: for( ; ; ) { } */&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; //SendStr(Introduction);&lt;BR /&gt;&amp;nbsp; if ( xbee_SetCommandMode() != false )&lt;BR /&gt;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xbeeResponse = xbee_SendCommand("ATPL4\r");&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xbee_ExitCommandMode();&lt;BR /&gt;&amp;nbsp; }&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;for (n=0; n&amp;lt;numberOfSamples; n++)&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //Used for offset removal&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; lastSampleV=sampleV;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; lastSampleI=sampleI;&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; //Read in voltage and current samples.&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;while ( AD1_Measure(TRUE) !=ERR_OK )&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;if (&amp;nbsp; AD1_GetValue16((unsigned int *) &amp;amp;ADC_Values) != ERR_OK ) &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;continue;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sampleV = ADC_Values[VAC_ADC];&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sampleI = ADC_Values[I1AC_ADC];&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; //Used for offset removal&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; lastFilteredV = filteredV;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; lastFilteredI = filteredI;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //Digital high pass filters to remove 2.5V DC offset.&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; filteredV = 0.996*(lastFilteredV+sampleV-lastSampleV);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; filteredI = 0.996*(lastFilteredI+sampleI-lastSampleI);&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; //Phase calibration goes here.&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; calibratedV = lastFilteredV + PHASECAL * (filteredV - lastFilteredV);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //Root-mean-square method voltage&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //1) square voltage values&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sqV= calibratedV * calibratedV;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //2) sum&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sumV += sqV;&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; //Root-mean-square method current&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //1) square current values&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sqI = filteredI * filteredI;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //2) sum&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sumI += sqI;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //Instantaneous Power&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; instP = calibratedV * filteredI;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //Sum&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sumP +=instP;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;//Calculation of the root of the mean of the voltage and current squared (rms)&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;//Calibration coeficients applied.&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;Vrms = VCAL*sqrt(sumV / numberOfSamples);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;Irms = ICAL*sqrt(sumI / numberOfSamples);&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;//Calculation power values&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;realPower = VCAL*ICAL*sumP / numberOfSamples;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;apparentPower = Vrms * Irms;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;powerFactor = realPower / apparentPower;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;sendStr("\r\n realPower:");&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;SendWordValue(realPower );&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; /*** Don't write any code pass this line, or it will be deleted during code generation. ***/&lt;BR /&gt;&amp;nbsp; /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/&lt;BR /&gt;&amp;nbsp; for(; ; ){}&lt;BR /&gt;&amp;nbsp; /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/&lt;BR /&gt;} /*** End of main routine. DO NOT MODI&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 May 2010 11:04:53 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Floating-point-division-problems/m-p/203441#M16834</guid>
      <dc:creator>Ashkan</dc:creator>
      <dc:date>2010-05-27T11:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: Floating point division problems</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Floating-point-division-problems/m-p/203442#M16835</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is math.h included? Missing function prototypes can be harmful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Did you analyze the cause of reset? Was it COP, ILAF, ILOP? Please check your derivative datasheet what do these bits mean and where you can read them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code below requires not less than ~150bytes of stack. You may check that for example&amp;nbsp;with simulator. Point data window to address&amp;nbsp;you see in SP register, scroll data window up until you see area filled with 'uu' - undefined. After execution of your code you may see how what bytes in stack space were modified and calculate used stack space.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 May 2010 13:03:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Floating-point-division-problems/m-p/203442#M16835</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2010-05-27T13:03:33Z</dc:date>
    </item>
    <item>
      <title>Re: Floating point division problems</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Floating-point-division-problems/m-p/203443#M16836</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Thanks for the reply. You were correct, some parts of the problem were due to the stack problems.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There were two major errors. First one was that I had created the project using different allocation sizes for float and double. Then, I changed them back to be the same in project setting. That was basically the source of the memory spill overs.&lt;/P&gt;&lt;P&gt;To solve it, I created a new clean project from scratch, added my source files to it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The second problem was that I was running out of RAM in my HCS08QG microcontroller. For some reason, the compiler and linker didn't give me errors, however I believe there were problems with that. So, I just removed some parts unrelated code, and now its working like a charm.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jun 2010 00:42:37 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Floating-point-division-problems/m-p/203443#M16836</guid>
      <dc:creator>Ashkan</dc:creator>
      <dc:date>2010-06-09T00:42:37Z</dc:date>
    </item>
    <item>
      <title>Re: Floating point division problems</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Floating-point-division-problems/m-p/203444#M16837</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;The linker will show errors only if the defined global and static variables, plus the specified stack size cannot be allocated within the available RAM,&amp;nbsp;&amp;nbsp;which does not usually include zero page RAM by default.&amp;nbsp; The linker does not estimate run time stack usage.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The determination of a suitable stack size is up to the programmer.&amp;nbsp; The stack size specified in the default PRM file is generally insufficient for floating point operations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would suggest that you check the allocation of the global and static variables within the map file for&amp;nbsp; the project, to determine the amount of RAM used.&amp;nbsp; The stack is normally allocated immediately above these variables.&amp;nbsp; The stack may usually be increased in size to occupy the remaining space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is possible that zero page RAM may not be used at all.&amp;nbsp; You might consider allocating some of the global variables to zero page RAM, to more fully utilize the RAM capacity.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is possible that the last global variable defined will have the highest address (check this out in the map file).&amp;nbsp; This variable might be allocated as a dummy variable, and initialised to a specific value.&amp;nbsp; If the value contained in the variable were corrupted, this would inticate the prior occurrence of a stack overflow, and that other variables may have become corrupted.&lt;/P&gt;&lt;P&gt;&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>Wed, 09 Jun 2010 12:35:46 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Floating-point-division-problems/m-p/203444#M16837</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2010-06-09T12:35:46Z</dc:date>
    </item>
  </channel>
</rss>

