pwm_in.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "stm32f0xx_hal.h"
  2. #include "pwm_in.h"
  3. #include <stdio.h>
  4. static TIM_HandleTypeDef TimHandle;
  5. static TIM_IC_InitTypeDef sConfig;
  6. static TIM_SlaveConfigTypeDef sSlaveConfig;
  7. __IO uint32_t uwIC2Value = 0; // Captured Value
  8. __IO uint32_t uwDutyCycle = 0; // Duty Cycle Value
  9. __IO uint32_t uwFrequency = 0; // Frequency Value
  10. //
  11. void tim_pwm_in_init(void)
  12. {
  13. GPIO_InitTypeDef GPIO_InitStruct;
  14. __HAL_RCC_GPIOA_CLK_ENABLE();
  15. __HAL_RCC_TIM3_CLK_ENABLE();
  16. GPIO_InitStruct.Pin = GPIO_PIN_6;
  17. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  18. GPIO_InitStruct.Pull = GPIO_PULLUP;
  19. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  20. GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;//GPIO_AF0_TIM3;
  21. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  22. HAL_NVIC_SetPriority(TIM3_IRQn, 0, 1);
  23. //HAL_NVIC_SetPriority(TIM3_IRQn, 2, 0); // From lasertag project
  24. HAL_NVIC_EnableIRQ(TIM3_IRQn);
  25. TimHandle.Instance = TIM3;
  26. TimHandle.Init.Period = 0xFFFF;
  27. TimHandle.Init.Prescaler = 0;
  28. TimHandle.Init.ClockDivision = 0;
  29. TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
  30. TimHandle.Init.RepetitionCounter = 0;
  31. TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  32. HAL_TIM_IC_Init(&TimHandle);
  33. //HAL_TIM_Base_Init(&TimHandle);
  34. sConfig.ICPrescaler = TIM_ICPSC_DIV1;
  35. sConfig.ICFilter = 0;
  36. sConfig.ICPolarity = TIM_ICPOLARITY_RISING; ;
  37. sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
  38. HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
  39. sConfig.ICPolarity = TIM_ICPOLARITY_FALLING;
  40. sConfig.ICSelection = TIM_ICSELECTION_INDIRECTTI;
  41. HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
  42. sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
  43. sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
  44. sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;
  45. sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
  46. sSlaveConfig.TriggerFilter = 0;
  47. HAL_TIM_SlaveConfigSynchro(&TimHandle, &sSlaveConfig);
  48. HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_1);
  49. HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_2);
  50. }
  51. //
  52. void tim_print_out_pwm(void)
  53. {
  54. printf("Captured Value %u\r\n", uwIC2Value);
  55. printf("Duty Cycle Value %u\r\n", uwDutyCycle);
  56. printf("Frequency Value %u\r\n\n", uwFrequency);
  57. }
  58. void TIM3_IRQHandler(void)
  59. {
  60. HAL_TIM_IRQHandler(&TimHandle);
  61. }
  62. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  63. {
  64. static uint32_t cnt = 0;
  65. cnt++;
  66. #if 1
  67. if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
  68. {
  69. // Get the Input Capture value
  70. uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
  71. if (uwIC2Value != 0)
  72. {
  73. // Duty cycle computation
  74. uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2)) * 100) / uwIC2Value;
  75. // uwFrequency computation
  76. //TIM3 counter clock = (RCC_Clocks.HCLK_Frequency)
  77. uwFrequency = (HAL_RCC_GetHCLKFreq()) / uwIC2Value;
  78. }
  79. else
  80. {
  81. uwDutyCycle = 0;
  82. uwFrequency = 0;
  83. }
  84. }
  85. #endif
  86. }