liyi 9 місяців тому
батько
коміт
10131fcae8
2 змінених файлів з 129 додано та 146 видалено
  1. 29 46
      control/voltmeter/TIMER/timer.c
  2. 100 100
      control/voltmeter/USER/main.c

+ 29 - 46
control/voltmeter/TIMER/timer.c

@@ -2,8 +2,8 @@
 #include "gd32e230_timer.h"
 #include "adc.h"
 
-uint16_t voltage_s[200] = {0};
-uint16_t current_s[200] = {0};
+
+
 
 static void ConfigTimer14(unsigned short arr, unsigned short psc)
 {
@@ -51,7 +51,7 @@ static void ConfigTimer15(unsigned short arr, unsigned short psc)
 
 void InitTimer(void)
 {
-	ConfigTimer14(99, 71);									//100ns
+	ConfigTimer14(39, 71);									//100ns
 	ConfigTimer15(9999, 7199);							//1s
 }
 
@@ -72,72 +72,55 @@ extern uint8_t read_flag;
 extern uint16_t adc_original_value[4];
 
 
+uint8_t flag_read_v_or_c = 0;
+
 
+uint16_t data_s[500];
+uint16_t data_i[500];
 
+uint8_t read_complete = 0;
 
 
+static int index = 0;
 
+uint32_t a,b,c,d;
+uint16_t count_v = 0;
+uint16_t count_i = 0;
 
 
 void TIMER14_IRQHandler(void)
 {  
-	static int index = 0;
+	
   if(timer_interrupt_flag_get(TIMER14, TIMER_INT_FLAG_UP) == SET) //判断定时器更新中断是否发生
   {
     timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP);       //清除定时器更新中断标志
   }
-	
-	if((adc_original_value[0] - adc_original_value[1]) >= 0x10)
+	a= adc_original_value[0];
+	b= adc_original_value[1];
+	c= adc_original_value[2];
+	d= adc_original_value[3];
+	if(a > b)
 	{
-		voltage_s[index] = adc_original_value[0] - adc_original_value[1]-5;
+		data_s[index] = a-b;
+		count_v++;
 	}else
 	{
-		voltage_s[index] = 0;  
+		data_s[index] = 0;
 	}
-	
-	if((adc_original_value[2] - adc_original_value[3]) >= 0x4)
+	if(c > d)
 	{
-		current_s[index] = adc_original_value[2] - adc_original_value[3]-3;
+		data_i[index] = c-d;
+		count_i++;
 	}else
 	{
-		current_s[index] = 0;  
+		data_i[index] = 0;
 	}
-//	if(adc_original_value[0] > adc_original_value[1])
-//	{
-//		if(adc_original_value[0] - adc_original_value[1] >= 10)
-//			voltage_s[index] = adc_original_value[0] - adc_original_value[1] - 5;
-//		else
-//			voltage_s[index] = 0;
-//		//voltage_s[index] = voltage_s[index] > 20 ? voltage_s[index]- 5: 0;
-//	}else
-//	{
-//		voltage_s[index] = 0;
-//	}
-//	if(adc_original_value[2] > adc_original_value[3])
-//	{
-//		if(adc_original_value[2] - adc_original_value[3] >= 4)
-//			current_s[index] = adc_original_value[2] - adc_original_value[3] -4;
-//		else
-//			current_s[index] = 0;
-//		//current_s[index] = current_s[index] > 10 ? current_s[index]- 4: 0;
-//	}else
-//	{
-//		current_s[index] = 0;
-//	}
-	index++;
-//	if(adc_original_value[0]>0x20 )
-//		voltage_s[index] = adc_original_value[0] -5;
-//	else
-//		voltage_s[index] = 0;
-//	if(adc_original_value[2] > 0x10 )
-//		current_s[index++] = adc_original_value[2]-4;
-//	else
-//		current_s[index++] = 0;
-	if(index == 200)
+	index ++;
+	if(index >= 500)
 	{
 		index = 0;
-		ADC_TimerDisable();		//close timer and calculate
-		read_flag = 1;
+		read_complete = 1;
+		ADC_TimerDisable();
 	}
 }
 

+ 100 - 100
control/voltmeter/USER/main.c

@@ -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();
+
 		}
 		
 	}