用
STM32f051的定时器2对外部输入的pwm进行捕获,配置好代码以后发现一个问题:无论如何大范围的改变PWM的频率,定时器的捕获值一直在一个很小的范围内变化,不知道是为什么??在捕获中断中读取的捕获值,代码贴上:
void Hall_Hall
timerInit(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Enable GPIOA, clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PA15 as HA pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF ;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure PB3 as HB pin,PB10 as HC pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_10;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_2);
TIM_DeInit(TIM2);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 65535; // Set full 16-bit working range
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICFilter =0x0B; // 11 <-> 1333 nsec //Êý×ÖÂ˲¨
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI ;//¸÷×ÔÁ¬½Óµ½¸÷×ÔµÄͨµÀÉÏ
TIM_ICInit(TIM2,&TIM_ICInitStructure);
// TIM_PrescalerConfig(TIM2, (u16)800u, TIM_PSCReloadMode_Immediate); //Èç¹ûÐèÒªµ÷Õû²âËÙ¾«¶È£¬ÔòÈ¡Ïû×¢ÊÍ
TIM_InternalClockConfig(TIM2); //¹Ø±Õ´Óģʽ£¬Ê¹ÓÃÄÚ²¿Ê±ÖÓ
//Enables the XOR of channel 1, channel2 and channel3
TIM_SelectHallSensor(TIM2, ENABLE);
//Timer2_Libin
TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1 ); //´¥·¢Ô´ÎªTI1FP1£¨hallÒì»ò£¬Â˲¨ºóµÃµ½£©
TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_Reset);//´ÓģʽΪ¸´Î»Ä£Ê½
TIM_ITConfig(TIM2, TIM_IT_CC1,DISABLE);
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_SetCounter(TIM2, 0x0);//¼ÆÊýÆ÷¸´Î»
TIM_Cmd(TIM2, ENABLE);
}
加个全局或静态变量用于计数溢出次数
一周热门 更多>