举例- typedef struct
- {
- uint16_t TIM_Channel; //通道
- uint16_t TIM_ICPolarity; //捕获极性
- uint16_t TIM_ICSelection; //直接/间接映射
- uint16_t TIM_ICPrescaler; //分频因子
- uint16_t TIM_ICFilter; //滤波
- } TIM_ICInitTypeDef;
- #define ICPolarity_Rising 0x40
- #define ICPolarity_Falling 0x80
- #define Capture1_Max 0x3f
- void TIM5_IRQHandler()
- {
- if(TIM_GetITStatus(TIM5, TIM_IT_Update) == SET) //产生更新中断
- {
- if((TIM_sta & ICPolarity_Falling) == ICPolarity_Falling) //低电平捕获中
- {
- if((TIM_sta & Capture1_Max) == Capture1_Max) //达到计数值上限
- {
- TIM_sta = ICPolarity_Falling;
- }
- else
- {
- TIM_sta++; //更新次数
- }
- }
- }
- if(TIM_GetITStatus(TIM5, TIM_IT_CC1) == SET)
- {
- if((TIM_sta & ICPolarity_Falling) == ICPolarity_Falling) //捕获到低电平
- {
- TIM_sta |= ICPolarity_Rising;
- Capture1_val = TIM_GetCapture1(TIM5); //保存捕获值
- TIM_OC1PolarityConfig(TIM5, TIM_ICPolarity_Rising); //设置上升沿捕获
- }
- else
- {
- TIM_sta = ICPolarity_Falling;
- TIM_Cmd(TIM5, DISABLE);
- TIM_OC1PolarityConfig(TIM5, TIM_ICPolarity_Falling); //设置下降沿捕获
- TIM_SetCounter(TIM5, 0); //计数器值清零
- TIM_Cmd(TIM5, ENABLE);
- }
- }
-
- TIM_ClearITPendingBit(TIM5, TIM_IT_Update | TIM_IT_CC1);
- }
- void KEY_TIM5_init(u16 period, u16 prescaler)
- {
- TIM_TimeBaseInitTypeDef tim_tb = {0};
- TIM_ICInitTypeDef tim_ic =
- {
- TIM_Channel_1, //通道1
- TIM_ICPolarity_Rising, //上升沿
- TIM_ICSelection_DirectTI, //直接映射
- TIM_ICPSC_DIV1, //1分频
- 0
- };
- NVIC_InitTypeDef nvic =
- {
- TIM5_IRQn,
- 2,
- 2,
- ENABLE
- };
- GPIO_InitTypeDef gpio =
- {
- GPIO_Pin_0,
- GPIO_Speed_50MHz,
- GPIO_Mode_IPD //下拉输入模式
- };
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //使能定时器时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- GPIO_Init(GPIOA, &gpio);
-
- tim_tb.TIM_Prescaler = prescaler;
- tim_tb.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
- tim_tb.TIM_Period = period;
- tim_tb.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInit(TIM5, &tim_tb); //初始化定时器
- TIM_ICInit(TIM5, &tim_ic); //设置通用定时器的输入捕获参数
- TIM_ITConfig(TIM5, TIM_IT_Update | TIM_IT_CC1, ENABLE); //开启捕获和定时器溢出中断
- NVIC_Init(&nvic); //初始化NVIC
- TIM_Cmd(TIM5, ENABLE); //使能定时器
- }
- int main(void)
- {
- KEY_TIM5_init(0xffff, 72 - 1);
- while(1)
- {
- if((TIM_sta & ICPolarity_Rising) == ICPolarity_Rising) //捕获到低电平
- {
- res_data = TIM_sta & Capture1_Max;
- res_data *= 0xffff;
- res_data += Capture1_val;
- printf("高电平持续时间 %d um.\n", res_data);
- TIM_sta = 0;
- }
- }
- }
|