mppt/App/src/collect_Conversion.c

296 lines
8.2 KiB
C
Raw Normal View History

2024-07-11 06:58:55 +00:00
/*
* collect_Conversion.c
*
* Created on: 2024<EFBFBD><EFBFBD>6<EFBFBD><EFBFBD>21<EFBFBD><EFBFBD>
* Author: psx
*/
#include "collect_Conversion.h"
2024-07-22 06:20:24 +00:00
#include "gpio.h"
2024-07-11 06:58:55 +00:00
#include "adc.h"
#include <math.h>
#include <stdlib.h>
#include <limits.h>
2024-07-11 06:58:55 +00:00
#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
2024-07-11 06:58:55 +00:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŵ<EFBFBD><C5B4><EFBFBD><EFBFBD><EFBFBD>*<2A><><EFBFBD><EFBFBD> */
const float P_CHG_CURR = (1.0 / (50 * 0.005));
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹϵ<D1B9><CFB5><EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD>ʱ<EFBFBD>ɼ<EFBFBD><C9BC><EFBFBD>׼<EFBFBD><D7BC> */
const float P_PV_VOLT_OUT = (47.0 + 10.0) / 10.0;
2024-07-11 06:58:55 +00:00
/* <20>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
const float P_DSG_CURR = (1.0 / (50 * 0.005));
2024-07-22 06:20:24 +00:00
///* <20><><EFBFBD><EFBFBD>1<EFBFBD><31>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD> */
const float P_PV1_VOLT_IN = (100 + 4.7) / 4.7;
2024-07-11 06:58:55 +00:00
/* <20><><EFBFBD><EFBFBD>1<EFBFBD><31>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD> */
//const float P_PV1_VOLT_IN = (47 + 4.7) / 4.7;
2024-07-11 06:58:55 +00:00
/* ϵͳ<CFB5><CDB3>Դ<EFBFBD><D4B4>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD> */
const float P_PV_VOLT_IN1 = (47 + 4.7) / 4.7;
/* <20>¶Ȳɼ<C8B2><C9BC><EFBFBD><EFBFBD><EFBFBD> */
//const float P_MOSFET_TEMPER = 0;
2024-07-11 06:58:55 +00:00
/* <20><><EFBFBD><EFBFBD>2<EFBFBD><32>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD> */
const float P_PV2_VOLT_IN = (47 + 4.7) / 4.7;
/* <20>¶ȵ<C2B6>adcֵ<63>ĸ<EFBFBD><C4B8><EFBFBD> */
#define mosTemperADCLen 241
/* <20><EFBFBD> */
const uint16_t mosTemperADC[mosTemperADCLen] = {
3707, 3697, 3687, 3676, 3665, 3654, 3643, 3632, 3620, 3608, /* -20 - -15.5 <20><> */
3596, 3584, 3571, 3559, 3546, 3533, 3519, 3506, 3492, 3478, /* -15 - -10.5 <20><> */
3464, 3449, 3434, 3419, 3404, 3389, 3373, 3358, 3341, 3325, /* -10 - -5.5 <20><> */
3309, 3292, 3275, 3258, 3241, 3223, 3205, 3187, 3169, 3151, /* -5 - -0.5 <20><> */
3132, 3114, 3095, 3075, 3056, 3037, 3017, 2997, 2977, 2957, /* 0 - 4.5 <20><> */
2936, 2916, 2895, 2874, 2854, 2832, 2811, 2790, 2768, 2747, /* 9 - 9.5 <20><> */
2725, 2703, 2681, 2659, 2637, 2615, 2592, 2570, 2548, 2525, /* 10 - 14.5 <20><> */
2503, 2480, 2457, 2435, 2412, 2389, 2366, 2343, 2321, 2298, /* 15 - 19.5 <20><> */
2275, 2252, 2229, 2207, 2184, 2161, 2138, 2116, 2093, 2071, /* 20 - 24.5 <20><> */
2048, 2026, 2003, 1981, 1959, 1937, 1914, 1893, 1871, 1849, /* 25 - 29.5 <20><> */
1827, 1806, 1784, 1763, 1742, 1721, 1700, 1679, 1658, 1638, /* 30 - 34.5 <20><> */
1617, 1597, 1577, 1557, 1537, 1518, 1498, 1479, 1460, 1441, /* 35 - 39.5 <20><> */
1422, 1403, 1385, 1366, 1348, 1330, 1312, 1295, 1277, 1260, /* 40 - 44.5 <20><> */
1243, 1226, 1209, 1192, 1176, 1160, 1144, 1128, 1112, 1097, /* 45 - 49.5 <20><> */
1081, 1066, 1051, 1036, 1022, 1007, 993, 979, 965, 951, /* 50 - 54.5 <20><> */
938, 924, 911, 898, 885, 872, 860, 848, 835, 823, /* 55 - 59.5 <20><> */
811, 800, 788, 777, 765, 754, 743, 732, 722, 711, /* 60 - 64.5 <20><> */
701, 691, 681, 671, 661, 651, 642, 632, 623, 614, /* 65 - 69.5 <20><> */
605, 596, 588, 579, 571, 562, 554, 546, 538, 530, /* 70 - 74.5 <20><> */
522, 515, 507, 500, 493, 486, 478, 471, 465, 458, /* 75 - 79.5 <20><> */
451, 445, 438, 432, 426, 420, 414, 408, 402, 396, /* 80 - 84.5 <20><> */
390, 385, 379, 374, 368, 363, 358, 353, 348, 343, /* 85 - 89.5 <20><> */
338, 333, 328, 324, 319, 315, 310, 306, 301, 297, /* 90 - 94.5 <20><> */
293, 289, 285, 281, 277, 273, 269, 266, 262, 258, /* 95 - 99.5 <20><> */
255 /* 100 <20><> */
};
2024-07-11 06:58:55 +00:00
static uint16_t get_adc(uint8_t ADC_Channel);
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ɼ<EFBFBD><EFBFBD><EFBFBD>adc<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У׼
* @param val <EFBFBD>ɼ<EFBFBD><EFBFBD><EFBFBD>adc<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @retval У׼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>adc<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
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 <EFBFBD>õ<EFBFBD>adc<EFBFBD>ɼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param ADC_Channel adcͨ<EFBFBD><EFBFBD>
* @retval val adc<EFBFBD>ɼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
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 <EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval I <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
*/
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;
2024-07-11 06:58:55 +00:00
// printf(" CHG_CURR I : %d /10000 \n", (int)(I * 10000));
2024-07-11 06:58:55 +00:00
#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 <EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ
* @param
* @retval V <EFBFBD><EFBFBD>ѹֵ
*/
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 <EFBFBD>õ<EFBFBD><EFBFBD>ŵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval I <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
*/
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;
2024-07-11 06:58:55 +00:00
#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 <EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>ѹ
* @param
* @retval V <EFBFBD><EFBFBD>ѹֵ
*/
float get_PV1_VOLT_IN(void)
{
float V;
uint16_t V_ADC;
// GPIO_WriteBit(G_FFMOS_CON1_GPIO, G_FFMOS_CON1_PIN, SET);
2024-07-11 06:58:55 +00:00
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);
2024-07-11 06:58:55 +00:00
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 <EFBFBD>õ<EFBFBD>ϵͳ<EFBFBD><EFBFBD>ѹ<EFBFBD><EFBFBD>ѹ
* @param
* @retval V <EFBFBD><EFBFBD>ѹֵ
*/
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;
2024-07-11 06:58:55 +00:00
/**
* @brief <EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval V <EFBFBD><EFBFBD>ѹֵ
*/
float get_MOSFET_Temper(void)
{
float T = 0;
2024-07-11 06:58:55 +00:00
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;
}
}
2024-07-11 06:58:55 +00:00
// 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));
2024-07-11 06:58:55 +00:00
#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);
2024-07-11 06:58:55 +00:00
return T;
}
/**
* @brief <EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>ѹ
* @param
* @retval V <EFBFBD><EFBFBD>ѹֵ
*/
float get_PV2_VOLT_IN(void)
{
float V;
uint16_t V_ADC;
// GPIO_WriteBit(G_FFMOS_CON2_GPIO, G_FFMOS_CON2_PIN, SET);
2024-07-11 06:58:55 +00:00
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);
2024-07-11 06:58:55 +00:00
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;
}