set_output_voltage function implemented. output to stdout.

This commit is contained in:
Jan Bader
2021-04-14 10:19:08 +02:00
parent 08cb46a2e8
commit feab9236d1
6 changed files with 86 additions and 9 deletions

BIN
lib/.eseries.c.kate-swp Normal file

Binary file not shown.

View File

@@ -2,11 +2,11 @@
#include"eseries.h"
ESeriesMaxValue get_E_MAX(ESeries E_value) {
ESeriesMaxValue get_E_MAX(ESeries E_series) {
ESeriesMaxValue output;
switch(E_value) {
switch(E_series) {
case E3:
output = E3_MAX;
break;
@@ -34,5 +34,8 @@ ESeriesMaxValue get_E_MAX(ESeries E_value) {
}
return output;
}
double round_to_E_series (double continuous_value, ESeries E_series) {
}

View File

@@ -21,6 +21,8 @@ double *E_values[7] = {E3_values, E6_values, E12_values, E24_values, E48_values,
typedef enum {E3, E6, E12, E24, E48, E96, E192} ESeries;
typedef enum {E_NIL=0, E3_MAX=3, E6_MAX=6, E12_MAX=12, E24_MAX=24, E48_MAX=48, E96_MAX=96, E192_MAX=192} ESeriesMaxValue;
extern ESeriesMaxValue get_E_MAX(ESeries E_value);
extern ESeriesMaxValue get_E_MAX(ESeries E_series);
extern double round_to_E_series (double continuous_value, ESeries E_series);
#endif /*ESERIES_H*/

View File

@@ -2,27 +2,93 @@
#include"max_functions.h"
void set_output_voltage(double setpoint_voltage, ESeries E_resistors) {
int set_output_voltage(double setpoint_voltage, ESeries E_resistors) {
bool solution_found = false;
int decade_max = 6;
int decade_min = 1;
double R_high_resistance = 0.0;
double R_low_resistance = 0.0;
//start at 10k for low resistor and at 1R for high resistor
int R_high_decade = 0;
//start at 10k for low resistor and at minimum for high resistor
int R_high_decade = decade_min;
int R_low_decade = 4;
double R_high_value = 1.00;
double R_low_value = 1.00;
// voltage computed from resistor configuration used to compare new result to previous optimum
double output_voltage = 0.0;
double output_voltage_w = 0.0;
// set the max array index for given e series
ESeriesMaxValue max_value = get_E_MAX(E_resistors);
unsigned int e_max_values = get_E_MAX(E_resistors);
if (e_max_values == E_NIL) {
printf("max668_calc:max_functions:set_output_voltage: Unknown E series.\n");
return EXIT_FAILURE;
}
// working variables
int R_high_decade_w = R_high_decade;
int R_low_decade_w = R_low_decade;
double R_high_value_w = R_high_value;
double R_low_value_w = R_low_value;
/*
* start at minimum given Rlow from datasheet and select optimal Rhigh for given Rlow and Vout.
* do this for all possible Rlows and choose the optimal combination
* (minimize |setpoint_voltage - output_voltage|)
*/
for () {
for(int i_low = R_low_decade; i_low <= 6; i_low++) {
R_low_decade_w = i_low;
for (int j_low = 0; j_low < e_max_values; j_low++) {
// in the 6th decate only examine R_low = 1M, then stop
if ( i_low == 6 && j_low > 0 )
break;
// select new R_low
R_low_value_w = E_values[E_resistors][j_low];
R_low_resistance = R_low_value_w * exp(log(10)*R_low_decade_w);
// calculate corresponding R_high
R_high_resistance = R_low_resistance *((setpoint_voltage/1.25)-1);
R_high_decade_w = floor(log10(R_high_resistance));
R_high_value_w = round_to_E_series((R_high_resistance / exp(log(10)*R_high_decade_w)), E_resistors);
// calculate the acchieved outputvoltage for resistor combination from the given E series
output_voltage_w = 1.25 * ((R_high_resistance/R_low_resistance) + 1);
// if the calculated resistance is too small or too great, ignore it
if (R_high_decade_w > decade_max || R_high_decade_w < decade_min)
continue;
// if a new optimum is found -> save the values
if (fabs(output_voltage - setpoint_voltage) > fabs(output_voltage_w - setpoint_voltage)) {
R_high_decade = R_high_decade_w;
R_high_value = R_high_value_w;
R_low_decade = R_low_decade_w;
R_low_value = R_low_value_w;
output_voltage = output_voltage_w;
solution_found = true;
}
}
}
// report the found optimal resistor combination in the given E series
if (solution_found) {
printf("Found optimal combination for R_high / R_low: \n");
printf("R_low: %5.2fE%i ohms\n",R_low_resistance,R_low_decade);
printf("R_high: %4.2fE%i ohms\n",R_high_resistance,R_high_decade);
printf("Achieved output_voltage:");
printf("V_out: %4.2f",output_voltage);
}
else {
printf("max668_calc:max_functions:set_output_voltage: Unable to find valid resistance combination within given range.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@@ -2,8 +2,13 @@
#ifndef MAX_FUNCTIONS_H
#define MAX_FUNCTIONS_H
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include "eseries.h"
extern void set_output_voltage(double voltage, ESeries E_resistors);
extern int set_output_voltage(double voltage, ESeries E_resistors);
#endif /*MAX_FUNCTIONS_H*/

1
main.c
View File

@@ -1,3 +1,4 @@
/*main.c*/
#include <stdio.h>
#include <stdlib.h>