各位高手好,我想对电机的正反转进行计数,所以设计了一块带STM32F407的计数板,想用其中的定时器正交编码接口模式进行计数,因为没有用过单片机,所以很多东西不懂,自己参考别人的改了一个程序,但编译不过去,所以在这里想请教各位。
void Tim_Init(void)
{
void gpio_init(void);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
Tim_Nvic_Init();
gpio_init();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // TIM3 clock enable
/* Compute the prescaler value */
PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 6000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Prescaler configuration */
TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);
/* Timer configuration in Encoder mode */
TIM_DeInit(ENCODER_TIMER);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;
TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);
//TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_TRC;
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 15;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(TIM3, TIM_FLAG_Update|TIM_IT_CC1|TIM_IT_CC2);
TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
//TIM_ITConfig(TIM4, TIM_FLAG_Update, ENABLE);
//Reset counter
TIM4->CNT=0x8000;
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
}
void gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
//Connect TIM3 Pin to AF2
GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_TIM3); //TIM3_Ch1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_TIM3); //TIM3_Ch2
/* Configure PB.04,05 as encoder input */
GPIO_InitStructure.GPIO_Pin=(GPIO_Pin_4|GPIO_Pin_5);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
中断服务函数呢?
一周热门 更多>