LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I convert bits into a word

I have a motor with a quadrature encoder. I lack the proper DAQ to read both channels (Currently have the NI-USB-6009) so I was going to try to go about it a different way to acquire the resolution I am looking for. I was going to go from the quadrature encoder to an external decoder/counter (HCTL-2016). This will only supply me the MSB and the LSB at one time. I am also going to use a PIC to interface into both the decoder and the DAQ, so both know when to send and accept signals. So therefore I have a total of 16 bits going into LabVIEW, in two seperate bytes. I am new to Labview and was wondering how I could go about taking these bits and putting them in a "register" so I could make a word and convert to a decimal to receive the motor RPM and response. This register would then have to be cleared for the next word to be stored. Please let me know if there is an easier way to acquire the motor response without purchasing a new DAQ or if you think that I am approaching this wrong.

 

Thanks,

Nicholas

0 Kudos
Message 1 of 17
(5,452 Views)

If you have a 2 byte string, you can use the typecast function to convert that to a number.  By putting a constant that is either U16 or I16 representation into the top of it, you'll get either an unsigned or signed 16-bit integer.

 

You'll have to pay attention to endianism (is MSB or LSB first).  If you need to, you could use swap bytes on the numeric value to correct the endianism.

0 Kudos
Message 2 of 17
(5,449 Views)

So right now, I have a DAQ Assistant running to the typecast function, with a constant of 16 going into the top. I am then using a split signal function so I can pull each bit from the word I created and multiplying each one by 2^n to where each one is then added together to give me my decimal equivalent for the motor RPM. Does this seem like the right approach? Thanks for the help.

 

0 Kudos
Message 3 of 17
(5,393 Views)

You do not need to muck with all the bits like that.

 

Take a look at the attached vi so you can figure out how this works. I convert an array of 16 bits to a number and then type cast to a string. The string is what I think you are dealing with. I then type cast that to a 16 bit unsigned number.

 

Edit: In the attached vi I forgot to do something. On the string indicator, right click and select "Hex Display" It is set for "\ Codes".

=====================
LabVIEW 2012


Message 4 of 17
(5,375 Views)

Wow, that is a lot better looking that my "muck." I will approach it this way. Thanks a lot for the help.

 

 

0 Kudos
Message 5 of 17
(5,357 Views)

Okay, so the supplied example makes sense, but I am struggling with having two bytes come in at separate times and putting them together to make a 16 bit string. Since I can only bring in 1 byte at a time, I need to grab the most significant byte and the least significant byte and put them together. Is there an array that I could put them into that will reset after the second byte is arrived and the program has run? 

0 Kudos
Message 6 of 17
(5,307 Views)

Hi archimedes,

 

It sounds like you need to bite the bullet and store the data somewhere. If it is in a loop, you could use shift registers and then a case structure to determine when you want to reset the values. If you feel up to it, you can use a functional global to store the data. Once you have both bits in the same place, it should be trivial to use build and reverse to get the data into a format that the typecast can use.

 

Thanks,

0 Kudos
Message 7 of 17
(5,267 Views)

Alright... So I feel that I am very close in getting my program to work, I am just working through some bugs that I can't seem to figure out. 

 

So I am using the PIC microcontroller to tell the external decoder/counter to latch (sends a pin low) and then tell it to release the MSByte (sens pin low) and then release the LSByte (send pin high). The latching pin is then sent high to let the decoder/counter unlatch. I am using LabVIEW to watch these two pins to know when it should grab the MSB and LSB and put them into an array to where it can convert it to a decimal to read the RPM of the motor. I attached my .vi file and was hoping if somebody could take a look at it and let me know if you seen a part of the program in which I do not fully understand the logic. Please let me know if you have any questions on what I am trying to do as well.

 

Thanks. 

0 Kudos
Message 8 of 17
(5,229 Views)

Hi Archimedes,

 

First off, you seem to be converting your boolean data into dynamic data. I cannot figure out why you are using dynamic data *anywhere* in your code. Build table seems to be converting booleans into numerics, for another. In your true/false case structure, you seem to be taking an empty array and a single value and building them together--why not just use the same tunnel for both data points? Also, you should put some sort of timer in your while loops, in order to prevent your code from killing your processor. Look in programming>timing and use the wait until next ms multiple function. Take a look at this example for an explanation of the function.

 

As to the logic, I am not 100% sure on where each signal is coming from. I thought LabVIEW was sending a command to the controller to get the next set of data, but you don't seem to be outputting data.  Is your controller constantly switching from LSB to MSB to LSB to MSB without any input from you? If so, it could easily be moving too fast for your computer (in which case you shouldnt put a wait in). Either way, your insert into array function would seem to be in the wrong location. That needs to be inside of your inner while loop, and you should use replace array subset if I am not mistaken about your goal. That is, you start with an empty array. Then, your code waits for the microcontroller to be high on input A, then you read in some data and replace the first or last set of data in your array with your new LSB or MSB. Then it waits for the pin to go low and replaces the other subset of data. Then when line 1/1/0 goes high your loop quits and sends the data along.

 

I hope that makes sense. If it doesn't, ask questions and try to be clearer about what is supposed to happen.

 

Thanks,

D Smith

0 Kudos
Message 9 of 17
(5,182 Views)

I just have one question about building my array from my MSB and LSB. If I use the same tunnel how do I go about building the array to make them stack on top of eachother to give me a 16 bit array? If I use the same tunnel from my true/false case structure it requires me to go into both the array and subarray inputs on the replace array subset. Thanks for the help and the above information helped out a lot.

 

Thanks.

0 Kudos
Message 10 of 17
(5,153 Views)