BCD Subtraction with PIC16F1829 - Page 1
Forum section: electronics / general

author: jpanhalt
author_rank: Super Contributor
timestamp: September 26, 2024, 12:05:05 pm
content: This is a follow-up to my project using an older Mitutoyo DRO to control threading on my lathe. See here and related posts:https://www.eevblog.com/forum/mechanical-engineering/mitutoyo-acu-rite-dro-zero-setting/msg5624315/#msg5624315Due to other factors, my work on that project was suspended until now. It turns out there are two easy methods to set "zero" on those and most likely other current similar models from Mitutoyo: 1) Toggle the low-voltage (1.5V) to the sensor; and 2) Subtract the reference reading from current reading to give a zero. Toggling must have a short time off, perhaps a few milliseconds that I have yet to determine, and mathematical correction is probably quicker. While Microchip Assembly for 8-bit enhanced midrange devices may be archaic, I thought presenting it here for comment might help improve it and save others the rather dull task of writing it themselves. Unfortunately, PICList, which served as a repository for such things is now dead. Those archives have code for a single byte of packed BCD. My code was developed for the full 7 digits of BCD plus sign necessary for the Mitutoyo (e.g., ab.cdef in inches or abc.def in mm).Two versions are presented. The first is is simple linear code ("Subtr") without indirect addressing. The second version uses the two indirect FSRn registers for those who may be short of instruction flash memory ("FSR_Subtr").The attached code is not simply Plug&Play. It includes code for testing various combinations of minuend ("x") and subtrahend ("y"). The difference is saved in registers z(n) for the non-FSR method and in registers x(n) for the FSR method. Be sure to add leading zeros if those registers are used. In the FSR method, the jump table for test values relies on having those registers. That is, the jump is 17 (2 lines of code + 14 values + 0) because of the way the "decfsz" instruction works. A disassembly listing will calculate the correct jump between two test entries, if you are not sure.Timing shows that the FSR method is slower, as expected. For the FSR method, time for calculation of the difference was approximately 181 Tcy for negatives and 103 Tcy for positives. Calculation of the 10's complement for negatives of 77 Tcy is included. The non-FSR method took 58 to 62 Tcy and 54 Tcy for subtraction and 10's complement , respectively.EDIT: I forgot to mention I use MPLab 8.92. Some of the Assembler macros I used may not work with current versions.

author: Andy Chee
author_rank: Super Contributor
timestamp: September 26, 2024, 12:24:52 pm
content: Any reason why you can't use double dabble and reverse double dabble, to convert BCD to binary, do the add/sub in binary, then convert back to BCD?

author: jpanhalt
author_rank: Super Contributor
timestamp: September 26, 2024, 12:49:11 pm
content: This step will proceed conversion from metric to inches using BCD and a table. Code for those steps was posted previously. Double-dabble while very short code has a long execution time.The particular Mitutoyo scales being used are "absolute" reading, and the reference is not lost during ordinary power down. Both caliper-type and DRO scales use the same inductive technology developed by Mitutoyo, but they differ in detail on how results are presented and zeroed. My lathe project will use a DRO scale for one axis and a caliper-type scale for the crossfeed.

author: Mia Barn
author_rank: Newbie
timestamp: October 02, 2024, 07:36:59 pm
content: i've seen the decription and visited the link. I must say it is an interesting project and I am glad you shared this with us. Do you have any video or any other visual decription?

author: jpanhalt
author_rank: Super Contributor
timestamp: October 02, 2024, 08:12:28 pm
content: Thank you for the interest.Sorry, no video yet. For me, doing that is complicated as all I have is a still camera (no smartphone). I try to include photos, and as the project gets further along, I will certainly attach more of them.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 05, 2024, 03:26:49 pm
content: Regardless of the execution time, that PIC is pretty fast and should be able to perform thousands of bcd operations per second even with the worst code, why do you need to polish the code for perfect efficiency?Unless you're doing it just for fun, of course

author: jpanhalt
author_rank: Super Contributor
timestamp: October 05, 2024, 04:14:03 pm
content: I write mostly for relaxation and to make time pass. It's also the challenge versus TV or going to an old people's gathering. I have been retired for 18 years. When I was much younger (7 to 12), my dad and I played chess every night instead of watching TV. It was that same combination for fun and challenge.