1.CUBEMAX配置
测量PA6,PA7输出的占空比,只需要把主要的配置,配置为A6口就行,A7口黄色表示配置不正确,不用管。
2.软件代码
TIME.c中找到TIM3的初始化,在后面初始化A7口
void MX_TIM3_Init_PA7(void)
{/* USER CODE BEGIN TIM3_Init 0 *//* USER CODE END TIM3_Init 0 */TIM_ClockConfigTypeDef sClockSourceConfig = {0};TIM_SlaveConfigTypeDef sSlaveConfig = {0};TIM_MasterConfigTypeDef sMasterConfig = {0};TIM_IC_InitTypeDef sConfigIC = {0};/* USER CODE BEGIN TIM3_Init 1 *//* USER CODE END TIM3_Init 1 */htim3.Instance = TIM3;htim3.Init.Prescaler = 80-1;htim3.Init.CounterMode = TIM_COUNTERMODE_UP;htim3.Init.Period = 65535;htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;if (HAL_TIM_Base_Init(&htim3) != HAL_OK){Error_Handler();}sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK){Error_Handler();}if (HAL_TIM_IC_Init(&htim3) != HAL_OK){Error_Handler();}sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;sSlaveConfig.InputTrigger = TIM_TS_TI2FP2;//第一个地方sSlaveConfig.TriggerPolarity = TIM_INPUTCHANNELPOLARITY_RISING;sSlaveConfig.TriggerFilter = 0;if (HAL_TIM_SlaveConfigSynchro(&htim3, &sSlaveConfig) != HAL_OK){Error_Handler();}sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK){Error_Handler();}sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;//第二个地方sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;//第三个地方sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;sConfigIC.ICFilter = 0;if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_1) != HAL_OK){Error_Handler();}sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;//第四个地方sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;//第五个地方if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_2) != HAL_OK){Error_Handler();}/* USER CODE BEGIN TIM3_Init 2 *//* USER CODE END TIM3_Init 2 */}
循环函数中
static uint32_t time10ms;static uint8_t time3_sta=0;if(uwTick-time10ms>10){time10ms=uwTick;if(time3_sta==0)//PA6{duty1=(HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1)*100.0f/(HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1)+1);MX_TIM3_Init_PA7();HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_1);HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_2);}if(time3_sta==1)//PA7{duty2=(HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1)+1)*100.0f/(HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1);MX_TIM3_Init();HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_1);HAL_TIM_IC_Start(&htim3,TIM_CHANNEL_2);}time3_sta=!time3_sta;}