Calculate 2's Complement | Arduino Compatible Compiler for LabVIEW Discussions | Forum

Avatar
Please consider registering
guest
sp_LogInOut Log In sp_Registration Register
Register | Lost password?
Advanced Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Feed Topic RSS sp_TopicIcon
Calculate 2's Complement
July 14, 2018
11:59 am
Avatar
Chrisreed
Member
Members
Forum Posts: 19
Member Since:
March 23, 2015
sp_UserOfflineSmall Offline

When I read the data registers in the ADXL345 3-axis accelerometer they come across as a U16 integer, but they are stored 2's Complement format.  There are a couple of ways of converting the 16-bit code into 2's complement (see example below) in standard LabVIEW, but some of the VI's are not available in the PWS Library.

eg. I read a 16-bit code of 0xFFF2 (65,522 dec) from the register, but it should be converted to the 2's complement of -14(dec) - is it possible to do this within the PWS Library?

2s-Complment-Format-2.JPGImage EnlargerCalculate-2s-Complement-Number.JPGImage EnlargerStandard LabVIEW code

Chris

July 14, 2018
2:42 pm
Avatar
Chrisreed
Member
Members
Forum Posts: 19
Member Since:
March 23, 2015
sp_UserOfflineSmall Offline

I did manage to get the following code to work in standard LabVIEW and all the necessary VI's were also available in the PWS library, but the decimal output is always "0" when I try to run it on the PWS Stamp - any ideas on why this doesn't work?

Chris

2s-Complment-Format-in-PWS.JPGImage Enlarger

sp_PlupAttachments Attachments
July 16, 2018
2:37 am
Avatar
Chrisreed
Member
Members
Forum Posts: 19
Member Since:
March 23, 2015
sp_UserOfflineSmall Offline

2s-Complment-Format-in-PWS2.JPGImage EnlargerOK I just realised that the 2's Complement Input in the above example was an I16, but in my PWS Code it is coming in as a U16.

Standard LabVIEW has the "To sign Word Integer" I16 Conversion VI which does the job, but of course the PWS Library doesn't.

 

It seems to me I only have two options:

1) Create a very large lookup table and index the correct I16 value out - yuk!

2) Write the U16 to I16 conversion in Arduino code and call that in LabVIEW - I think that is possible using the PWS Library?

Chris

sp_PlupAttachments Attachments
July 16, 2018
7:25 am
Avatar
Chrisreed
Member
Members
Forum Posts: 19
Member Since:
March 23, 2015
sp_UserOfflineSmall Offline

Well I did get it working with the large lookup Table (4096 elements), but that took up 3% of my program storage space!

So instead I managed to do the 2's complement conversion in code (see attached image).

This is still a lot of extra coding - I just wish we had the I16 conversion VI 🙁

Chris2s-Complment-Format-in-PWS_Final.JPGImage Enlarger

July 16, 2018
7:06 pm
Avatar
Steffan
Admin
Forum Posts: 408
Member Since:
March 12, 2015
sp_UserOfflineSmall Offline

There are several simple ways to do it including this one.  The key is to use LabVIEW to coerce the datatype to a signed number.  Notice here that the 65536 constant is an I32 which forced the output of the subtract to be an I32.

conversion.pngImage Enlarger 

sp_PlupAttachments Attachments
July 17, 2018
1:43 am
Avatar
Chrisreed
Member
Members
Forum Posts: 19
Member Since:
March 23, 2015
sp_UserOfflineSmall Offline

Well bugger me silly - yes that is a lot simpler than my method thanks Steffan.

I was fixated on trying to convert a U16 into a I16, when I should have been thinking of an I32.

Chris

Forum Timezone: UTC 0
Most Users Ever Online: 100
Currently Online:
39
Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
miche91: 33
scottj: 28
scadao: 23
Member Stats:
Guest Posters: 5
Members: 202
Moderators: 0
Admins: 3
Forum Stats:
Groups: 1
Forums: 2
Topics: 266
Posts: 1222
Newest Members:
pujacontrol
Administrators: geadmin: 22, filipealtoe: 96, Steffan: 356