// ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
typedef struct
{
double load_current;
double R_groud;
double R_votagle;
double V_gain;
double R_total;
double V_ref;
double Gain;
double R_point;
double I_protect;
}calc_value;
calc_value Calc; //创建实例对象
void calc(void); //函数声明
void calc_f2();
unsigned char mode = 1;
int main()
{
printf("*******************************过流电压输出计算********************************\n\n");
Calc.load_current = 0.0;
Calc.R_groud = 0.0;
Calc.R_votagle = 0.0;
Calc.V_gain = 0.0;
Calc.R_total = 0.0;
Calc.V_ref = 0.0;
Calc.Gain = 0.0;
Calc.R_point = 0.0;
Calc.I_protect = 0.0;
bool end = true;
while (end) //循环计算
{
printf("请选择计算模式(1.计算偏置 2.计算比较电压): ");
scanf_s("%d", &mode);
if(mode ==1)calc_f2();
else
calc();
}
return 0;
}
void calc(void)
{
unsigned int i, num = 100;
printf("1.请输入采集电阻(mΩ): ");
scanf_s("%lf", &Calc.R_point);
printf("2.请输入保护电流(A): ");
scanf_s("%lf", &Calc.I_protect);
printf("3.请输入放大倍数(倍): ");
scanf_s("%lf", &Calc.Gain);
printf("4.请输入偏置电压(V): ");
scanf_s("%lf", &Calc.V_ref);
while (Calc.V_ref > 4.5 || Calc.V_ref <0)
{
printf("**********************************Warning!*************************************\n");
printf("************************输入偏置电压过大或格式不对,请重新输入*****************\n");
printf("4.请输入偏置电压(V): ");
scanf_s("%lf", &Calc.V_ref);
}
Calc.V_gain = ((Calc.R_point / 1000.0)*Calc.I_protect)*Calc.Gain + Calc.V_ref;
printf(" 输出比较电压为:Vcompare=%0.2fV\n\n", Calc.V_gain);
if (Calc.V_gain > 5)
{
printf("**********************************Warning!*************************************\n");
printf("************************输入放大倍数过大或者偏置电压选取过大!*****************\n");
num = 0;
}
else
{
num = 100;
Calc.load_current = 0;
}
printf("*******************************参数计算结果输出********************************\n");
if (num == 0)
printf(" 为空! \n");
for (i = 0; i<num; i++)
{
Calc.load_current = Calc.load_current + 0.01;
printf("load_current=%0.2fmA ", Calc.load_current);
Calc.R_total = Calc.R_groud + Calc.R_votagle;
Calc.R_groud = Calc.V_gain / Calc.load_current;
Calc.R_votagle = (5 - Calc.V_gain) / Calc.load_current;
printf("R_groud=%0.2fk ", Calc.R_groud);
printf("R_votagle=%0.2fk \n", Calc.R_votagle);
}
}
void calc_f2(void)
{
double R1 = 2, R2 = 0.00,Gain=0.00,Vref=0.00,Cur=0.00;//Gain===Rf
unsigned int i;
printf("请输入保护电流:");
scanf_s("%lf", &Cur);
printf("请输入放大倍数:");
scanf_s("%lf",&Gain);
for (i = 0; i < 500; i++)
{
R1 += 0.1;
R2 = (Gain*R1)/(R1-Gain);
Vref = (5 * R2) / (R1+R2);
printf("偏置电压为:Vref=%0.2fV ", Vref);
printf("R_votagle=%0.2fk ", R1);
printf("R_groud=%0.2fk ", R2);
printf("R_gain=%0.4fV\n", Vref+ ((Cur*0.01)*Gain));
}
}