<?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: How to do floating point operations ? in S12 / MagniV Microcontrollers</title>
    <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-do-floating-point-operations/m-p/1177804#M17569</link>
    <description>&lt;P&gt;This is a perfect reply. Thanks!&lt;/P&gt;</description>
    <pubDate>Wed, 04 Nov 2020 09:14:16 GMT</pubDate>
    <dc:creator>YTawil</dc:creator>
    <dc:date>2020-11-04T09:14:16Z</dc:date>
    <item>
      <title>How to do floating point operations ?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-do-floating-point-operations/m-p/1174248#M17559</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;About the problem:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I took a lot of time, but I'm still stuck in doing floating point mathematical operations in my &lt;SPAN class="fontstyle0"&gt;MC9S12ZVM&lt;/SPAN&gt; MCU program. Maybe the question is elementary, but usually in my other projects, the compiler handles the casting between floats and integers back and forth. In the project I use, which is a demo project of one of your developments board S12ZVMLMINIBRD_BLDC_SW_CW11 the default floating point is turned off that is why once I include any explicit floating point operation I ends up with compilation error like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Symbol __fdiv in file  MC9S12ZVML128_BLDC_Sensorless_c.obj is undefined S12ZVMLMINIBRD_BLDC_SW_CW11 C/C++ Problem&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I read parts of AMMCLIB manual and I tried to use the APIs like MLIB_Mul and MLIB_Div and so one, but I ended up with variables coded in 32/16 bit fixed point and did not find how to convert them back to integer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;The question:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One of the thing I want to do is:&lt;/P&gt;&lt;P&gt;1- Read one of the ADCs.&lt;/P&gt;&lt;P&gt;2- Convert to volt by multiplying the nummeric/digital value by 2.5V/1023.&lt;/P&gt;&lt;P&gt;3- Then scale the result using a voltage divider scale 0.85 (in my design).&lt;/P&gt;&lt;P&gt;4- Send the result back using CAN. Here I need to convert back from tFrac32 or tFrac16 to integer (how?)&lt;/P&gt;&lt;P&gt;Also is it possible and safe to enable the usual and direct floating point calculation for the demo project S12ZVMLMINIBRD_BLDC_SW_CW11 ? How (I did not find where from project settings)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 28 Oct 2020 06:00:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-do-floating-point-operations/m-p/1174248#M17559</guid>
      <dc:creator>YTawil</dc:creator>
      <dc:date>2020-10-28T06:00:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to do floating point operations ?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-do-floating-point-operations/m-p/1177790#M17568</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I understand that many people coming from high-end platforms or computer programming are used to use floating point operations. But when moving to low-cost solutions, they struggle to understand benefits of fixed point arithmetic.&lt;/P&gt;
&lt;P&gt;Here are some points to consider:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;S12Z core does not support floating point operations natively. Any floating point operation would take a lot of time in comparison to fixed point equivalent calculation (e.g. 150 cycles instead of 2-cycles). The very same algorithm done in floating point may take significantly higher time than fixed-point version.&lt;/LI&gt;
&lt;LI&gt;S12Z core is optimized to do many fixed-point calculations in a single step - we call it DSP support, which includes e.g. "MAC operations" - multiply and accumulate - typical implementation of integration in math (used in many signal processing functions or PI-controllers).&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;The real world is typically interfaced with analog-to-digital converters or PWM modulators, which are by nature integer-oriented. Further more, ADC in S12Z is only 12-bit and PWM modulator is only 15-bit, so there is no benefit in having those numbers in 32-bit floating point. Typically, 16-bit numbers are enough.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;In conclusion, I would recommend to have all the data in S12ZVM in fixed-point format (represented by tFrac16 or tFrac32). It seems that your CAN protocol accepts integers. Here you can see the benefit - no need to convert anything. You can just take tFrac16 number and treat it as Word16 (or short). Fixed-point tFrac16 numbers are internally stored as integers Word16 (short). The scale is the same as above, 0 to 32767 represents 0 - 5V in real-world scale.&lt;/P&gt;
&lt;P&gt;Solution to your task:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Read one of the ADC - use left-justified results (so if the number is 1,024 for 5V input, the result will be 65,535). Then, right-shift the result by one bit, so the result is 32,767 - this is already within the tFrac16 range. Now, you can store the result directly into your tFrac16 variable and consider the range as 0 - 32,767 is displayed as 0 - 1.0, which corresponds to real voltage scale of 0 - 5.0V.&amp;nbsp;I recommend to use some digital filtering, such as GDFLIB_MAfilter or similar. Again, no need to scale the result. However, if your CAN protocol requires to have the voltage in e.g. 10 V scale, then you would divide the result by 2 (better to use right-shift by one bit).&lt;/LI&gt;
&lt;LI&gt;No need for further conversion. You just need to make a note about the scale mentioned as above.&lt;/LI&gt;
&lt;LI&gt;If there is any scale, you would like to apply, again, no need to do any math. You just make another note about your new scale, that the range of 0 - 32767 in integer (or 0 - 1.0 in fixed-point representation) corresponds to whatever voltage range, e.g. 0 - 4.325 V.&lt;/LI&gt;
&lt;LI&gt;No need for any further action, you can just write the tFrac16 variable to the CAN message, but the recipient of the message should be aware of the scale - if it is e.g.&amp;nbsp;0 - 32767 means&amp;nbsp;0 - 4.325 V.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;If you have any further questions, please don't hesitate to reply.&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;Matej&lt;/P&gt;</description>
      <pubDate>Wed, 04 Nov 2020 08:52:27 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-do-floating-point-operations/m-p/1177790#M17568</guid>
      <dc:creator>pachamatej</dc:creator>
      <dc:date>2020-11-04T08:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to do floating point operations ?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-do-floating-point-operations/m-p/1177804#M17569</link>
      <description>&lt;P&gt;This is a perfect reply. Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 04 Nov 2020 09:14:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/How-to-do-floating-point-operations/m-p/1177804#M17569</guid>
      <dc:creator>YTawil</dc:creator>
      <dc:date>2020-11-04T09:14:16Z</dc:date>
    </item>
  </channel>
</rss>

