08-27-2015 05:36 AM
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
Solved! Go to Solution.
08-27-2015 12:34 PM
I believe there's an error in your math: FE9 should be -23, not -16. I would do it as shown in this snippet:
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.
08-28-2015 05:54 AM
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
08-28-2015 01:14 PM
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.
08-28-2015 01:25 PM
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
08-31-2015 02:56 AM
Thank you very much Nathand, it's clear for me now !