测量按键按下时长思路 测量按键按下时间实验目的 使用定时器 2 通道 2 来捕获按键 (按键接PA0)按下时间,并通过串口打印。 计一个数的时间:1us,PSC=71,ARR=65535 下降沿捕获、输入通道 2 映射在 TI2 上、不分频、不滤波
1.硬件
2.软件
定时器HAL驱动层文件添加 ic驱动文件添加 GPIO常用函数 定时器输入捕获实验配置步骤 main.c程序
# include "sys.h"
# include "delay.h"
# include "led.h"
# include "uart1.h"
# include "ic.h" int main ( void )
{ HAL_Init ( ) ; stm32_clock_init ( RCC_PLL_MUL9) ; led_init ( ) ; uart1_init ( 115200 ) ; printf ( "hello world!\r\n" ) ; ic_init ( 65536 - 1 , 72 - 1 ) ; while ( 1 ) { led1_on ( ) ; led2_off ( ) ; delay_ms ( 500 ) ; led1_off ( ) ; led2_on ( ) ; delay_ms ( 500 ) ; }
}
**ic_init(65536 - 1, 72 - 1);//计时1us
**语句定时参考
# include "ic.h"
# include "stdio.h" TIM_HandleTypeDef ic_handle = { 0 } ;
void ic_init ( uint16_t arr, uint16_t psc)
{ TIM_IC_InitTypeDef ic_config = { 0 } ; ic_handle. Instance = TIM2; ic_handle. Init. Prescaler = psc; ic_handle. Init. Period = arr; ic_handle. Init. CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_IC_Init ( & ic_handle) ; ic_config. ICPolarity = TIM_ICPOLARITY_FALLING; ic_config. ICSelection = TIM_ICSELECTION_DIRECTTI; ic_config. ICPrescaler = TIM_ICPSC_DIV1; ic_config. ICFilter = 0 ; HAL_TIM_IC_ConfigChannel ( & ic_handle, & ic_config, TIM_CHANNEL_2) ; __HAL_TIM_ENABLE_IT ( & ic_handle, TIM_IT_UPDATE) ; HAL_TIM_IC_Start_IT ( & ic_handle, TIM_CHANNEL_2) ;
} void HAL_TIM_IC_MspInit ( TIM_HandleTypeDef * htim)
{ if ( htim-> Instance == TIM2) { GPIO_InitTypeDef gpio_initstruct; __HAL_RCC_GPIOA_CLK_ENABLE ( ) ; __HAL_RCC_TIM2_CLK_ENABLE ( ) ; gpio_initstruct. Pin = GPIO_PIN_0; gpio_initstruct. Mode = GPIO_MODE_AF_PP; gpio_initstruct. Pull = GPIO_PULLUP; gpio_initstruct. Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init ( GPIOB, & gpio_initstruct) ; HAL_NVIC_SetPriority ( TIM2_IRQn, 2 , 2 ) ; HAL_NVIC_EnableIRQ ( TIM2_IRQn) ; }
} void TIM2_IRQHandler ( void )
{ HAL_TIM_IRQHandler ( & ic_handle) ;
} void HAL_TIM_IC_CaptureCallback ( TIM_HandleTypeDef * htim)
{ printf ( "捕获到下降沿\r\n" ) ;
}
# ifndef __IC_H__
# define __IC_H__ # include "sys.h"
void ic_init ( uint16_t arr, uint16_t psc) ; # endif
3.实物效果
硬件模块接线 KEY一端—>PA0 KEY另一端—>GND ST-Link下载方式 打开串口软件,当按键按下时,串口打印输出“捕获到下降沿”