staticvoidMX_TIM2_Init(void){/* USER CODE BEGIN TIM2_Init 0 *//* USER CODE END TIM2_Init 0 */
LL_TIM_InitTypeDef TIM_InitStruct ={0};
LL_GPIO_InitTypeDef GPIO_InitStruct ={0};/* Peripheral clock enable */LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);/**TIM2 GPIO Configuration
PA0-WKUP ------> TIM2_ETR
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_1;LL_GPIO_Init(GPIOA,&GPIO_InitStruct);/* USER CODE BEGIN TIM2_Init 1 *//* USER CODE END TIM2_Init 1 */
TIM_InitStruct.Prescaler =0;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload =4294967295;
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;LL_TIM_Init(TIM2,&TIM_InitStruct);LL_TIM_DisableARRPreload(TIM2);LL_TIM_ConfigETR(TIM2, LL_TIM_ETR_POLARITY_NONINVERTED, LL_TIM_ETR_PRESCALER_DIV1, LL_TIM_ETR_FILTER_FDIV1);LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_EXT_MODE2);LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);LL_TIM_DisableMasterSlaveMode(TIM2);/* USER CODE BEGIN TIM2_Init 2 *//* USER CODE END TIM2_Init 2 */}
定时器3,和4做为定时功能,准确定时1秒
/**
* @brief TIM3 Initialization Function
* @param None
* @retval None
*/staticvoidMX_TIM3_Init(void){/* USER CODE BEGIN TIM3_Init 0 *//* USER CODE END TIM3_Init 0 */
LL_TIM_InitTypeDef TIM_InitStruct ={0};/* Peripheral clock enable */LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);/* USER CODE BEGIN TIM3_Init 1 *//* USER CODE END TIM3_Init 1 */
TIM_InitStruct.Prescaler =0;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload =8400-1;
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;LL_TIM_Init(TIM3,&TIM_InitStruct);LL_TIM_DisableARRPreload(TIM3);LL_TIM_SetClockSource(TIM3, LL_TIM_CLOCKSOURCE_INTERNAL);LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_UPDATE);LL_TIM_EnableMasterSlaveMode(TIM3);/* USER CODE BEGIN TIM3_Init 2 *//* USER CODE END TIM3_Init 2 */}/**
* @brief TIM4 Initialization Function
* @param None
* @retval None
*/staticvoidMX_TIM4_Init(void){/* USER CODE BEGIN TIM4_Init 0 *//* USER CODE END TIM4_Init 0 */
LL_TIM_InitTypeDef TIM_InitStruct ={0};/* Peripheral clock enable */LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);/* TIM4 interrupt Init */NVIC_SetPriority(TIM4_IRQn,NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0,0));NVIC_EnableIRQ(TIM4_IRQn);/* USER CODE BEGIN TIM4_Init 1 *//* USER CODE END TIM4_Init 1 */
TIM_InitStruct.Prescaler =0;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload =10000-1;
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;LL_TIM_Init(TIM4,&TIM_InitStruct);LL_TIM_DisableARRPreload(TIM4);LL_TIM_SetTriggerInput(TIM4, LL_TIM_TS_ITR2);LL_TIM_SetSlaveMode(TIM4, LL_TIM_SLAVEMODE_GATED);LL_TIM_DisableIT_TRIG(TIM4);LL_TIM_DisableDMAReq_TRIG(TIM4);LL_TIM_SetTriggerOutput(TIM4, LL_TIM_TRGO_RESET);LL_TIM_DisableMasterSlaveMode(TIM4);/* USER CODE BEGIN TIM4_Init 2 */LL_TIM_EnableIT_UPDATE(TIM4);/* USER CODE END TIM4_Init 2 */}
定时器1秒中断
voidTIM4_IRQHandler(void){/* USER CODE BEGIN TIM4_IRQn 0 */if(LL_TIM_IsActiveFlag_UPDATE(TIM4)!= RESET){// TODO: 1秒中断的逻辑LL_TIM_ClearFlag_UPDATE(TIM4);if(pmt_start==0){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_RESET);LL_TIM_DisableCounter(TIM4);LL_TIM_DisableCounter(TIM3);LL_TIM_DisableCounter(TIM2);
pmt_counter=LL_TIM_GetCounter(TIM2);if(mpt_cnt<900){
pmt_data[mpt_cnt++]=pmt_counter;}}}/* USER CODE END TIM4_IRQn 0 *//* USER CODE BEGIN TIM4_IRQn 1 *//* USER CODE END TIM4_IRQn 1 */}
主程序实现
intmain(void){/* USER CODE BEGIN 1 *//* 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_TIM2_Init();MX_TIM3_Init();MX_TIM4_Init();/* USER CODE BEGIN 2 */HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_RESET);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while(1){if((pmt_start==1)||(pmt_start==2)){if(pmt_start==2){
pmt_start=3;}else{
pmt_start=0;}HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_SET);LL_TIM_SetCounter(TIM2,0);LL_TIM_SetCounter(TIM3,0);LL_TIM_SetCounter(TIM4,0);LL_TIM_EnableCounter(TIM2);LL_TIM_EnableCounter(TIM3);LL_TIM_EnableCounter(TIM4);}else{}HAL_Delay(1100);
pmt_start=1;/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */}