mppt/App/src/collect_Conversion.c

296 lines
8.2 KiB
C

/*
* collect_Conversion.c
*
* Created on: 2024年6月21日
* Author: psx
*/
#include "collect_Conversion.h"
#include "gpio.h"
#include "adc.h"
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#define CHG_CURR ADC_Channel_1
#define PV_VOLT_OUT ADC_Channel_2
#define DSG_CURR ADC_Channel_3
#define PV1_VOLT_IN ADC_Channel_4
#define PV_VOLT_IN1 ADC_Channel_5
#define MOSFET_Temper ADC_Channel_6
#define PV2_VOLT_IN ADC_Channel_7
//#define enable_Printf_VI
/* 光伏充电输出电流比例,放大倍数*电阻 */
const float P_CHG_CURR = (1.0 / (50 * 0.005));
/* 光伏充电输出电压比例,分压系数(放电时采集不准) */
const float P_PV_VOLT_OUT = (47.0 + 10.0) / 10.0;
/* 放电电流采集电流倍数 */
const float P_DSG_CURR = (1.0 / (50 * 0.005));
///* 光伏1开路输出电压比例 */
const float P_PV1_VOLT_IN = (100 + 4.7) / 4.7;
/* 光伏1开路输出电压比例 */
//const float P_PV1_VOLT_IN = (47 + 4.7) / 4.7;
/* 系统电源电压比例 */
const float P_PV_VOLT_IN1 = (47 + 4.7) / 4.7;
/* 温度采集比例 */
//const float P_MOSFET_TEMPER = 0;
/* 光伏2开路输出电压比例 */
const float P_PV2_VOLT_IN = (47 + 4.7) / 4.7;
/* 温度的adc值的个数 */
#define mosTemperADCLen 241
/* 温度 */
const uint16_t mosTemperADC[mosTemperADCLen] = {
3707, 3697, 3687, 3676, 3665, 3654, 3643, 3632, 3620, 3608, /* -20 - -15.5 ℃ */
3596, 3584, 3571, 3559, 3546, 3533, 3519, 3506, 3492, 3478, /* -15 - -10.5 ℃ */
3464, 3449, 3434, 3419, 3404, 3389, 3373, 3358, 3341, 3325, /* -10 - -5.5 ℃ */
3309, 3292, 3275, 3258, 3241, 3223, 3205, 3187, 3169, 3151, /* -5 - -0.5 ℃ */
3132, 3114, 3095, 3075, 3056, 3037, 3017, 2997, 2977, 2957, /* 0 - 4.5 ℃ */
2936, 2916, 2895, 2874, 2854, 2832, 2811, 2790, 2768, 2747, /* 9 - 9.5 ℃ */
2725, 2703, 2681, 2659, 2637, 2615, 2592, 2570, 2548, 2525, /* 10 - 14.5 ℃ */
2503, 2480, 2457, 2435, 2412, 2389, 2366, 2343, 2321, 2298, /* 15 - 19.5 ℃ */
2275, 2252, 2229, 2207, 2184, 2161, 2138, 2116, 2093, 2071, /* 20 - 24.5 ℃ */
2048, 2026, 2003, 1981, 1959, 1937, 1914, 1893, 1871, 1849, /* 25 - 29.5 ℃ */
1827, 1806, 1784, 1763, 1742, 1721, 1700, 1679, 1658, 1638, /* 30 - 34.5 ℃ */
1617, 1597, 1577, 1557, 1537, 1518, 1498, 1479, 1460, 1441, /* 35 - 39.5 ℃ */
1422, 1403, 1385, 1366, 1348, 1330, 1312, 1295, 1277, 1260, /* 40 - 44.5 ℃ */
1243, 1226, 1209, 1192, 1176, 1160, 1144, 1128, 1112, 1097, /* 45 - 49.5 ℃ */
1081, 1066, 1051, 1036, 1022, 1007, 993, 979, 965, 951, /* 50 - 54.5 ℃ */
938, 924, 911, 898, 885, 872, 860, 848, 835, 823, /* 55 - 59.5 ℃ */
811, 800, 788, 777, 765, 754, 743, 732, 722, 711, /* 60 - 64.5 ℃ */
701, 691, 681, 671, 661, 651, 642, 632, 623, 614, /* 65 - 69.5 ℃ */
605, 596, 588, 579, 571, 562, 554, 546, 538, 530, /* 70 - 74.5 ℃ */
522, 515, 507, 500, 493, 486, 478, 471, 465, 458, /* 75 - 79.5 ℃ */
451, 445, 438, 432, 426, 420, 414, 408, 402, 396, /* 80 - 84.5 ℃ */
390, 385, 379, 374, 368, 363, 358, 353, 348, 343, /* 85 - 89.5 ℃ */
338, 333, 328, 324, 319, 315, 310, 306, 301, 297, /* 90 - 94.5 ℃ */
293, 289, 285, 281, 277, 273, 269, 266, 262, 258, /* 95 - 99.5 ℃ */
255 /* 100 ℃ */
};
static uint16_t get_adc(uint8_t ADC_Channel);
/**
* @brief 将采集的adc进行校准
* @param val 采集的adc数据
* @retval 校准后的adc数据
*/
uint16_t Get_ConversionVal(int16_t val)
{
if((val + Calibrattion_Val) < 0 || val == 0)
return 0;
if((Calibrattion_Val + val) > 4095 || val == 4095)
return 4095;
return (val + Calibrattion_Val);
}
/**
* @brief 得到adc采集的数据
* @param ADC_Channel adc通道
* @retval val adc采集的数据
*/
uint16_t get_adc(uint8_t ADC_Channel)
{
uint16_t val;
ADC_RegularChannelConfig(ADC1, ADC_Channel, 1, ADC_SampleTime_CyclesMode5);
// ADC_RegularChannelConfig(ADC1, ADC_Channel, 1, ADC_SampleTime_CyclesMode7);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
val = ADC_GetConversionValue(ADC1);
return val;
}
/**
* @brief 得到充电电流
* @param
* @retval I 电流值
*/
float get_CHG_CURR(void)
{
float I;
uint16_t I_ADC;
I_ADC = Get_ConversionVal(get_adc(CHG_CURR));
// I_ADC = get_adc(CHG_CURR);
I = (float)(I_ADC) / 4095 * 3.3 * P_CHG_CURR * 2;
// printf(" CHG_CURR I : %d /10000 \n", (int)(I * 10000));
#ifdef enable_Printf_VI
printf("\n CHG_CURR ADC : %d \n", I_ADC);
printf(" CHG_CURR I : %d /10000 \n", (int)(I * 10000));
#endif
return I;
}
/**
* @brief 得到光伏输出电压
* @param
* @retval V 电压值
*/
float get_PV_VOLT_OUT(void)
{
float V;
uint16_t V_ADC;
V_ADC = Get_ConversionVal(get_adc(PV_VOLT_OUT));
// V_ADC = get_adc(PV_VOLT_OUT);
V = (float)(V_ADC) / 4095 * 3.3 * P_PV_VOLT_OUT;
#ifdef enable_Printf_VI
printf("\n PV_VOLT_OUT ADC : %d \n", V_ADC);
printf(" PV_VOLT_OUT V : %d /100 \n", (int)(V * 100));
#endif
return V;
}
/**
* @brief 得到放电电流
* @param
* @retval I 电流值
*/
float get_DSG_CURR(void)
{
float I;
uint16_t I_ADC;
I_ADC = Get_ConversionVal(get_adc(DSG_CURR));
// I_ADC = get_adc(DSG_CURR);
I = (float)(I_ADC) / 4095 * 3.3 * P_DSG_CURR * 2;
#ifdef enable_Printf_VI
printf("\n DSG_CURR ADC : %d \n", I_ADC);
printf(" DSG_CURR I : %d /100 \n", (int)(I * 100));
#endif
return I;
}
/**
* @brief 得到光伏1开路电压
* @param
* @retval V 电压值
*/
float get_PV1_VOLT_IN(void)
{
float V;
uint16_t V_ADC;
// GPIO_WriteBit(G_FFMOS_CON1_GPIO, G_FFMOS_CON1_PIN, SET);
V_ADC = Get_ConversionVal(get_adc(PV1_VOLT_IN));
// V_ADC = get_adc(PV1_VOLT_IN);
// GPIO_WriteBit(G_FFMOS_CON1_GPIO, G_FFMOS_CON1_PIN, RESET);
V = (float)(V_ADC) / 4095 * 3.3 * P_PV1_VOLT_IN;
#ifdef enable_Printf_VI
printf("\n PV1_VOLT_IN ADC : %d \n", V_ADC);
printf(" PV1_VOLT_IN V1 : %d /100 \n", (int)(V * 100));
#endif
return V;
}
/**
* @brief 得到系统电压电压
* @param
* @retval V 电压值
*/
float get_PV_VOLT_IN1(void)
{
float V;
uint16_t V_ADC;
V_ADC = Get_ConversionVal(get_adc(PV_VOLT_IN1));
// V_ADC = get_adc(PV_VOLT_IN1);
V = (float)(V_ADC) / 4095 * 3.3 * P_PV_VOLT_IN1;
#ifdef enable_Printf_VI
printf("\n PV_VOLT_IN1 ADC : %d \n", V_ADC);
printf(" PV_VOLT_IN1 V : %d /100 \n", (int)(V * 100));
#endif
return V;
}
//const float Rp = 10000.0; //10K
//const float T2 = (273.15+25.0);//T2
//const float Bx = 3950.0;//B
////const float Bx = 3435.0;//B
//const float Ka = 273.15;
/**
* @brief 得到温度
* @param
* @retval V 电压值
*/
float get_MOSFET_Temper(void)
{
float T = 0;
uint16_t T_ADC;
T_ADC = Get_ConversionVal(get_adc(MOSFET_Temper));
for (int i = 0; i < mosTemperADCLen; ++i) {
if (T_ADC >= mosTemperADC[i]) {
T = -20 + i * 0.5;
break;
}
}
// T = (float)(T_ADC) / 4095 * 3.3 * P_MOSFET_TEMPER;
#ifdef enable_Printf_VI
printf("\n MOSFET_Temper ADC : %d \n", T_ADC);
printf(" MOSFET_Temper T : %d /10 \n", (int)(T * 10));
#endif
// const uint16_t arr[] = {1, 2, 4, 5, 6, 8, 9};
// int size = sizeof(arr) / sizeof(arr[0]);
// int x = 3;
// int index = findNearest(arr, size, x);
// printf("The nearest element to %d is %d at index %d\n", x, arr[index], index);
return T;
}
/**
* @brief 得到光伏2开路电压
* @param
* @retval V 电压值
*/
float get_PV2_VOLT_IN(void)
{
float V;
uint16_t V_ADC;
// GPIO_WriteBit(G_FFMOS_CON2_GPIO, G_FFMOS_CON2_PIN, SET);
V_ADC = Get_ConversionVal(get_adc(PV2_VOLT_IN));
// V_ADC = get_adc(PV2_VOLT_IN);
// GPIO_WriteBit(G_FFMOS_CON2_GPIO, G_FFMOS_CON2_PIN, RESET);
V = (float)(V_ADC) / 4095 * 3.3 * P_PV2_VOLT_IN;
#ifdef enable_Printf_VI
printf("\n PV2_VOLT_IN ADC : %d \n", V_ADC);
printf(" PV2_VOLT_IN V : %d /100 \n", (int)(V * 100));
#endif
return V;
}