// 输出范围定义 #define RANGE_UNIPOLAR_5V 0x00 // 0 to 5V #define RANGE_UNIPOLAR_10V 0x01 // 0 to 10V #define RANGE_BIPOLAR_5V 0x02 // ±5V #define RANGE_BIPOLAR_10V 0x03 // ±10V #define RANGE_CURRENT_4_20 0x04 // 4-20mA // 电压到数字码转换(双极性±10V模式) uint16_t voltage_to_code_bipolar(float voltage) { // ±10V对应0-65535数字码 uint16_t code = (uint16_t)((voltage + 10.0) / 20.0 * 65535); return code; } // 电流到数字码转换(4-20mA模式) uint16_t current_to_code_4_20ma(float current_ma) { // 4-20mA对应0-65535数字码 uint16_t code = (uint16_t)((current_ma - 4.0) / 16.0 * 65535); return code; }