tiM1捕获模式正常工作后,用__HAL_TIM_SET_PRESCALER(&htim1,psc);函数修改psc;__HAL_TIM_SET_AUTORELOAD()函数设置定时周期,修改后没有按照新的参数计数;
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- unsigned short psc = 143;
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- MX_TIM1_Init();
- /* USER CODE BEGIN 2 */
- printf("³õʼ»¯...
");
- HAL_TIM_Base_Start_IT(&htim1);
- HAL_TIM_IC_Start_IT(&htim1, TIM_CHANNEL_1);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)==GPIO_PIN_RESET)
- {
- HAL_Delay(20);
- if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)==GPIO_PIN_RESET)
- {
- printf("°´¼ü°´Ï£¡
");
- // cap_time = 1;
- // HAL_TIM_IC_Start_IT(&htim1,TIM_CHANNEL_1);
- //HAL_TIM_IC_Start(&htim5,TIM_CHANNEL_1);
- psc = psc - 10;
- if(psc < 20)
- {
- psc = 143;
- }
- __HAL_TIM_SET_PRESCALER(&htim1,psc);
-
- }
- while(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)==GPIO_PIN_RESET);
- }
-
- if(fre.flag == 1)
- {
- printf("fre = %f
",fre.result);
- fre.result = 0;
- fre.flag = 0;
- }
- }
- /* USER CODE END 3 */
- }
复制代码- void MX_TIM1_Init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_IC_InitTypeDef sConfigIC = {0};
- htim1.Instance = TIM1;
- htim1.Init.Prescaler = 143;
- htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim1.Init.Period = 50000;
- htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim1.Init.RepetitionCounter = 0;
- htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
- if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
- {
- Error_Handler();
- }
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_IC_Init(&htim1) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
- sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
- sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
- sConfigIC.ICFilter = 0;
- if (HAL_TIM_IC_ConfigChannel(&htim1, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
- }
复制代码- void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
- {
- if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_9) == GPIO_PIN_SET)
- {
- if(fre.times == 1)
- {
- //cap_value1 = HAL_TIM_ReadCapturedValue(&htim1,TIM_CHANNEL_1);
- fre.times = 2;
- fre.period = 0;
- __HAL_TIM_SET_COUNTER(&htim1,0);
- // Çå³ýÖжϱê־λ
- __HAL_TIM_CLEAR_IT(&htim1, TIM_IT_CC1);
-
- HAL_TIM_IC_Start_IT(&htim1, TIM_CHANNEL_1);
-
- printf("µÚÒ»¸öÉÏÉýÑØ£¬fre.period = %d
",fre.period);
-
- }
- else
- {
- fre.velue = HAL_TIM_ReadCapturedValue(&htim1,TIM_CHANNEL_1);
- fre.times = 1;
- //HAL_TIM_IC_Stop_IT(&htim1, TIM_CHANNEL_1);
- __HAL_TIM_CLEAR_IT(&htim1, TIM_IT_CC1);
- HAL_TIM_IC_Start_IT(&htim1, TIM_CHANNEL_1);
-
- printf("µÚ¶þ¸öÉÏÉýÑØ£¬fre.period = %d
",fre.period);
- fre.result = 1000000.0 / (fre.velue + fre.period * 1000);
- fre.period = 0;
- fre.flag = 1;
- }
- }
-
-
-
- }
复制代码- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- /* USER CODE BEGIN Callback 0 */
- /* USER CODE END Callback 0 */
- if (htim->Instance == TIM6) {
- HAL_IncTick();
- }
- /* USER CODE BEGIN Callback 1 */
- if (htim->Instance == TIM1) {
-
- }
- fre.period++;
- /* USER CODE END Callback 1 */
- }
复制代码
一周热门 更多>