How Can I Use Fixed-Point Numbers With The C API For FPGA?

Updated Apr 18, 2018

Reported In


  • LabVIEW FPGA Module
  • LabWindows/CVI

Issue Details

I have a LabVIEW FPGA VI that I have developed which uses fixed point numbers for calculations and for controls, indicators and/or DMA FIFOs. When I create my C interface, the fixed-point inputs and outputs do not work. Why is this? What can I do about it?



The new C Interface API for LabVIEW FPGA does not support fixed-point numbers, clusters, or arrays of arrays when reading from and writing to controls, indicators, and FIFOs. In order to pass data in the form of fixed-point numbers, you must convert the data to integers.

The best approach to accomplish this is to first typecast the fixed-point data to an integer wide enough to represent the data in the FPGA. This integer is then read in the C API for FPGA, and typecasted back to a fixed-point number by creating a function in C. This function would take the data, as well as the fixed-point encoding (signed vs. unsigned, word length, and integer word length) as inputs, and return the correct number in fixed-point datatype.

In order to typecast the fixed-point data to an integer in the FPGA, you can use the fixed-point to integer cast function found in the functions palette under Programming»Numeric»Fixed-point. You can see how the code is implemented in the screenshot below.
As an additional reference below is an image of how the integer number can be converted back to a fixed-point number. The code in the image is implemented in LabVIEW using Integer to Fixed-point cast the for illustration purposes, but a similar function can be implemented in C to accomplish the same task. Notice how the inputs to the Cast function are both the integer data as well as the encoding of the fixed-point data that it is casting to.
A second approach to using integers, is to use integers throughout the entirety of the FPGA code. For CompactRIO FPGA VIs, you can set each of your C-Series modules to operate with raw data rather than fixed-point. This will reduce the need to use fixed point data inside of the FPGA VI. For R-Series devices, analog IO will only operate in the RAW mode.

In order to convert RAW integer values into usable measurements, consult the specific module documentation inside the LabVIEW help to obtain the least significant bit (LSB) weight and offset values as well as the conversion equation which will use these constants to produce the required RAW value for your FPGA, or to convert the RAW value from your FPGA. Note that fractional precision may be required to represent this measurement value. You will need to create an appropriate structure in C to represent this fractional component.

You can also export the required LSB weight and offset from your IO Channel by using a property node in your FPGA block diagram. You can then transfer these values to your C interface using front panel indicators. Note that you only need to read these measurements once per execution of your application
Consult the FPGA Interface C API Help for details on the C API. When NI RIO is installed, you can find this help document by navigating to the Start Menu»Programs»National Instruments»NI-RIO.


Not Helpful