ソースを参照

STM32: 71 samples + 4 Avg

Thomas Chef 3 年 前
コミット
9ede735c8f
3 ファイル変更64 行追加27 行削除
  1. 4 0
      STM32/Core/Src/cmdRx.c
  2. 20 10
      STM32/Core/Src/main.c
  3. 40 17
      STM32/Core/Src/stm32f1xx_it.c

+ 4 - 0
STM32/Core/Src/cmdRx.c

@@ -2,6 +2,8 @@
 #include "stdio.h"
 #include "main.h"
 
+/*
+
 #define MAX_LEN 100
 
 void handleCmdRx() {
@@ -48,3 +50,5 @@ void handleCmdRx() {
 		}
 	}
 }
+
+*/

+ 20 - 10
STM32/Core/Src/main.c

@@ -64,7 +64,7 @@ static void MX_ADC1_Init(void);
 static void MX_TIM1_Init(void);
 static void MX_NVIC_Init(void);
 /* USER CODE BEGIN PFP */
-void handleCmdRx();
+//void handleCmdRx();
 
 /* USER CODE END PFP */
 
@@ -76,15 +76,18 @@ int getADCDiff() {
 	volatile uint32_t firstCnt;
 	int32_t timeoutCnt = 20;
 
-	firstCnt = adcCounter;
-	HAL_TIM_Base_Stop(&htim1);	// Stop TIM1-Counter to maybe stop disturbances in measurments
-	ADC1->CR1  = ADC_IT_EOC; // Enable interrupt
 
-	while( timeoutCnt > 0 && adcCounter < (firstCnt+3) ) {
-		timeoutCnt--;
-		HAL_Delay(10);	// Three measurements takes around 140mS. 20*10 = 200mS timeout
+	HAL_TIM_Base_Stop(&htim1);	// Stop TIM1-Counter to maybe stop disturbances in measurements
+
+	for(int i=0;i<3;i++) {
+		firstCnt = adcCounter;
+		ADC1->CR1  = ADC_IT_EOC; // Enable interrupt
+		while( timeoutCnt > 0 && adcCounter == firstCnt ) {
+			timeoutCnt--;
+			HAL_Delay(10);	// Three measurements takes around 140mS. 20*10 = 200mS timeout
+		}
 	}
-	ADC1->CR1  = 0; // Disable interrupts
+
 	HAL_TIM_Base_Start(&htim1); // Start TIM1 again
 
 	if( timeoutCnt == 0 ) return 0;
@@ -106,7 +109,8 @@ void waitForNextPeriod(uint32_t Delay)
 
   while ((HAL_GetTick() - tickstart) < wait)
   {
-	  handleCmdRx();
+	  HAL_Delay(5);
+	  //handleCmdRx();
   }
 }
 
@@ -196,6 +200,7 @@ int main(void)
 
 
 	  uint32_t startTime = HAL_GetTick();
+	  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
 
 
 	  int adcDiff = getADCDiff();
@@ -209,6 +214,9 @@ int main(void)
 		  printf(",%.2f}\n",temp);
 	  }
 
+	  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
+
+	  //HAL_Delay(1000);
 	  waitForNextPeriod( TIME_BETWEEN_READS-(HAL_GetTick()-startTime)-1 );
   }
   /* USER CODE END 3 */
@@ -316,7 +324,9 @@ static void MX_ADC1_Init(void)
   HAL_ADCEx_Calibration_Start(&hadc1);
 
   // Set sampling frequency
-  sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
+  //sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES_5;
+  //sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
+  sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
   HAL_ADC_ConfigChannel(&hadc1, &sConfig);
 
   /* USER CODE END ADC1_Init 2 */

+ 40 - 17
STM32/Core/Src/stm32f1xx_it.c

@@ -217,36 +217,59 @@ void ADC1_2_IRQHandler(void)
 	 * max-min measured in ADC of 2605 = 2098,7mV so a small diff of 61mV
 	 */
 
+	const uint16_t NO_OF_SAMPLES = 3333*2;	//71samples = 3333/Wave
+	//const uint16_t NO_OF_SAMPLES = 4117*2;	//55samples = 4117/Wave
+	const uint16_t FILTER_STEPS = 4;
 
+	static uint8_t state = 0;	// 0=Pre-sampl, 1=Sample, 3=Done
+
+	static uint32_t filteredValue=0;
+	static uint32_t emaVal = 0;
 	static uint16_t adcData = 0;
 	static uint16_t minValue = 0xFFF;
 	static uint16_t maxValue = 0;
-	static uint16_t minCounter = 0;
-
-	static uint32_t cnt = 10000;	// 205 882,35Hz = 4 117,6 samples/wave. 10000 samples = 48mS = 2.4 Waves
-
-	adcData = ADC1->DR;
-
-	if( cnt > 0 ) {
-		if( adcData > maxValue ) {
-			maxValue = adcData;
+	static uint32_t cnt = NO_OF_SAMPLES;
+
+	adcData = ADC1->DR;		// Get data
+
+	switch( state ) {
+	case 0:
+		// Pre-sample state. Sample and avg
+		filteredValue += adcData;
+		if( cnt == (NO_OF_SAMPLES-FILTER_STEPS+1) ) {
+			filteredValue /= FILTER_STEPS;
+			emaVal =  (filteredValue << FILTER_STEPS) - filteredValue; // Init start value
+			state = 1;
 		}
-		if( adcData < minValue ) {
-			minValue = adcData;
-			minCounter++;
+		cnt--;
+		break;
+	case 1:
+		// Sampling state
+		emaVal += adcData;
+		filteredValue = (emaVal + (1 << (FILTER_STEPS - 1) )) >> FILTER_STEPS;
+		emaVal -= filteredValue;
+		if( filteredValue > maxValue ) maxValue = filteredValue;
+		if( filteredValue < minValue ) minValue = filteredValue;
+		if( cnt == 0 ) {
+			state = 2;
 		}
 		cnt--;
-	}
-	else {
-		cnt = 10000;
+		break;
+	case 2:
+		// End of main sampling. Reset everything
+		ADC1->CR1  = 0; // Disable interrupts
+		filteredValue = 0;
+		cnt=NO_OF_SAMPLES;
+		state = 0;
 		maxWaveDiff = (int)maxValue - (int)minValue;
-		maxValue = 0;
 		minValue = 0xFFF;
-		minCounter = 0;
+		maxValue = 0;
 		adcCounter++;
+		break;
 	}
 
 
+
   /* USER CODE END ADC1_2_IRQn 0 */
   /* USER CODE BEGIN ADC1_2_IRQn 1 */