ADC10>pulsout and display pulse time

I want to pulseout (at 32MHz) with a time value straight from the ADC10 input and display the pulse time on a 2X8 OLED display, the thing I cant figure out is how to display a time such as "1279.25u", if I reduce the maximum time by dividing ADC10 by 10 I can display the time like this:

w10=w9*125+50
bintoascii w10,b6,b7,b8,b9,b10
gosub print

where w9 is the ADC10 value.
But if I want up to "1280.50u" (ADC10 +1 *125+50) for example it results in an overflow.
Note: I add 1 to ADC10 so the minimum is 1.

I cant do w9*12 because its inaccurate, so how do I achieve this without exceeding 65535?
 

hippy

Technical Support
Staff member
Using ** was my first thought but the result of an overflowed value is a modulo 65536 rather anything conveniently power of ten.

The easy option would be find 32-bit maths routines which can deliver the result as separate digits.

Doing it in native PICAXE basic I am guessing the solution would be to multiply something which doesn't overflow and then take the rest, adjusted so it doesn't overflow either and combine the two together.

One way might be to split the adc 0-1023 reading into 4 digits ...
Code:
d1    = adc        // 10 
d10   = adc / 10   // 10
d100  = adc / 100  // 10
d1000 = adc / 1000
Multiply each by 'something' then add the results back together.

Or you could split '1023' into two parts '10' and '23'.

It's not something I have an immediate answer on but I'll think about it.
 

marks

Senior Member
symbol ADC10 =w9
adc10=1023 'test
w1=adc10+1*5+2/4 '(ADC10 +1 x125 +50)
w0=adc10+1*5+2//4*25 'steps 1.25
sertxd(#w1,".",#w0,13,10)'(1280.50)
 

AllyCat

Senior Member
Hi,

There might be more elegant ways, but a general solution is to calculate a 32 bit product (e.g. for tens of ns) by using the ** (for the High word) and * (for Low word) operators. Then I would divide that result by 100 to give an Integer result (microseconds) and Remainder (hundredths of us).

The division is quite easy as I wrote a Code Snippet some years ago; the latest version is here, with a hot link to the earlier Snippet. Basically you could use a program something like this:

Code:
w1 = var1 * var2    ; Low word
w2 = var1 ** var2   ; High word
w3 = 100     ; Divisor
call ddiv     ; "Double-Word division (max result 16 bits)
integer_us = w1    ; Units
fractional_us = w2   ; Hundredths
Than send the values to the LCD using BINTOASCII or / and // operators.

Cheers, Alan.
 

hippy

Technical Support
Staff member
My thoughts were ...
Code:
result = N * 125 + 50

result = ( K * 125 + 50 ) + ( (N-K) * 125 )
Where K = 500 to 523, and these numbers probably make the addition easier ...
Code:
502 * 125 + 50 = 62800
506 * 125 + 50 = 63300
510 * 125 + 50 = 63800
514 * 125 + 50 = 64300
518 * 125 + 50 = 64800
522 * 125 + 50 = 65300
 
Last edited:
Top