|
说明:感觉这种方式实用价值不大,因为要每次都进入中断里面更新CCR比较捕获寄存器。
不如用户自己搞个定时器中断,在中断里面直接操作IO即可,简单省事。
调用函数HAL_TIM_OC_Init,HAL_TIM_OC_ConfigChannel和HAL_TIM_OC_Start_IT即可实现

官方例子里面是测试了4路,我这里用示波器测试了前两路效果:
PA.08 (TIM1_Channel1)
PA.09 (TIM1_Channel2)
PA.10 (TIM1_Channel3)
PE.14 (TIM1_Channel4)

实现的代码如下:
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- __IO uint32_t uhCCR1_Val = 40960;
- __IO uint32_t uhCCR2_Val = 20480;
- __IO uint32_t uhCCR3_Val = 10240;
- __IO uint32_t uhCCR4_Val = 5120;
- uint32_t uhCapture = 0;
- /* Timer handler declaration */
- TIM_HandleTypeDef TimHandle;
- /* Timer Output Compare Configuration Structure declaration */
- TIM_OC_InitTypeDef sConfig;
- /* Counter Prescaler value */
- uint32_t uwPrescalerValue = 0;
- /* Private function prototypes -----------------------------------------------*/
- static void SystemClock_Config(void);
- static void Error_Handler(void);
- static void CPU_CACHE_Enable(void);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program.
- * @param None
- * @retval None
- */
- int main(void)
- {
- /* Enable the CPU Cache */
- CPU_CACHE_Enable();
-
- /* STM32H7xx HAL library initialization:
- - Systick timer is configured by default as source of time base, but user
- can eventually implement his proper time base source (a general purpose
- timer for example or other time source), keeping in mind that Time base
- duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
- handled in milliseconds basis.
- - Set NVIC Group Priority to 4
- - Low Level Initialization
- */
- HAL_Init();
- /* Configure the system clock to 400 MHz */
- SystemClock_Config();
- /* Configure LED2 */
- BSP_LED_Init(LED2);
- /*##-1- Configure the TIM peripheral #######################################*/
- /* ---------------------------------------------------------------------------
- TIM1 Configuration: Output Compare Toggle Mode:
- To get TIM1 counter clock at 20 MHz, the prescaler is computed as follows:
- Prescaler = (TIM1CLK / TIM1 counter clock) - 1
- Prescaler = (SystemCoreClock /2*20000000) - 1
- CC1 update rate = TIM1 counter clock / uhCCR1_Val
- = 20 MHz/40961 = 488.269 Hz
- ==> So the TIM1 Channel 1 generates a periodic signal with a frequency equal
- to 244.13 Hz.
- CC2 update rate = TIM1 counter clock / uhCCR2_Val
- = 20 MHz/20480 = 976.56 Hz
- ==> So the TIM1 Channel 2 generates a periodic signal with a frequency equal
- to 488.28 Hz.
- CC3 update rate = TIM1 counter clock / uhCCR3_Val
- = 20 MHz/10240 = 1953.1 Hz
- ==> So the TIM1 Channel 3 generates a periodic signal with a frequency equal
- to 976.56 Hz.
- CC4 update rate = TIM1 counter clock / uhCCR4_Val
- = 20 MHz/5120 = 3906.25 Hz
- ==> So the TIM1 Channel 4 generates a periodic signal with a frequency equal
- to 1953.12 Hz.
- --------------------------------------------------------------------------- */
- /* Compute the prescaler value to have TIM1 counter clock equal to 20 MHz */
- uwPrescalerValue = (uint32_t)((SystemCoreClock / 40000000) - 1);
-
- TimHandle.Instance = TIM1;
-
- TimHandle.Init.Period = 65535;
- TimHandle.Init.Prescaler = uwPrescalerValue;
- TimHandle.Init.ClockDivision = 0;
- TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
- if(HAL_TIM_OC_Init(&TimHandle) != HAL_OK)
- {
- /* Initialization Error */
- Error_Handler();
- }
-
- /*##-2- Configure the Output Compare channels ##############################*/
- /* Common configuration for all channels */
- sConfig.OCMode = TIM_OCMODE_TOGGLE;
- sConfig.OCPolarity = TIM_OCPOLARITY_LOW;
- /* Output Compare Toggle Mode configuration: Channel1 */
- sConfig.Pulse = uhCCR1_Val;
- if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
- {
- /* Configuration Error */
- Error_Handler();
- }
-
- /* Output Compare Toggle Mode configuration: Channel2 */
- sConfig.Pulse = uhCCR2_Val;
- if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
- {
- /* Configuration Error */
- Error_Handler();
- }
-
- /* Output Compare Toggle Mode configuration: Channel3 */
- sConfig.Pulse = uhCCR3_Val;
- if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
- {
- /* Configuration Error */
- Error_Handler();
- }
-
- /* Output Compare Toggle Mode configuration: Channel4 */
- sConfig.Pulse = uhCCR4_Val;
- if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK)
- {
- /* Configuration Error */
- Error_Handler();
- }
- /*##-3- Start signals generation #######################################*/
- /* Start channel 1 in Output compare mode */
- if(HAL_TIM_OC_Start_IT(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- /* Start channel 2 in Output compare mode */
- if(HAL_TIM_OC_Start_IT(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- /* Start channel 3 in Output compare mode */
- if(HAL_TIM_OC_Start_IT(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- /* Start channel 4 in Output compare mode */
- if(HAL_TIM_OC_Start_IT(&TimHandle, TIM_CHANNEL_4) != HAL_OK)
- {
- /* Starting Error */
- Error_Handler();
- }
- while (1)
- {}
- }
- /**
- * @brief Output Compare callback in non blocking mode
- * @param htim : TIM OC handle
- * @retval None
- */
- void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
- {
- /* TIM1_CH1 toggling with frequency = 244.13 Hz */
- if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
- {
- uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
- /* Set the Capture Compare Register value */
- __HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_1, (uhCapture + uhCCR1_Val));
- }
-
- /* TIM1_CH2 toggling with frequency = 488.28 Hz */
- if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
- {
- uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
- /* Set the Capture Compare Register value */
- __HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_2, (uhCapture + uhCCR2_Val));
- }
-
- /* TIM1_CH3 toggling with frequency = 976.56 Hz */
- if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
- {
- uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3);
- /* Set the Capture Compare Register value */
- __HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_3, (uhCapture + uhCCR3_Val));
- }
-
- /* TIM1_CH4 toggling with frequency = 1953.12 Hz */
- if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
- {
- uhCapture = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_4);
- /* Set the Capture Compare Register value */
- __HAL_TIM_SET_COMPARE(&TimHandle, TIM_CHANNEL_4, (uhCapture + uhCCR4_Val));
- }
- }
复制代码
|
评分
-
查看全部评分
|