LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Complément à 2 sur 12 bits/ 2's complement on 12 bits

Solved!
Go to solution

Bonjour à tous,

 

Après avoir essayé différentes méthodes trouvées dans le forum sans succès, je viens poster ma question ici à qui pourrait m'aider 🙂

 

Voici le contexte :

 

- Je reçois des trames d'un micro contrôleur (sur bus série) de ce type là : S_90FE7000903D_P

- S_ correspond au header et _P au footer

- Une fois la data extraite j'ai : 90FE pour X, 7000 pour Y et 903D pour Z (X,Y,Z axes de l'accéléromètre en question)

- La datasheet de l'accéléro montre que le traitement qui doit être fait est le suivant :

 

                      90FE -> FE9 -> puis complément à 2 pour avoir en décimal : -16

 

Aujourd'hui je reste bloqué à l'étape du complément à 2 sur 12 bits car sur 16 je sais faire mais sur 12 je n'ai plus d'idée.

 

Vous trouverez le VI ne question en copie.

 

Merci d'avance pour votre aide

Download All
0 Kudos
Message 1 of 6
(4,839 Views)
Solution
Accepted by topic author hamzdiou

I believe there's an error in your math: FE9 should be -23, not -16. I would do it as shown in this snippet:

12bit 2s complement.png

Don't worry about removing the unnecessary byte; it's the low-order byte and will get shifted away in the x2^n node. What this does is gets 4 characters (16 bits), converts from text to a number, swaps the bytes, then shifts right by 4 bits while preserving the sign.

 

0 Kudos
Message 2 of 6
(4,796 Views)

Thank you nathand !!!

 

Your code works well but can you detail me a little more how does it work ?
I find quite hard to understand from hexa to decimal directly, but when I try to understand how does it work in binary, I fail ...

 

You have 90FE so in binary : 10010000 11111110 so after the swaping : 11111110 10010000 and then with the shifting 11111110 1001 (so that's great) but when the 2's complement has been done ?

 

Thanks for your help

0 Kudos
Message 3 of 6
(4,777 Views)
Solution
Accepted by topic author hamzdiou

What mathematical operation do you think you need to do in order to take the 2's complement?

 

The first bit of a 2's complement value indicates the sign (although it is not enough to say it's a sign bit). To make a 12-bit value into a 16-bit value, you need to sign-extend, that is, put the first bit from the 12-bit value in all the upper bits of the 16-bit value.

 

After the byte swap, you have your desired value in the upper 12 bits of the 16-bit value; that is, you have your number, with the correct sign, multiplied by 2^4. So, to get the actual number, you need to multiply by 2^-4. This shifts the value right by 4 bits, and importantly, unlike a simple bit shift, it also preserves the sign.

0 Kudos
Message 4 of 6
(4,757 Views)

I went looking all over for that Power of 2 function -- I'd never before noticed it on the Numeric Palette ...

 

Bob (always learning something) Schor

0 Kudos
Message 5 of 6
(4,749 Views)

Thank you very much Nathand, it's clear for me now !

 

0 Kudos
Message 6 of 6
(4,718 Views)