Pārlūkot izejas kodu

小电压表滤波,以及精确计算。

liyi 9 mēneši atpakaļ
vecāks
revīzija
1e741628e9
2 mainītis faili ar 124 papildinājumiem un 39 dzēšanām
  1. 1 1
      control/voltmeter/DISPLAY/display.c
  2. 123 38
      control/voltmeter/USER/main.c

+ 1 - 1
control/voltmeter/DISPLAY/display.c

@@ -328,7 +328,7 @@ void display_voltage(float num_f,float num_i)
 		{
 			if(channel[i] == 0)
 				show_num(i,num[i]);
-			for(int j = 0; j <1000;j++);
+			for(int j = 0; j <100;j++);
 		}
 //	}
 }

+ 123 - 38
control/voltmeter/USER/main.c

@@ -84,26 +84,74 @@ extern uint16_t count_i ;
 
 static float voltage_show = 0.0;
 static float current_show = 0.0;
-float v_data[500] = {0};
+//float v_data[500] = {0};
 
 
 static  float calculate_voltage(uint16_t *data)
 {
 	float val_v = 0.0;
-	memset(v_data,0,sizeof(float)*500);
-
-	for(int j = 0; j < 500;j++)
-	{
-		if(count_v >=244)
+	int h = 0;
+	uint8_t flag = 0;
+	int start = 0;
+	int end = 0;
+	
+//	memset(v_data,0,sizeof(float)*500);
+	
+	
+ for(int i = 0; i < 500;)
+{
+		if(data[i] > 0)
 		{
-			v_data[j] = read_voltage_v(data[j]);
+			start = i;
+			if(i == 500 -1)
+              break;
+			for(int j = i+1;j < 500;j++)
+			{
+				if(data[j] > 0)
+				{
+					h++;
+					if(j == (500-1))
+					{
+						end = j;
+					}
+				}else
+				{
+					end = j;
+					break;
+				}
+			}
+			if(h < 2)
+			{
+
+				for(int k = start;k <= end;k++)
+					data[k] =0;
+                
+			}
+      h =0;
+			i= (end+1);
 		}else
 		{
-			v_data[j] = 0;
+			i++;
 		}
-		
-		val_v += (v_data[j] *v_data[j]);
 	}
+
+	
+//	if(count_v >=244)
+//	{
+		for(int j = 0; j < 500;j++)
+		{
+			if(data[j])
+			{
+				float da = 0.0;
+				da = read_voltage_v(data[j]);
+				val_v += (da*da);
+			}else
+			{
+				val_v += 0;
+			}
+		}
+//	}
+	
 	val_v /= 500;
 	val_v = sqrt(val_v);
 	val_v /= 0.707;
@@ -114,14 +162,56 @@ static  float calculate_voltage(uint16_t *data)
 static  float calculate_current(uint16_t *data)
 {
 	float val_v = 0.0;
-	memset(v_data,0,sizeof(float)*500);
+	int h = 0;
+	uint8_t flag = 0;
+	int start = 0;
+	int end = 0;
+	
+ for(int i = 0; i < 500;)
+	{
+		if(data[i] > 0)
+		{
+			start = i;
+			if(i == 500 -1)
+              break;
+			for(int j = i+1;j < 500;j++)
+			{
+				if(data[j] > 0)
+				{
+					h++;
+					if(j == (500-1))
+					{
+						end = j;
+					}
+				}else
+				{
+					end = j;
+					break;
+				}
+			}
+			if(h < 2)
+			{
+
+				for(int k = start;k <= end;k++)
+					data[k] =0;
+                
+			}
+            h =0;
+			i= (end+1);
+		}else
+		{
+			i++;
+		}
+	}
+//	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]);
+		float da = 0.0;
+		if(data[j])
+		{
+			da = read_voltage_I(data[j]);
+			val_v +=(da*da);
+		}
 	}
 	val_v /= 500;
 	val_v = sqrt(val_v);
@@ -133,9 +223,13 @@ static  float calculate_current(uint16_t *data)
 
 
 
+float show_val_1 =888.8;
+float show_val_2 =888.8;
+
 int main()
 {
 	InitSysTick();
+	DelayNms(500);
 	DisplayInit();
 	BeepInit();
 	AdcInit();
@@ -146,48 +240,39 @@ int main()
 	
 	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();;
 
 	while(1)
 	{
-		
+
+
+		display_voltage(show_val_2,show_val_1);
+				
+		if(flash_led)
+		{
+			led_flash();
+			flash_led = 0;
 			if(read_complete == 1)
 			{
 				read_complete = 0;
-				
+					
 				voltage_show += calculate_voltage(data_s);
-				
 				current_show += calculate_current(data_i);
 
-				count_i = 0;
-				count_v = 0;
 				index++;
 				ADC_TimerEnable();
-				if(index >= 20)
+				if(index >= 5)
 				{
 					index = 0;
-					show_val_2 = (voltage_show /20.0);
+					show_val_2 = (voltage_show /5.0);
 					voltage_show = 0;
-					show_val_1 = (current_show /20.0);
+					show_val_1 = (current_show /5.0);
 					current_show = 0;				
 					
 				}
-					
 			}
-
-
-		 
-		
-		
-		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();