现在使用TIM4 CH1对外部脉冲进行计数,用的是输入捕获模式,现在遇到的问题是计数到255就自己溢出了~~而且能计数的频率好小啊,也就几百HZ。新手啊~~请各位大侠们指教一下,而且该怎么提高计数频率呢?
下面是配置代码
void TIM4_Configuration()
{
/********TIM3 GPIO与RCC设置**********/
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* PB6 TIM4_CH1 RCC */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能TIM4的GPIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); // 使能TIM4 CH1时钟 板子上铀为TIM4 CH1
/*TIM4 CH1 PB6*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //最高频率50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 输入
GPIO_Init(GPIOB, &GPIO_InitStructure); //根据上述参数初始化
/* Enable the TIM4 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3全局中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //次优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能指定的IRQ通道
NVIC_Init(&NVIC_InitStructure); // 按照以上参数初始化中断
/********TIM4 基础设置*************/
TIM_DeInit(TIM4); // 设置为缺省值
TIM_TimeBaseStructure.TIM_Period =0xffff ; // 计数器自动装载值 计数到65536溢出
TIM_TimeBaseStructure.TIM_Prescaler = 0x0000; // 预分频不分频
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 定时器时钟分频比为1 APB1出来最大36MHZ TIM3则为36MHZ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 计数器向上计数
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); // TIM 基础配置初始化
//TIM_ITRxExternalClockConfig(TIM2,TIM_TS_ETRF); //设置为外部ETR时钟
TIM_TIxExternalClockConfig(TIM4,TIM_TIxExternalCLK1Source_TI1,TIM_ICPolarity_Rising,0); //设置为外部时钟模式1 TI
/* input capture */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //INPUT CAPTURE 用CH1
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
/* TIM enable counter */
// TIM_Cmd(TIM4, ENABLE);
/* Enable the CC1 Interrupt Request */
TIM_SetCounter(TIM4, 0); // 初始化Timer的计数器初始值为0;
TIM_Cmd(TIM4, ENABLE); // 使能计数器
TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE); // 打开捕获中断
中断服务程序
if(TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET) // 中断时
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1); // 清楚中断待处理位
Count_U = TIM_GetCapture1(TIM4);
GPIO_ResetBits(GPIOF,GPIO_Pin_8 ); // DS3
Date_Display(0x80,0x80,Count_U); //128 128 |