ADC Sampling Breakthrough - Page 52 — Parallax Forums

• Posts: 662
@evanh

Got it working with a good understanding.

Thanks
• Posts: 662
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
• Posts: 662
@ersmith

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

Thanks much
• Posts: 662
@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
• Posts: 12,030
edited 2020-02-09 09:53
Pilot,
You've messed up the variable ordering again!
```ax              long   1
number          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
digit1 long 1
digit2 long 1
```
• Posts: 12,030
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.
• Posts: 662
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
• Posts: 191
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.
• Posts: 12,030
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.

• Posts: 662
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.

• Posts: 662
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
• Posts: 457
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.
• Posts: 3,486
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
• Posts: 457
> @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.
• Posts: 662
@AJL
@msrobots

Thanks to both of you as well as @evanh

• Posts: 662
@AJL
@msrobots
@evanh

His making it complicated and your making it simple gave me a better understanding.
• Posts: 457
Mission accomplished.
• Posts: 662
@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
• Posts: 662

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

Thanks

• Posts: 662
@evanh

Thanks
• Posts: 662
@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??
• Posts: 662
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.
• Posts: 12,030
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
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	#\$
```
• Posts: 662
@evanh

I will rewrite it and try that.

Thanks
• Posts: 559

@Parallax
I have tried to read/understand the P2 docu and this thread regarding Syn2 filter mode and Sync3 filter mode. The info given is good enough to activate these modes in my code. But the info is not good enough to be able to rely on these modes. Some other people have stated the same discomfort in this thread. What are the limits, downsides and caveats to use these filters?
In my experience successful engineering is not to make impossible things true but to know limits and respect them.

So let me ask the question, if it is planned to improve the information regarding these modes?
Thank you! Christof

• Posts: 12,030

What isn't so great is the noise floor and drift of the internal ADCs.

The sinc2/3 filters work well. They're just extensions of the simpler sinc1 (box filter) that everyone has used before. What they are is second and third order respectively. I didn't understand them at first either, but second order just means it tracks the slope at a squared rate. And third order means it tracks the slope at a cubed rate.

• Posts: 12,030
edited 2021-09-15 07:25

Yes that all applies to the Prop2. The sigma-delta ADC is in the custom laid pad-ring with the low-level I/O circuitry. Whereas the sinc filters are located in the smartpins and streamers, which are part of the synthesised HDL in the Prop2's central region. The pin's IN signal is used to transport the bitstream from the ADC to the smartpin.

Here's a drawing I did a while back - https://forums.parallax.com/discussion/comment/1484327/#Comment_1484327

Chip posted the real schematic of the ADC here - https://forums.parallax.com/discussion/comment/1510744/#Comment_1510744

• Posts: 559

Thank you very much, evanh!
This is very helpful, as it answers the question how the accumulators work and also about input impedance.
https://forums.parallax.com/discussion/comment/1484327/#Comment_1484327
This information is needed in the P2 docu!