<?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: binary fraction multiplication in HCS08 assembler in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203699#M16875</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;(First of all, with a title of "binary fraction multiplication in ... assembler" you only scare people away! :smileyhappy:)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, in the simplest case you want to do something like Actual_ADC_Reading * Max_Voltage / MAX_ADC_VALUE&lt;/P&gt;&lt;P&gt;(To minimize errors, first multiply, then divide.&amp;nbsp; You will need the corresponding math library routines for multiplication and division.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Let's assume Max_Voltage&amp;nbsp; is 5V.&amp;nbsp; MAX_ADC_VALUE is 255 for 8-bit mode, 1023 for 10-bit mode, and 4095 for 12-bit mode.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want one decimal place, then Max_Voltage should become 50 (or, if you need two decimal places, 500).&amp;nbsp; Then, the integer number you get will have one (or two, for the 500 case) implied decimal place(s).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When you convert the number to a string so you can print it on your LCD, simply make the string long enough for the decimal places you want (by adding the correct number of leading zeros, when needed) and then, add the decimal point one (or two) place(s) before the end of the string.&amp;nbsp; (As a reference, see the AddDecimalPoint routine in &lt;A href="http://www.aspisys.com/code/hc08/adddecimalpoint.html"&gt;here&lt;/A&gt;.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 12 Dec 2012 09:45:20 GMT</pubDate>
    <dc:creator>tonyp</dc:creator>
    <dc:date>2012-12-12T09:45:20Z</dc:date>
    <item>
      <title>binary fraction multiplication in HCS08 assembler</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203698#M16874</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi guys.. I am capturing voltage values from ADC &amp;amp; would like to display the voltage value captures on the LCD with minimum 1 decimal place. Does anyone has some tips to do the math in assembler?&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;MY&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Dec 2012 16:28:48 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203698#M16874</guid>
      <dc:creator>mylim</dc:creator>
      <dc:date>2012-12-11T16:28:48Z</dc:date>
    </item>
    <item>
      <title>Re: binary fraction multiplication in HCS08 assembler</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203699#M16875</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;(First of all, with a title of "binary fraction multiplication in ... assembler" you only scare people away! :smileyhappy:)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, in the simplest case you want to do something like Actual_ADC_Reading * Max_Voltage / MAX_ADC_VALUE&lt;/P&gt;&lt;P&gt;(To minimize errors, first multiply, then divide.&amp;nbsp; You will need the corresponding math library routines for multiplication and division.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Let's assume Max_Voltage&amp;nbsp; is 5V.&amp;nbsp; MAX_ADC_VALUE is 255 for 8-bit mode, 1023 for 10-bit mode, and 4095 for 12-bit mode.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want one decimal place, then Max_Voltage should become 50 (or, if you need two decimal places, 500).&amp;nbsp; Then, the integer number you get will have one (or two, for the 500 case) implied decimal place(s).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When you convert the number to a string so you can print it on your LCD, simply make the string long enough for the decimal places you want (by adding the correct number of leading zeros, when needed) and then, add the decimal point one (or two) place(s) before the end of the string.&amp;nbsp; (As a reference, see the AddDecimalPoint routine in &lt;A href="http://www.aspisys.com/code/hc08/adddecimalpoint.html"&gt;here&lt;/A&gt;.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Dec 2012 09:45:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203699#M16875</guid>
      <dc:creator>tonyp</dc:creator>
      <dc:date>2012-12-12T09:45:20Z</dc:date>
    </item>
    <item>
      <title>Re: binary fraction multiplication in HCS08 assembler</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203700#M16876</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello MY,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The ADC result is given by the expression:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;N = Vi * Nmax / Vr&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vi is the direct input voltage to the ADC channel,&lt;/P&gt;&lt;P&gt;The reference voltage Vr is likely to equal Vdd.&lt;/P&gt;&lt;P&gt;Nmax = 1024 for 10-bit mode, or 4096 for 12-bit mode.&lt;/P&gt;&lt;P&gt;The result N has the range 0 to Nmax-1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Since you require a decimal result (rather than binary), you would multiply by a factor of 100 to achieve two decimal places for the displayed result.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;100 * Vi = N * 100 * Vdd / Nmax&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For 10-bit mode, with Vdd = 5 volts, you would require the following calculation to obtain the scaled binary result, which would then need to be converted to an ASCII numeric display sequence.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;100 * Vi = N * 500 / 1024 or N * 250 / 512&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;16 x 16 bit unsigned multiplication will be used for the first part of the calculation.&amp;nbsp; The 32-bit result will then be shifted 9 positions to the right to achieve the "power of two" division process.&amp;nbsp; This would give a result between 0 and 499, to be converted to the numeric ASCII sequence.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The attachment has subroutines to cater for the unsigned multiplication, and for the ASCII conversion.&amp;nbsp; There are two conversion rotines shown, one with left justification, and the other with right justification.&amp;nbsp; These routines could potentially be simplified since you would require only three display digits, rather than five digits for the full 16-bit range.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Dec 2012 13:29:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203700#M16876</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2012-12-12T13:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: binary fraction multiplication in HCS08 assembler</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203701#M16877</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Tony, thanks for the tips. &lt;/P&gt;&lt;P&gt;I will have to divide the max voltage by the total steps to get voltage per steps as the multiplier for the ADC values captured. The multiplier will probably be 0.something. I would need to know how to represent the 0.something in assembler and the subsequent calculations.&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;MY&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Dec 2012 15:50:21 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203701#M16877</guid>
      <dc:creator>mylim</dc:creator>
      <dc:date>2012-12-13T15:50:21Z</dc:date>
    </item>
    <item>
      <title>Re: binary fraction multiplication in HCS08 assembler</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203702#M16878</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi bigmac, as always the savior.. thanks for the reference code. I will check it out.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;MY&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Dec 2012 16:00:29 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203702#M16878</guid>
      <dc:creator>mylim</dc:creator>
      <dc:date>2012-12-13T16:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: binary fraction multiplication in HCS08 assembler</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203703#M16879</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No need to mess with floating-point math.&amp;nbsp; As any fraction 0.x can be represented as a a ratio (e.g., x/10, using as many digits as needed for the required precision,)&amp;nbsp; I don't really see with what you're having problems.&amp;nbsp; The above should still work.&amp;nbsp; Just do the math in a different order than you had thought about it.&amp;nbsp; Instead of voltage/step * steps to find the voltage corresponding to the A/D reading value, do it as shown already.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;(Otherwise, I don't yet understand what it is you're trying to do.)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Dec 2012 16:03:54 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203703#M16879</guid>
      <dc:creator>tonyp</dc:creator>
      <dc:date>2012-12-13T16:03:54Z</dc:date>
    </item>
    <item>
      <title>Re: binary fraction multiplication in HCS08 assembler</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203704#M16880</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;MY, have you checked out the suggested answers?&lt;/P&gt;&lt;P&gt;We would like to know how is the project going! :smileywink:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best regards!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Dec 2012 21:14:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203704#M16880</guid>
      <dc:creator>Monica</dc:creator>
      <dc:date>2012-12-17T21:14:09Z</dc:date>
    </item>
    <item>
      <title>Re: binary fraction multiplication in HCS08 assembler</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203705#M16881</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi bigmac.. &lt;/P&gt;&lt;P&gt;after going through the code.. I couldn't understand a part if my multiplier if 128 &amp;amp; multiplicand is 0.14. How do I relate this equation into the code?&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;MY&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Dec 2012 15:10:24 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203705#M16881</guid>
      <dc:creator>mylim</dc:creator>
      <dc:date>2012-12-19T15:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: binary fraction multiplication in HCS08 assembler</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203706#M16882</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P lang="en-GB"&gt;&lt;SPAN style="font-size: 10pt; font-family: Arial, sans-serif;"&gt;Hi Mylim,&lt;/SPAN&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;SPAN style="font-size: 10pt; font-family: Arial, sans-serif;"&gt;to make math in assembler, usually in a fixed point you must change your mind.&lt;/SPAN&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;SPAN style="font-size: 10pt; font-family: Arial, sans-serif;"&gt;First of all I assume you have to do simple calculations like those BigMac suggests to you, i.e. operations on 8 or 16 bit numbers which means an integer number ranging 0 to 2^16-1, i.e 0 to 65535. When you use a fixed point calculation you already know if this bigger number is an integer or represent a value in decimal or centesimal or millesimal format: I mean, if this represents a voltage, it may represent a value as large as 65535V or lower as 6.5535V&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; font-family: Arial, sans-serif;"&gt; if a single bit represents 1V o 100uV or any else in the middle or even higher or lower!.&lt;/SPAN&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;SPAN style="font-size: 10pt; font-family: Arial, sans-serif;"&gt;If you have to multiply it for a fractional value B, where B= 0.14 in your example, you must multiply your 16 bit number for another 16 bit constant, taking the higher 2 MsB of your 4 Byte result, i.e. 16 out of 32 bit.&lt;/SPAN&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;SPAN style="font-size: 10pt; font-family: Arial, sans-serif;"&gt;You must take in mind that in this case 2^16 (65536 dec or $10000) represents 1.0 (the unity). In your example 0.14 would be represented by a binary number in 16bit format equal to (65536*0.14)dec = 9175dec or $23D7&lt;/SPAN&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;SPAN style="font-size: 10pt; font-family: Arial, sans-serif;"&gt;Expanding your example C= A*B, where A=128, B=0.14, in 16*16 bit multiplication this would give:&lt;/SPAN&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;SPAN style="font-size: 10pt; font-family: Arial, sans-serif;"&gt;A=$0080, B=$23D7, C=$(0080*23D7)= $0011.EB80&lt;/SPAN&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;SPAN style="font-size: 10pt; font-family: Arial, sans-serif;"&gt;where $0011 represents the integer result, in fact $0011= 17dec or INT(128*0.14) and $EB80 the fractional part you may use or discard depending on your needs. In this case the fractional part is very near 1, in fact it means 0.92 and represent quite a large error compared to the integer part: 0.92/17= 0.054 or 5.4%. For a good precision your multiplicand "A" must be as high as possible to have a result with good precision where the discarded bits in the multiplication would be little meaningful. You could have done this by adding 2 dummy lsB (00) on the multiplicand to do $8000*$23D7. Your result would have been $11EB8000 or $11.EB8000 which is higher in precision than before: this is specially useful if you have needed to use this result as an intermediate 16 bit operand ($11EB) for another calculation.&lt;/SPAN&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;SPAN style="font-size: 10pt; font-family: Arial, sans-serif;"&gt;With a little care you may achieve a precision in the order of 0.05 to 0.01% at the end of all your calculations which is more than enough for any voltmeter based on a mCU embedded ADC.&lt;/SPAN&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;/P&gt;&lt;P lang="en-GB"&gt;&lt;SPAN style="font-size: 10pt; font-family: Arial, sans-serif;"&gt;Than you will finally need a routine which transforms your binary/hexadecimal result in a 3 to 5 digit decimal number, which is “&lt;SPAN style="font-family: Courier New, monospace;"&gt;&lt;SPAN lang="it-IT"&gt;CONVERT3” or “CONVERT4”&lt;/SPAN&gt;&lt;/SPAN&gt; from BigMac code snippet&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2012 21:17:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/binary-fraction-multiplication-in-HCS08-assembler/m-p/203706#M16882</guid>
      <dc:creator>Encoder1</dc:creator>
      <dc:date>2012-12-20T21:17:43Z</dc:date>
    </item>
  </channel>
</rss>

