Author Topic: BCD Subtraction with PIC16F1829  (Read 1302 times)

0 Members and 1 Guest are viewing this topic.

Offline jpanhaltTopic starter

  • Super Contributor
  • ***
  • Posts: 4080
  • Country: us
BCD Subtraction with PIC16F1829
« on: September 26, 2024, 12:05:05 pm »
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/#msg5624315

Due 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.


« Last Edit: September 27, 2024, 10:28:49 am by jpanhalt »
 

Offline Andy Chee

  • Super Contributor
  • ***
  • Posts: 1399
  • Country: au
Re: BCD Subtraction with PIC16F1829
« Reply #1 on: September 26, 2024, 12:24:52 pm »
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?

 

Offline jpanhaltTopic starter

  • Super Contributor
  • ***
  • Posts: 4080
  • Country: us
Re: BCD Subtraction with PIC16F1829
« Reply #2 on: September 26, 2024, 12:49:11 pm »
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.
 

Offline Mia Barn

  • Newbie
  • Posts: 4
  • Country: pk
Re: BCD Subtraction with PIC16F1829
« Reply #3 on: October 02, 2024, 07:36:59 pm »
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?
 

Offline jpanhaltTopic starter

  • Super Contributor
  • ***
  • Posts: 4080
  • Country: us
Re: BCD Subtraction with PIC16F1829
« Reply #4 on: October 02, 2024, 08:12:28 pm »
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.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6438
  • Country: es
Re: BCD Subtraction with PIC16F1829
« Reply #5 on: October 05, 2024, 03:26:49 pm »
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  :)
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline jpanhaltTopic starter

  • Super Contributor
  • ***
  • Posts: 4080
  • Country: us
Re: BCD Subtraction with PIC16F1829
« Reply #6 on: October 05, 2024, 04:14:03 pm »
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.
 
The following users thanked this post: Nominal Animal, DavidAlfa, Dazed_N_Confused


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf