ADC Sampling Breakthrough - Page 52 — Parallax Forums

# ADC Sampling Breakthrough

## Comments

• @evanh

Got it working with a good understanding.

Thanks
• edited 2020-02-02 22:17
@evanh

Got it working with idot. Thanks and I have a better understanding

about to use the other routine
• @ersmith

Got the dec_to_ascii working and understood with some help from @evanh

Thanks much
• @evanh

Got the dec_to_ascii from @ersmith working. Now trying to get the remainder from qdiv working. Even numbers are of course zero.

Looking around as to how to get the remainder e.g. 25/3 = 8.33
Any ideas?

Thanks
• edited 2020-02-09 09:53
Pilot,
You've messed up the variable ordering again!
```ax              long   1
number          long     1
adder   long 1
bcdi long 1
bcdlen long 1
bcdstr res 2          'IMPORTANT: overlays digit1/2
digit1 long 1         '  must follow bcdstr
digit2 long 1
```

EDIT: Ah, I note you've deleted my code. You may as well delete the variables too.
```ax              long   1
number          long     1
adder   long 1
digit1 long 1
digit2 long 1
```
• Remainder is a useful result to have ... but it is not a fractional number. In remainder terms, 25 / 3 equals 8r1 rather than 8.3333.
• Understood. I was trying different things with the two printing methods.
I will put it back and try again. Thanks
evanh wrote: »
Remainder is a useful result to have ... but it is not a fractional number. In remainder terms, 25 / 3 equals 8r1 rather than 8.3333.

since I am not a mathemitician, gladly not , how do I make 8 r 1 become 8.33?

Thanks
• pilot0315 wrote: »
Understood. I was trying different things with the two printing methods.
I will put it back and try again. Thanks
evanh wrote: »
Remainder is a useful result to have ... but it is not a fractional number. In remainder terms, 25 / 3 equals 8r1 rather than 8.3333.

since I am not a mathemitician, gladly not , how do I make 8 r 1 become 8.33?

Thanks

You want to decimal places, so you do 1*100/3=33.
• edited 2020-02-09 23:26
If you want it as a 16-bit binary fraction for, say, 16.16 fixed point format then the fraction (of 64k) = remainder * 65536 / divider.

• I will try that.
Thanks
dnalor wrote: »
pilot0315 wrote: »
Understood. I was trying different things with the two printing methods.
I will put it back and try again. Thanks
evanh wrote: »
Remainder is a useful result to have ... but it is not a fractional number. In remainder terms, 25 / 3 equals 8r1 rather than 8.3333.

since I am not a mathemitician, gladly not , how do I make 8 r 1 become 8.33?

Thanks

You want to decimal places, so you do 1*100/3=33.

• I used a calculator to check that out and it gave me 33.3333. So you are saying to take the remainder the 1 and use that formula. That gives me a new integer and a floating point.

Still not clear in coding the asm for that formula

division
qdiv dividend,divisor

getqx dig1
getqy dig2

mov number ,dig1
call #dec_to_ascii
call #putnl
mov number,dig2
' qmul number,remainder

call #dec_to_ascii

I had some laptop problems today and did not get to slowly try to work this. I will try later tonight.

Thanks
• pilot0315 wrote: »
I used a calculator to check that out and it gave me 33.3333. So you are saying to take the remainder the 1 and use that formula. That gives me a new integer and a floating point.

Still not clear in coding the asm for that formula

division
qdiv dividend,divisor

getqx dig1
getqy dig2

mov number ,dig1
call #dec_to_ascii
call #putnl
mov number,dig2
' qmul number,remainder

call #dec_to_ascii

I had some laptop problems today and did not get to slowly try to work this. I will try later tonight.

Thanks

What are you dividing by, and how many decimal places do you want?

If you are trying to develop something generally applicable you'll need much more effort than if you are making something specific.

So, for 25/3 the result you'd expect when doing it on a basic calculator is 8.33... (out to the number of digits it can display)
But doing it the old-school (paper) way it would become 8 1/3 (improper fraction)
Now, qdiv gives results of 8 (quotient) and 1 (remainder) which is the improper fraction without the divisor but you already know that.

A specific divide by 3 routine can only give 3 possible results for the remainder (0, 1, & 2)

For a display routine, you could select the number of decimal places you want to display (e.g. 2) and then select a pre-canned string to display (e.g. .00, .33, or .67)

This can work for other divisors too (4: .00, .25, .50, .75)

For a small number of divisors you could create a look-up table of results

You could accommodate the decimal point and three digits in a single long per result and for most applications that would be sufficient.

A simple lookup could be performed with something like (untested)
```  ALTS dig2, #rem3   'where #rem3 is the address of the table encoding the strings of the remainders for divide by 3
'can have tables for any single integer divisors required and with self-modifying code to replace the table address use the same code loop
MOV temp, 0-0      'temp holds remainder string
REP  #3, #4            'print remainder string (S= #4 for 3 digits, #3 for 2 digits)
GETBYTE pa, temp, #0   '0 or 3, depending on encoding order (big-endian or little-endian)
SHR temp, #8        'SHR or SHL, depending on encoding order
CALL #send_char
```

This would be used in a more generic loop, where you loop dividing by 10 until you get to the decimal point and then use this code for the remainder.
• edited 2020-02-10 07:31
@AJL

you really make it complicated.

The remainder is the rest left over after calculating the full division. Now since we want a decimal output, the question is how many digits.

So we need to multiply the remainder by 10 for each digit and do the division again for the rest, getting a result and a new remainder.

no need for tables...

Mike
• > @msrobots said:
> @AJL
>
> you really make it complicated.
>
> The remainder is the rest left over after calculating the full division. Now since we want a decimal output, the question is how many digits.
>
> So we need to multiply the remainder by 10 for each digit and do the division again for the rest, getting a result and a new remainder.
>
> no need for tables...
>
> Mike

@msrobots,

That is the general approach which is slower (two extra operations) and also requires some effort to deal with rounding (a runtime decision). Sometimes ‘complicated’ is actually simpler when the full context is understood.

Additionally, the OP has already shown some difficulty in understanding what to do with the non-terminating remainders that can result from upstream operations (like dividing 25 by 3 and printing the result). I simply have shown an embedded systems solution that doesn’t rely on floating point libraries. If it works for him what’s the damage to you?

If you care to note, my first question was what he was dividing by.
• @AJL
@msrobots

Thanks to both of you as well as @evanh

• @AJL
@msrobots
@evanh

His making it complicated and your making it simple gave me a better understanding.
• Mission accomplished.
Glad to be of assistance.
• @AJL
@evanh
@msrobots

You guys have been helping me learn how to do math in P2asm.
My goal is to use the math to learn asm as well as to hopefully use it in future projects.

Thanks

• @evanh
@AJL
Is there a plain english version of the P2asm command list??

Thanks

• @evanh

Thanks
• @evanh

How are you, been working nights for 5 weeks now back with the living.
Finally got back to coding brain was fried.
I simply multiplied 7x3 and got 21.

Where does the 20 come from??
• msrobots wrote: »
@AJL

you really make it complicated.

The remainder is the rest left over after calculating the full division. Now since we want a decimal output, the question is how many digits.

So we need to multiply the remainder by 10 for each digit and do the division again for the rest, getting a result and a new remainder.

no need for tables...

Mike

Howdy,
I tried to follow what you said and I did not get anything that follows your guidance. What did I screw up.
• pilot0315 wrote: »
I simply multiplied 7x3 and got 21.
Where does the 20 come from??
What 20? Do you mean the 2 from the GETQY? That 2 is because #itod routine is using cordic QDIV and that's the final remainder it generated. Best solution is grab both components together, eg:
```                 qmul number,multiplier
getqx answer
getqy decimal
```

Also, your program is going off into la-la-land at the end without any stop or loop. I usually put a JMP to self at the end of my main test routine:
```		jmp	#\$
```
• @evanh

I will rewrite it and try that.

Thanks
Sign In or Register to comment.