I am debugging a piece of code and I am not too familiar with fixed-point Q format and such so I'm testing out some code and seeing how the arithmetic works between it and other stuff. Long story short I reached a roadblock in the main function, inside the if-condition 'adc == 1', on 'q_format_voltaged' value, which is 15. But when that variable is used within line 'q_format_voltages', it seems to be 0 and 'q_format_voltage_offset' subtracts from it making 'q_format_voltages = -1.5' roughly. Why is 'q_format_voltaged' value 15 not being used and what can I do to fix it? Thank you in advance.
#include <stdio.h>
// Define Q format parameters
#define FIXED_POINT_FRACTIONAL_BITS 16
int float_to_Q(float x) {
return (int)(x * (1 << FIXED_POINT_FRACTIONAL_BITS)); // Multiply by 2^Q to shift the decimal point
}
float Q_to_float(int x) {
return (float)x / (1 << FIXED_POINT_FRACTIONAL_BITS); // Divide by 2^Q to get the float value
}
int fixed_point_multiply(int x, int y) {
long long result = (long long)x * y; // Perform multiplication in a wider type to avoid overflow
// Adjust the result for the Q format representation
result >>= FIXED_POINT_FRACTIONAL_BITS;
return (int)result;
}
int fixed_point_divide(int x, int y) {
long long result = (long long)x * (1 << FIXED_POINT_FRACTIONAL_BITS); // Multiply numerator by 2^Q
result /= y; // Perform division
return (int)result;
}
float original_voltage = 5; // Define voltage as a global variable
float original_LPF = 0;
float original_LPF_A = 0.002;
float original_due_volt = 3;
float original_ADC_bits = 2;
float original_voltage_offset = 1.5;
volatile int DUTYCYCLE = 10;
int adc = 1;
int mode = 1;
int main() {
int q_format_voltage = float_to_Q(original_voltage);
int q_format_LPF = float_to_Q(original_LPF);
int q_format_LPF_A = float_to_Q(original_LPF_A);
int q_format_due_volt = float_to_Q(original_due_volt);
int q_format_ADC_bits = float_to_Q(original_ADC_bits);
int q_format_voltage_offset = float_to_Q(original_voltage_offset);
int q_format_voltage_squared;
int q_format_voltagem;
int q_format_voltaged;
int q_format_voltages;
if (adc == 1) {
q_format_voltagem = fixed_point_multiply(DUTYCYCLE, q_format_due_volt);
q_format_voltaged = fixed_point_divide(q_format_voltagem, q_format_ADC_bits);
if (mode == 1) {
q_format_voltages = q_format_voltaged - q_format_voltage_offset;
q_format_voltage_squared = fixed_point_multiply(q_format_voltages, q_format_voltages); // Square voltages
}
q_format_LPF = q_format_LPF - fixed_point_multiply(q_format_LPF_A, (q_format_LPF - q_format_voltage_squared)); // LPF calculation in Q16 format
}
// Printing results
printf("Original voltage: %f\n", original_voltage);
printf("Q format voltage: %d\n", q_format_voltage);
printf("Original LPF: %f\n", original_LPF);
printf("Q format LPF: %d\n", q_format_LPF);
printf("Original LPF_A: %f\n", original_LPF_A);
printf("Q format LPF_A: %d\n", q_format_LPF_A);
printf("Original Due volt: %f\n", original_due_volt);
printf("Q format Due volt: %d\n", q_format_due_volt);
printf("Original ADC bits: %f\n", original_ADC_bits);
printf("Q format ADC bits: %d\n", q_format_ADC_bits);
printf("Original voltage offset: %f\n", original_voltage_offset);
printf("Q format voltage offset: %d\n", q_format_voltage_offset);
printf("dutycycle * due volt: %d\n", q_format_voltagem);
printf("voltagem divided by adc bits: %d\n", q_format_voltaged);
printf("voltage - voltage offset: %f\n", Q_to_float(q_format_voltages));
printf("voltages number squared: %f\n", Q_to_float(q_format_voltage_squared));
printf("LPF amount: %f\n", Q_to_float(q_format_LPF));
return 0;
}