Browse Source

Cleanup. All good to go for P

Thomas Chef 3 years ago
parent
commit
6707caa929
1 changed files with 30 additions and 25 deletions
  1. 30 25
      STM32/Core/Src/main.c

+ 30 - 25
STM32/Core/Src/main.c

@@ -35,6 +35,7 @@
 /* Private define ------------------------------------------------------------*/
 /* USER CODE BEGIN PD */
 #define MAX_NO_OF_OW 5
+#define TIME_BETWEEN_READS 10000
 /* USER CODE END PD */
 
 /* Private macro -------------------------------------------------------------*/
@@ -69,15 +70,6 @@ void handleCmdRx();
 
 /* Private user code ---------------------------------------------------------*/
 /* USER CODE BEGIN 0 */
-/*void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
-{
-    // Conversion Complete & DMA Transfer Complete As Well
-    // So The AD_RES Is Now Updated & Let's Move IT To The PWM CCR1
-    // Update The PWM Duty Cycle With Latest ADC Conversion Result
-    //TIM2->CCR1 = (AD_RES<<4);
-	//HAL_GPIO_TogglePin (GPIOC, GPIO_PIN_13);
-	adcCounter++;
-}*/
 
 uint16_t getADCDiff() {
 
@@ -85,19 +77,38 @@ uint16_t getADCDiff() {
 	int32_t timeoutCnt = 20;
 
 	firstCnt = adcCounter;
-	ADC1->CR1  = ADC_IT_EOC; // Enable interrupts
+	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
 	}
 	ADC1->CR1  = 0; // Disable interrupts
+	HAL_TIM_Base_Start(&htim1); // Start TIM1 again
 
 	if( timeoutCnt == 0 ) return 0;
 
 	return maxWaveDiff;
 }
 
+/**
+ * This function is a copy of the HAL_Delay() but with the extra add-on
+ * to do some UART Rx work while waiting
+ */
+void waitForNextPeriod(uint32_t Delay)
+{
+  uint32_t tickstart = HAL_GetTick();
+  uint32_t wait = Delay;
+
+  /* Add a freq to guarantee minimum wait */
+  if (wait < HAL_MAX_DELAY) wait += (uint32_t)(uwTickFreq);
+
+  while ((HAL_GetTick() - tickstart) < wait)
+  {
+	  handleCmdRx();
+  }
+}
 
 /* USER CODE END 0 */
 
@@ -176,8 +187,6 @@ int main(void)
   //printf("\n");
   reset_oneWireSearch();
 
-  //adr = 0x28FF22DA551603C3;
-  //setConfigRegister(adr, 0x00); // 9-bit resolution
 
   while (1)
   {
@@ -186,25 +195,21 @@ int main(void)
     /* USER CODE BEGIN 3 */
 
 
-	  //adr = 0x080C25372F3D253F;
-
-	  for(uint8_t sId=0;sId<MAX_NO_OF_OW && adr[sId]>0 ;sId++) {
-		  float temp = readTemperature(adr[sId]);
-		  printf("{\"type\":\"temp\",\"id\":\"");
-		  for( uint8_t i = 8; i>0; i--) printf("%02X",(uint8_t)(adr[sId] >> ((i-1)*8))&0xFF);
-		  printf("\",\"value1\":%.2f}\n",temp);
-	  }
+	  uint32_t startTime = HAL_GetTick();
 
 
 	  uint16_t adcDiff = getADCDiff();
 	  //printf("Cnt:%lu  Diff:%u   mV:%u\n",adcCounter, adcDiff, (adcDiff*805)/1000);
+	  printf("{VPP,0,%u}\n",adcDiff);
 
-	  printf("{\"type\":\"vpp\",\"id\":\"0");
-	  printf("\",\"value1\":%u}\n",adcDiff);
-
-	  handleCmdRx();
+	  for(uint8_t sId=0;sId<MAX_NO_OF_OW && adr[sId]>0 ;sId++) {
+		  float temp = readTemperature(adr[sId]);
+		  printf("{OWT,");
+		  for( uint8_t i = 8; i>0; i--) printf("%02X",(uint8_t)(adr[sId] >> ((i-1)*8))&0xFF);
+		  printf(",%.2f}\n",temp);
+	  }
 
-	  HAL_Delay(1000);
+	  waitForNextPeriod( TIME_BETWEEN_READS-(HAL_GetTick()-startTime)-1 );
   }
   /* USER CODE END 3 */
 }