32 bit unsigned integer calculations with 10 digit decimal output (with code)

Terry Smith

New Member
I recently made a pulse timer with an LCD display to measure servo pulses. It then occurred to me that the unit could be modified to display frequency as a frequency counter or tachometer. The relationship between period and frequency is frequency = 1/time period so the challenge was to use the PicAxe maths to calculate the reciprocal.

As a reciprocal is just a special case of A*B/C where A and B are fixed, I needed to calculate A*B/C. I found a couple of posts where the subject is explored but decided to have a go at doing it for myself.

In addition, as the answer would need to be displayed, the 32 bit high and low words would have to be converted to the equivalent decimal. I share my thoughts and the code in case they are useful to others.

The attached “TLR 32 Bit to Decimal Demo 1.0.txt” code contains two subroutines. The first is “Calculate”, (94 bytes) that derives an EXACT answer to A*B/C where A, B and C are ANY value between 1 and 65535. Variables containing the high word, the low word and the remainder are returned giving a 32 bit answer. The second is “Convert”, (99 bytes) that converts these high and low word variables to their decimal equivalent and then stores the individual digits in RAM. For the demo, the 10 decimal output digits are sent via the serial port and can be displayed in the Editor Serial Terminal (F8) or an AXE-133 or similar LCD. Alternatively, the code can be run in the Programming Editor simulator. Both subroutines can be used in any programme that provides the required variables and constants. For the demo, input values for A, B and C can be entered into the programme where indicated and when the programme is run the 10 digit decimal answer will be sent via the serial port. The programme will run once and then stop.

In a loop, the programme calculated and converted 1000 answers to different A*B/C in between 9S and 26S giving a calculation plus conversion time of between 9mS and 26mS.

The demo programme was produced and tested in the Programme Editor but has a .txt extension for ease of viewing. It must be renamed to .bas for it to be run in the Programme Editor simulator or be downloaded into a chip.

I also attach “32 Bit Subroutine Demo.pdf” giving details of the methods used in the subroutines and some possible uses.
 

Attachments

Top