Author Topic: Converting BCD(mm) to BCD(inch)[SOLVED]  (Read 4525 times)

0 Members and 1 Guest are viewing this topic.

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6264
  • Country: fi
    • My home page and email address
Re: Converting BCD(mm) to BCD(inch)
« Reply #50 on: November 29, 2023, 11:00:13 pm »
Metric systems are not needed for feed rate control and mark setting. Millimeters and inches are an option for displaying information, and are not suitable for controlling iron.
What you really need is a fractional binary number (an unsigned fixed-point integer). By the way, rounding in such calculations is always obtained naturally - approximation to zero. The error does not accumulate!!!
This is a common misunderstanding, related to the error in the difference of two measurements.  If we use A and B for the readings, C for the difference, and a and b for the error in each measurement, then
    C = (B + b) - (A + a) = B - A + (b - a)

The misunderstanding comes from the mistaken belief that (b - a) means the errors will cancel out and not accumulate when rounding towards zero, i.e. 0≤a<1, 0≤b<1.  Yet, this is no more true or false than for any other consistent rounding scheme (i.e, -0.5<a≤+0.5, -0.5<b≤+0.5).

The actual maximum error in C is obtained by considering the extreme cases of (b - a).

For rounding towards zero, these are when (b is almost one, a is zero) and (b is zero, a is almost one): -1 < b-a < +1.

For standard rounding, halfway up, they are when (b is almost half, a is negative half) and (b is negative half, a is almost half): -1 < b-a < +1.

Thus, as long as you use your rounding method consistently, the error in your measurements will stay the same.  If you expand the same examination to multiple measurements, you will find that the error bounds evolve the exact same way, too.

However, when we do mapping, proper selection of the rounding method to be used is crucial.  The best example of this is calculating a specific number in the Fibonacci series via rounding: \$F_n = \left\lfloor \frac{\varphi^n}{\sqrt{5}}\right\rceil, \quad n \ge 0, \quad \varphi = \frac{1 + \sqrt{5}}{2}\$.)

In the case here, conversion from hundredths-of-a-millimeter (or equivalently tens-of-micrometers) to even-ten-thousandths-of-an-inch (or equivalently five-thousandths-of-an-inch) is only bijective when rounding halfway up.
 
The following users thanked this post: SiliconWizard

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: Converting BCD(mm) to BCD(inch)
« Reply #51 on: November 30, 2023, 05:10:59 am »
At this point, the code is rough, linear, and lengthy.  I will be using indirect addressing or a macro to avoid all the clutter from repeated sequences for each byte.
Looks perfectly fine? Why that obsession of optimizing something that doesn't need it?
I would understand this if it was the Voyager being updated at billion miles away, it's so far away that it can only do 16 bits per second!
But this, you'll get the mcu program done, close the device and forget about it, optimized or not.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline jpanhaltTopic starter

  • Super Contributor
  • ***
  • Posts: 3479
  • Country: us
Re: Converting BCD(mm) to BCD(inch)
« Reply #52 on: November 30, 2023, 09:09:44 am »
It's not an obsession.  I just enjoy the challenge and have no deadlines in the Winter.  I have to effectively stop coding come April -- my 20 acre hobby keeps me busy. 
 
The following users thanked this post: DavidAlfa

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: Converting BCD(mm) to BCD(inch)
« Reply #53 on: November 30, 2023, 03:11:17 pm »
Understood, then I completely agree, for the fun!  :-+
I missinterpreted the reason, its common to see things like "My code uses 20KB please help!" to later see he has 100KB+ free but he still thinks we're in 1980... It's there to be used! :-DD
« Last Edit: November 30, 2023, 03:15:41 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: zapta

Offline jpanhaltTopic starter

  • Super Contributor
  • ***
  • Posts: 3479
  • Country: us
Re: Converting BCD(mm) to BCD(inch)
« Reply #54 on: December 02, 2023, 10:36:26 am »
Just a final note.  I have attached alternative code using indirect addressing (FSR0 andFSR1).

Statistics based on a single test number, 56795 (567.95 mm):
Full FSR (this code)
Total Tcy: 215
Math Tcy: 43
Total instructions: 32 + 15 (+ table = 150, i.e., 10x15) = 197

Linear (no FSR)
Total Tcy: 144
Math Tcy: 28
Total instructions:  55+ 28 (+ table = 150) = 233

EDIT:  Delete  the -303 (an out of range message) and the comment about using db rather that dt or dtm.

EDIT2:
I just noticed a bit of sloppy code that the assembler fixed.  It is in the section where FSR0 is reading the table.  The current line reads, " addfsr    -10"  The assembler used the default FSR0 register, which happens to be correct.  The correct line should be:  "addfsr    0,-10"   The current attachment has been fixed.
 
« Last Edit: December 03, 2023, 12:40:48 pm by jpanhalt »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf