Esempio n. 1
0
/**
 * Sets options for the encoder configuration register.
 *
 * @param[in]  channel  A struct containing the registers on the encoder channel
 *                      to modify.
 * @param[in]  mask     Array of flags that indicate which of the configure
 *                      settings are valid.
 * @param[in]  settings Array of flags that indicate the configuration settings.
 */
void Encoder_Configure(MyRio_Encoder* channel, Encoder_ConfigureMask mask,
                       Encoder_ConfigureSettings settings)
{
    NiFpga_Status status;
    uint8_t cnfgValue;

    /*
     * Get the current value of the configure register.
     *
     * The returned NiFpga_Status value is stored for error checking.
     */
    status = NiFpga_ReadU8(myrio_session, channel->cnfg, &cnfgValue);

    /*
     * Check if there was an error reading from the encoder registers.
     *
     * If there was an error then the rest of the function cannot complete
     * correctly so print an error message to stdout and return from the
     * function early.
     */
    MyRio_ReturnIfNotSuccess(status,
        "Could not read from the encoder configure registers!")

    /*
     * Clear the value of the masked bits in the configure register. This is
     * done so that the correct value can be set later on.
     */
    cnfgValue = cnfgValue & (~mask);

    /*
     * Set the value of the settings in the configure register. If the
     * value to set is 0 this operation would not work unless the bit was
     * previously cleared.
     */
    cnfgValue = cnfgValue | settings;


    /*
     * Write the new value of the configure register to the device.
     */
    NiFpga_MergeStatus(&status,
            NiFpga_WriteU8(myrio_session, channel->cnfg, cnfgValue));

    /*
     * Check if there was an error writing to encoder configure registers.
     *
     * If there was an error then print an error message to stdout.
     */
    MyRio_ReturnIfNotSuccess(status,
            "Could not write to the encoder configure registers!")
}
Esempio n. 2
0
/**
 * Write a voltage value to a single channel. The channel structure must
 * previously been initialized with the appropriate scale factors with
 * AnalogScaling.
 *
 * @param[in]  channel  A struct containing the registers for the AIO
 *                      channel to be written to
 * @param[in]  value    the voltage value in volts
 */
void Aio_Write(MyRio_Aio* channel, double value)
{
    NiFpga_Status status;
    uint16_t valueScaled;

    /*
     * The value is always stored in an unsigned 16-bit register. For a signed
     * channel, cast this value directly to a signed 16-bit value. Bound the
     * values to their respective limits.
     */
    if (channel->is_signed)
    {
        /*
         * Scale the voltage value to the raw value.
         */
        value = (value - channel->scale_offset) / channel->scale_weight;
        value = (value < INT16_MIN) ? INT16_MIN : value;
        value = (value > INT16_MAX) ? INT16_MAX : value;

        /*
         * Round the scaled value to the nearest integer.
         */
        value += (value < 0.0) ? -0.5 : 0.5;

        /*
         * Convert the scaled value to an unsigned integer.
         */
        valueScaled = (uint16_t)((int16_t)(value));
    }
    else
    {
        value = (value - channel->scale_offset) / channel->scale_weight + 0.5;
        value = (value < 0) ? 0 : value;
        value = (value > UINT16_MAX) ? UINT16_MAX : value;
        valueScaled = (uint16_t) value;
    }

    /*
     * Write the value to the value register.
     *
     * The returned NiFpga_Status value is stored for error checking.
     */
    status = NiFpga_WriteU16(myrio_session, channel->val, valueScaled);

    /*
     * Check if there was an error writing to the write register.
     *
     * If there was an error then print an error message to stdout and return.
     */
    MyRio_ReturnIfNotSuccess(status,
            "Could not write to the AO value registers!");

    /*
     * Write the signal so that the FPGA will apply the new value.
     *
     * The returned NiFpga_Status value is stored for error checking.
     */
    status = NiFpga_WriteU16(myrio_session, channel->set, 1);

    /*
     * Check if there was an error writing to the set register.
     *
     * If there was an error then print an error message to stdout.
     */
    MyRio_ReturnIfNotSuccess(status, "Could not write to the AO set registers!")
}