|
|
@@ -7,10 +7,25 @@
|
|
|
#include "SysTick.h"
|
|
|
#include "math.h"
|
|
|
|
|
|
+
|
|
|
+enum {
|
|
|
+ VLOTAGE =0,
|
|
|
+ CURREENT,
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
#define CAIYANG (680)
|
|
|
|
|
|
+#define M_PI (3.1415926)
|
|
|
+
|
|
|
+
|
|
|
#define VLAUE1 (880*1000 + CAIYANG)
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
float read_voltage_v(uint16_t val)
|
|
|
{
|
|
|
float value = 0.0;
|
|
|
@@ -25,7 +40,7 @@ float read_voltage_v(uint16_t val)
|
|
|
value /= 10.0;
|
|
|
value /= CAIYANG;
|
|
|
value /= 1000.0;
|
|
|
- value /= 1.414213;
|
|
|
+// value /= 1.414213;
|
|
|
return value;
|
|
|
}
|
|
|
|
|
|
@@ -42,54 +57,80 @@ float read_voltage_I(uint16_t val)
|
|
|
value /= 10.0;
|
|
|
value /= 100.0;
|
|
|
value /= 1000.0;
|
|
|
- value /= 1.414213;
|
|
|
+// value /= 1.414213;
|
|
|
return value;
|
|
|
}
|
|
|
|
|
|
|
|
|
+extern uint8_t read_complete;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
uint8_t flash_led = 0;
|
|
|
-uint8_t read_flag = 0;
|
|
|
|
|
|
-extern uint16_t voltage_s[200];
|
|
|
-extern uint16_t current_s[200];
|
|
|
|
|
|
-void filter(uint16_t arr[],uint32_t lenth,float arr_o[])
|
|
|
-{
|
|
|
- float a[5] = {1,0,0.4860,0,0.0177};
|
|
|
- float b[5] = {0.0940,0.3759,0.5639,0.3759,0.0940};
|
|
|
- arr_o[0] = b[0] * arr[0];
|
|
|
- arr_o[1] = b[0] * arr[1] + b[1] * arr[0] - a[1] * arr_o[0];
|
|
|
- arr_o[2] = b[0] * arr[2] + b[1] * arr[1] + b[2] * arr[0] - a[1] * arr_o[1] - a[2] * arr_o[0];
|
|
|
- arr_o[3] = b[0] * arr[3] + b[1] * arr[2] + b[2] * arr[1] + b[3] * arr[0] - a[1] * arr_o[2] - a[2] * arr_o[1] - a[3] * arr_o[0];
|
|
|
- for (int i = 4; i < lenth; i++)
|
|
|
- {
|
|
|
- arr_o[i] = b[0] * arr[i] + b[1] * arr[i - 1] + b[2] * arr[i - 2] + b[3] * arr[i - 3] + b[4] * arr[i - 4] \
|
|
|
- - a[1] * arr_o[i - 1] - a[2] * arr_o[i - 2] - a[3] * arr_o[i - 3] - a[4] * arr_o[i - 4];
|
|
|
- }
|
|
|
+//float data_f[500];
|
|
|
+//float data_c[500];
|
|
|
+extern uint16_t data_s[500];
|
|
|
+extern uint16_t data_i[500];
|
|
|
+float value = 0.0;
|
|
|
|
|
|
-}
|
|
|
-#define VALUE_A (0xD0)
|
|
|
|
|
|
-void filter_kk(uint16_t arr[])
|
|
|
+extern uint16_t count_v ;
|
|
|
+extern uint16_t count_i ;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static float voltage_show = 0.0;
|
|
|
+static float current_show = 0.0;
|
|
|
+float v_data[500] = {0};
|
|
|
+
|
|
|
+
|
|
|
+static float calculate_voltage(uint16_t *data)
|
|
|
{
|
|
|
- uint16_t value_1 = arr[0];
|
|
|
- for(int i = 1;i < 200;i++)
|
|
|
+ float val_v = 0.0;
|
|
|
+ memset(v_data,0,sizeof(float)*500);
|
|
|
+
|
|
|
+ for(int j = 0; j < 500;j++)
|
|
|
{
|
|
|
- if((arr[i]-value_1) > VALUE_A)
|
|
|
+ if(count_v >=244)
|
|
|
{
|
|
|
- arr[i] = value_1;
|
|
|
- }else if((value_1 - arr[i])> VALUE_A)
|
|
|
+ v_data[j] = read_voltage_v(data[j]);
|
|
|
+ }else
|
|
|
{
|
|
|
- arr[i] = value_1;
|
|
|
+ v_data[j] = 0;
|
|
|
}
|
|
|
- value_1 = arr[i];
|
|
|
+
|
|
|
+ val_v += (v_data[j] *v_data[j]);
|
|
|
}
|
|
|
+ val_v /= 500;
|
|
|
+ val_v = sqrt(val_v);
|
|
|
+ val_v /= 0.707;
|
|
|
+ val_v /= 0.9904;
|
|
|
+ return val_v;
|
|
|
}
|
|
|
|
|
|
+static float calculate_current(uint16_t *data)
|
|
|
+{
|
|
|
+ float val_v = 0.0;
|
|
|
+ memset(v_data,0,sizeof(float)*500);
|
|
|
+ for(int j = 0; j < 500;j++)
|
|
|
+ {
|
|
|
+ if(count_i >= 244)
|
|
|
+ v_data[j] = read_voltage_I(data[j]);
|
|
|
+ else
|
|
|
+ v_data[j] = 0;
|
|
|
+ val_v += (v_data[j] *v_data[j]);
|
|
|
+ }
|
|
|
+ val_v /= 500;
|
|
|
+ val_v = sqrt(val_v);
|
|
|
+ val_v /= 0.707;
|
|
|
+ val_v /= 0.9904;
|
|
|
+
|
|
|
+ return val_v;
|
|
|
+}
|
|
|
|
|
|
-float V_max[100] = {0};
|
|
|
-float I_max[100] = {0};
|
|
|
|
|
|
|
|
|
int main()
|
|
|
@@ -102,95 +143,54 @@ int main()
|
|
|
InitTimer();
|
|
|
while(!dma_flag_get(DMA_CH0,DMA_FLAG_FTF));
|
|
|
dma_flag_clear(DMA_CH0,DMA_FLAG_FTF);
|
|
|
+
|
|
|
float voltage = 0.0;
|
|
|
float cureent = 0.0;
|
|
|
float show_val_1 =888.8;
|
|
|
float show_val_2 =888.8;
|
|
|
int index = 0;
|
|
|
- ADC_TimerEnable();
|
|
|
- float value_i[200] = {0.0};
|
|
|
- float value_v[200] = {0.0};
|
|
|
+ ADC_TimerEnable();;
|
|
|
+
|
|
|
while(1)
|
|
|
{
|
|
|
- if(read_flag)
|
|
|
- {
|
|
|
- //calculate
|
|
|
- read_flag = 0;
|
|
|
- float v_max = 0;
|
|
|
- float i_max = 0;
|
|
|
- filter_kk(voltage_s);
|
|
|
- filter_kk(current_s);
|
|
|
- filter(voltage_s,200,value_v);
|
|
|
- filter(current_s,200,value_i);
|
|
|
- for(int i = 0; i < 200;i++)
|
|
|
+
|
|
|
+ if(read_complete == 1)
|
|
|
{
|
|
|
- v_max = v_max > value_v[i] ? v_max : value_v[i];
|
|
|
- i_max = i_max > value_i[i] ? i_max : value_i[i];
|
|
|
- }
|
|
|
+ read_complete = 0;
|
|
|
|
|
|
- // voltage = read_voltage_v(v_1);
|
|
|
- // cureent = read_voltage_I(v_2);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- V_max[index] = read_voltage_v(v_max);
|
|
|
- I_max[index] = read_voltage_I(i_max);
|
|
|
- index ++;
|
|
|
- if(index == 100)
|
|
|
- {
|
|
|
+ voltage_show += calculate_voltage(data_s);
|
|
|
+
|
|
|
+ current_show += calculate_current(data_i);
|
|
|
|
|
|
- for(int i = 0; i < 100;i++)
|
|
|
+ count_i = 0;
|
|
|
+ count_v = 0;
|
|
|
+ index++;
|
|
|
+ ADC_TimerEnable();
|
|
|
+ if(index >= 20)
|
|
|
{
|
|
|
- voltage +=V_max[i];
|
|
|
- cureent +=I_max[i];
|
|
|
- // V_max[i] = Filter(V_max[i],V_max[i-1]);
|
|
|
- // I_max[i] = Filter(I_max[i],I_max[i-1]);
|
|
|
- // voltage += read_voltage_v(V_max[i]);
|
|
|
- // cureent += read_voltage_I(I_max[i]);
|
|
|
+ index = 0;
|
|
|
+ show_val_2 = (voltage_show /20.0);
|
|
|
+ voltage_show = 0;
|
|
|
+ show_val_1 = (current_show /20.0);
|
|
|
+ current_show = 0;
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
- show_val_1 = (voltage/100)-3.0;
|
|
|
- show_val_2 = cureent/100;
|
|
|
- voltage = 0.0;
|
|
|
- cureent = 0.0;
|
|
|
- index = 0;
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
- ADC_TimerEnable();
|
|
|
- }
|
|
|
-
|
|
|
-// if(read_flag == 0){
|
|
|
-// float val = read_voltage();
|
|
|
-// if(val > 1000)
|
|
|
-// val = 0.0;
|
|
|
-// voltage = (val > voltage ) ? val : voltage;
|
|
|
-// val = read_current();
|
|
|
-// current = (val > current ) ? val : current;
|
|
|
-// }else{
|
|
|
-// voltage_s[index] = voltage;
|
|
|
-// current_s[index++] = current;
|
|
|
-// //display_voltage(voltage);
|
|
|
-// if(index == 100)
|
|
|
-// {
|
|
|
-// index = 0;
|
|
|
-// Bubble_Sort(voltage_s,100);
|
|
|
-// Bubble_Sort(current_s,100);
|
|
|
-// show_val_1 = voltage_s[50];
|
|
|
-// show_val_2 = current_s[50];
|
|
|
-// }
|
|
|
-// voltage = 0.0;
|
|
|
-// current = 0.0;
|
|
|
-// read_flag = 0;
|
|
|
-// //
|
|
|
-// ADC_TimerEnable();
|
|
|
-// }
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
display_voltage(show_val_1,show_val_2);
|
|
|
if(flash_led)
|
|
|
{
|
|
|
led_flash();
|
|
|
flash_led = 0;
|
|
|
+
|
|
|
+ //display_voltage(show_val_1,show_val_2);
|
|
|
+ //show_val_2 = read_voltage();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|