一、目的 在正点原子精英版上,采用TIM5的通道1(PA0)作为输入捕获,捕获PA0上高电平的脉宽(用WK_UP)按键输入高电平,通过串口打印高电平脉宽时间。
二、输入捕获简介 输入捕获模式可以用来测量脉冲宽度或者测量频率。STM32定时器,除了TIM6和TIM7,其他定时器都有输入捕获功能。STM32的输入捕获,简单的说就是通过检测TIMx_CHx上的边沿信号,在边沿信号发生跳变(上升沿/下降沿)的时候,将当前定时器的值(TIMx_CNT)存放到对应的通道的捕获/比较寄存器(TIMx_CCRx)里面,完成一次捕获。同时还可以配置捕获时是否触发中断/DMA等。 设计思路: 本次实验采用TIM5_CH1来捕获高电平脉宽,先设置输入捕获为上升沿检测,记录发生上升沿的时候TIM5_CNT的值。然后配置输入捕获信号为下降沿捕获,当下降沿到来时,发生捕获,并记录此时TIM5_CNT的值。这样,前后两次TIM5_CNT之差,就是高电平的脉宽。TIM5的计数频率已知,由此我们可以准确计算出高电平脉宽时间。
三、程序设计 输入捕获配置步骤如下: (1)时钟使能,开启TIM5时钟和GPIOA时钟; (2)初始化TIM5,配置其分频系数、ARR和PSC; (3)设置TIM5输入捕获通道1比较参数,开启输入捕获; (4)初始化NVIC中断优先级分组; (5)使能TIM5的更新中断和捕获中断; (6)编写中断服务函数; (7)使能定时器TIM5。
void TIM5_Cap_init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitSturct;
TIM_ICInitTypeDef TIM_ICInitSturct;
NVIC_InitTypeDef NVIC_InitSturct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
TIM_TimeBaseInitSturct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitSturct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitSturct.TIM_Period = arr;
TIM_TimeBaseInitSturct.TIM_Prescaler = psc;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitSturct);
TIM_ICInitSturct.TIM_Channel = TIM_Channel_1;
TIM_ICInitSturct.TIM_ICFilter =0;
TIM_ICInitSturct.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitSturct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitSturct.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM5,&TIM_ICInitSturct);
NVIC_InitSturct.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitSturct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitSturct.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitSturct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitSturct);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM5,ENABLE);
}
u8 TIM5CH1_CATURE_STA = 0;
u16 TIM5CH1_CAPTURE_VAL;
void TIM5_IRQHandler(void)
{
if((TIM5CH1_CATURE_STA & 0x80) == 0)
{
if(TIM_GetITStatus(TIM5,TIM_IT_Update) == SET)
{
if(TIM5CH1_CATURE_STA & 0x40)
{
if((TIM5CH1_CATURE_STA & 0x3f) == 0x3f)
{
TIM5CH1_CATURE_STA |= 0x80;
TIM5CH1_CAPTURE_VAL = 0xffff;
}
else
TIM5CH1_CATURE_STA++;
}
}
}
if(TIM_GetITStatus(TIM5,TIM_IT_CC1) == SET)
{
if(TIM5CH1_CATURE_STA & 0x40)
{
TIM5CH1_CATURE_STA |= 0x80;
TIM5CH1_CAPTURE_VAL = TIM_GetCapture1(TIM5);
TIM_OC1PolarityConfig(TIM5,TIM_OCPolarity_High);
}
else
{
TIM5CH1_CATURE_STA = 0;
TIM5CH1_CAPTURE_VAL = 0;
TIM_SetCounter(TIM5,0);
TIM5CH1_CATURE_STA |= 0x40;
TIM_OC1PolarityConfig(TIM5,TIM_OCPolarity_Low);
}
}
TIM_ClearITPendingBit(TIM5,TIM_IT_Update|TIM_IT_CC1);
}
主函数
extern u8 TIM5CH1_CATURE_STA;
extern u16 TIM5CH1_CAPTURE_VAL;
int main(void)
{
u32 temp = 0;
delay_init();
key_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
TIM5_Cap_init(0xffff,72-1);
while(1)
{
delay_ms(10);
if(TIM5CH1_CATURE_STA & 0x80)
{
temp = TIM5CH1_CATURE_STA & 0x3f;
temp *= 65536;
temp += TIM5CH1_CAPTURE_VAL;
printf("High:%d us\r\n",temp);
TIM5CH1_CATURE_STA = 0;
}
}
}
|