Author Topic: MSP430 and ADC  (Read 16994 times)

0 Members and 1 Guest are viewing this topic.

Offline miguelvp

  • Super Contributor
  • ***
  • Posts: 5550
  • Country: us
Re: MSP430 and ADC
« Reply #50 on: February 27, 2014, 06:23:29 am »
But it will still be useful for others that have processors with DSP and floating point capabilities, or at least to understand some things.

Well, at least for me, your long post there actually quite useful since I use ARM M4F 80 Mhz core (still learning though  :-[) which has a dedicated FPU and with all the whole shebang for hardware based floating point operations in it.  :P

At least the effort was not in vain :)

I guess I could follow up with how to implement double/float arithmetic in assembler (C and C++ can't really do carries directly), I also find it amazing that my ZX spectrum dedicated a big chunk of their precious ROM to handle the basic float arithmetic with different mantissas and all, I mean 512 bytes of code is just atrocious when you have only 64K of memory, it's almost  a percent of the whole memory space!

Well to be real, it's more than 512 bytes since their extra math, like  trig, exponent square root, etcetera routines took way more than just half a kilobyte, now we are talking 4K with all those trig and exponent tables and that's a whole 6.25% of resources for 16 bit address space.

Anyways, say you have a 3.3V Vref voltage that will be 11.01(0011)b and the parenthesis mean periodic, meaning the 0011 pattern can be saved and the multiplication of that can be stored and just shifted 4 bits right and added to get the whole thing.

Since I don't want to confuse the matter too much more I'll just go with 5V Vref since it will be easier 5d=101b meaning copy the mantissa shift left 2 bits and add the original mantissa. the result? your double or float in Volts, but that's just too much work to do to get an accurate reading I guess.

I could go on detail how to perform fast floating point math with assembler since before the 8087 no one use floats then I guess it's not worth it  :-//

But a hint for all, if you work with normalized vector components (0.0 to 1.0) math gets really easy for multiplication, adds and subtracts; divisions a bit more cumbersome but not that hard either. Math co-processors are indeed optimized for doing general math but I'm not talking about getting e^pi-pi = 20.00000000 scenario in here and yeah our dear 8087 gets 19.999099979189475767266442984669 as a result as well.


Edit: the e^pi-pi is really a joke and a well known mathematical coincidence. Do not try to solve it or prove it.
« Last Edit: February 27, 2014, 07:43:48 am by miguelvp »
 

Offline paf

  • Regular Contributor
  • *
  • Posts: 91
Re: MSP430 and ADC
« Reply #51 on: February 27, 2014, 10:51:08 am »


In a computer one can make calculations using integers, floating point or fixed point.

What MiguelVP is calling "normalized vector components" and "floating point tricks", the rest of the world knows as "fixed point".
Using the right words is the first step in good engineering.

And having a constant with an infinite binary representation like the 3.3 is also a bad idea.
If you have an integer and you want to multiply it by 3.3, just multiply by 33 (32+1) and then divide by 10.

If you really want/need Floating Point it is not enough to implement the basic operations, you need to uderstand what kind of errors can happen, and what to do when those errors happen.   

Some references:

What Every Computer Scientist Should Know About Floating-Point Arithmetic:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Simplified Version
http://floating-point-gui.de

Handbook of Floating-Point Arithmetic
http://www.springer.com/birkhauser/mathematics/book/978-0-8176-4704-9

Implementation of Mathematical functions (sometimes in fixed point):

Computer Approximations, John Hart
1968  John Wiley & Sons

Software Manual for the Elementary Functions, William Cody & William Waite
1980  Prentice-Hall

Math Toolkit for Realtime Programming, Jack Crenshaw
http://www.amazon.com/Math-Toolkit-Real-Time-Programming-Crenshaw/dp/1929629095

Modern Computer Arithmetic, Brent & Zimmermann, Cambridge University Press
http://www.loria.fr/~zimmerma/mca/pub226.html

Elementary Functions:   Muller, Jean-Michel
http://www.springer.com/birkhauser/computer+science/book/978-0-8176-4372-0


And if you are doing embedded work, you should  have this book:
http://www.hackersdelight.org

 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: MSP430 and ADC
« Reply #52 on: February 27, 2014, 12:26:42 pm »
Quote
Apologies to all,

No apologies needed. But more context for your approach would be helpful to your readers.
================================
https://dannyelectronics.wordpress.com/
 

Offline miguelvp

  • Super Contributor
  • ***
  • Posts: 5550
  • Country: us
Re: MSP430 and ADC
« Reply #53 on: February 27, 2014, 05:20:24 pm »



What MiguelVP is calling "normalized vector components" and "floating point tricks", the rest of the world knows as "fixed point".
Using the right words is the first step in good engineering.

And having a constant with an infinite binary representation like the 3.3 is also a bad idea.
If you have an integer and you want to multiply it by 3.3, just multiply by 33 (32+1) and then divide by 10.

...


Nice reference links thanks.

by "normalized vector components"  I mean just that, not fix point at all.
A unified vector of N dimensions  also called "normalized" vector has a length of 1 unit.

These are used on DSP and 3D graphics all around, you can make matrices with them and all kinds of real math with them (not floating point tricks at all, just pure unadulterated math).

fix point is a different beast since by nature the decimal point can be at any bit index. I guess a normalized value of 0-1 could be expressed in a fix point where the decimal point is past the most significant bit but that would be a special case.

"Using the right words is the first step in good engineering. "

You betcha.


Dividing by 10 (2*5) is not a good approach since 1/5 can't be represented in binary very well. But summations will help.

Anyways, I don't get the "math is hard" thing I guess I never had a toy telling me that.

We are talking basic algebra here nothing really complicated

Edit:

Let me hint you on something else that might be too obvious but no one actually thinks about this often.
power is intensity  times volts and of course intensity is volts by resistance.

So you end up with p=V^2/R

So 5 volts square is pretty high, so it's 3.3 v squared, but what about 1.0V? or under 1.0V?
« Last Edit: February 27, 2014, 05:55:22 pm by miguelvp »
 

Offline miguelvp

  • Super Contributor
  • ***
  • Posts: 5550
  • Country: us
Re: MSP430 and ADC
« Reply #54 on: February 27, 2014, 07:16:32 pm »
One more thing, on base 10 (decimal) when you multiply two numbers together you apply a multiplication table [10 by 10] and then shift the result and add it.

On base 2, your multiplication table is very simple
0*0 = 0
0*1 = 0
1*0 = 0
1*1 = 1

So look that as a truth table, looks like an AND gate to me. so you keep the full value when the multiplicand is 1 or skip it when 0
shift left then add.

It's the same thing but computers can do it very fast sequentially

FPGAs can do this in parallel without using a lot of fabric.

Why does NVidia use FPGAs to develop their GPUs?  what is really a shader for graphics?
it's just vector processing but with custom data paths and actually not very efficient since you have to denormalize the value, do the operation then normalize the result and repeat.

But you can do your own algorithms that don't need to denormalize if it's already on the same exponent and you keep it that way. The same is true for the whole range, you can keep it denormal in memory and keep computing until you are done, then normalize the result.

So we have ands, adds, shifts, what else we need, well memory that is convenient to use.
How about the stack? you got it, push and pops.

Edit: don't forget the flags, Carry Zero Overflow etc.. for multi byte operations

3d animation is just that too, push pops of transform matrices and vector operations.

Computer math also uses reverse polish notation, by organizing the values and the operands in the stack, so you don't need parenthesis. I mean, this is pretty basic and you shouldn't rely on a C compiler to do things for you to begin with if you use micro controllers that are too tiny.

One more edit:

Because I realized that even doing the math is not even really that important.

I don't think, and correct me if I'm wrong that you'll have to link with the floating point library to print a number. Having it in memory in the format that the printf function can handle will give you a readable value.
-0.125 to (0-1LSB) if you are under Vref on the lower EXT range
0 to 0.99999999(9) when you are within range (with special case at 0 that the EXT range bit can flip within 0 having really two zeros.
1.0 to 1.125 when you are above range.

if you get a reading under or higher than those values then you know you are outside the parameters of the ADC.
« Last Edit: February 28, 2014, 03:40:46 pm by miguelvp »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf