Kaynağa Gözat

Разбираюсь с захватом PWM

TelenkovDmitry 11 ay önce
ebeveyn
işleme
4d34b0e33d
6 değiştirilmiş dosya ile 537 ekleme ve 531 silme
  1. 24 11
      modules/pwm_in.c
  2. 2 0
      modules/pwm_in.h
  3. BIN
      output/lasertag.bin
  4. 508 518
      project/ewarm/drone.dep
  5. 1 1
      project/settings/drone.wsdt
  6. 2 1
      user/main.c

+ 24 - 11
modules/pwm_in.c

@@ -9,6 +9,9 @@ static TIM_IC_InitTypeDef      sConfig;
 static TIM_SlaveConfigTypeDef  sSlaveConfig;
 
 
+__IO uint32_t uwIC2Value = 0;  // Captured Value
+__IO uint32_t uwDutyCycle = 0; // Duty Cycle Value
+__IO uint32_t uwFrequency = 0; // Frequency Value
 
 //
 void tim_pwm_in_init(void)
@@ -22,10 +25,11 @@ void tim_pwm_in_init(void)
     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
     GPIO_InitStruct.Pull = GPIO_PULLUP;
     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
-    GPIO_InitStruct.Alternate = GPIO_AF0_TIM3;
+    GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;//GPIO_AF0_TIM3;
     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
     
     HAL_NVIC_SetPriority(TIM3_IRQn, 0, 1);
+    //HAL_NVIC_SetPriority(TIM3_IRQn, 2, 0); // From lasertag project
     HAL_NVIC_EnableIRQ(TIM3_IRQn);
         
     TimHandle.Instance = TIM3;
@@ -37,17 +41,17 @@ void tim_pwm_in_init(void)
     TimHandle.Init.RepetitionCounter = 0;
     TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
     HAL_TIM_IC_Init(&TimHandle);
+    //HAL_TIM_Base_Init(&TimHandle);
 
     sConfig.ICPrescaler = TIM_ICPSC_DIV1;
     sConfig.ICFilter = 0;
   
+    sConfig.ICPolarity = TIM_ICPOLARITY_RISING; ;
+    sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
+    HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
+    
     sConfig.ICPolarity = TIM_ICPOLARITY_FALLING;
     sConfig.ICSelection = TIM_ICSELECTION_INDIRECTTI;
-  
-    HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
-
-    sConfig.ICPolarity = TIM_ICPOLARITY_RISING;
-    sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
     HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
     
     sSlaveConfig.SlaveMode        = TIM_SLAVEMODE_RESET;
@@ -58,8 +62,16 @@ void tim_pwm_in_init(void)
     
     HAL_TIM_SlaveConfigSynchro(&TimHandle, &sSlaveConfig);
     
-    HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_2);
     HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_1);
+    HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_2);
+}
+
+//
+void tim_print_out_pwm(void)
+{
+    printf("Captured Value %u\r\n", uwIC2Value);
+    printf("Duty Cycle Value %u\r\n", uwDutyCycle);
+    printf("Frequency Value %u\r\n\n", uwFrequency);
 }
 
 
@@ -75,16 +87,16 @@ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
     
     cnt++;
     
-#if 0  
-    if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
+#if 1  
+    if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
     {
         // Get the Input Capture value
-        uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
+        uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
 
         if (uwIC2Value != 0)
         {
             // Duty cycle computation
-            uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1)) * 100) / uwIC2Value;
+            uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2)) * 100) / uwIC2Value;
 
             // uwFrequency computation
             //TIM3 counter clock = (RCC_Clocks.HCLK_Frequency)
@@ -98,3 +110,4 @@ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
     }
 #endif    
 }
+

+ 2 - 0
modules/pwm_in.h

@@ -4,5 +4,7 @@
 //
 void tim_pwm_in_init(void);
 
+//
+void tim_print_out_pwm(void);
 
 #endif

BIN
output/lasertag.bin


Dosya farkı çok büyük olduğundan ihmal edildi
+ 508 - 518
project/ewarm/drone.dep


Dosya farkı çok büyük olduğundan ihmal edildi
+ 1 - 1
project/settings/drone.wsdt


+ 2 - 1
user/main.c

@@ -41,7 +41,8 @@ int main()
     
     while(1) 
     {
-        //HAL_Delay(1000);
+        HAL_Delay(1000);
+        tim_print_out_pwm();
         //led_togle();
         timer_Main();
     }

Bu fark içinde çok fazla dosya değişikliği olduğu için bazı dosyalar gösterilmiyor