你的浏览器版本过低,可能导致网站不能正常访问!为了你能正常使用网站功能,请使用这些浏览器。
yanhaijian 发表于 2015-7-30 16:42 前面还要初始化相关引脚,使能相关外设时钟。我以为你应该知道的。
小蚂蚁快溜跑 发表于 2015-7-30 16:50 void Tim3_init(void) {
yanhaijian 发表于 2015-7-31 08:18 你GPIOA外设时钟使能了吗?你TIMER3中断初始化了吗?
你好我好大家好! 发表于 2015-7-31 17:14 查看手册就好了
小蚂蚁快溜跑 发表于 2015-7-31 17:01 GPIOA的时钟使能了,TIM3的中断初始化是说中断优先级的初始化吗/
yanhaijian 发表于 2015-8-3 10:36 我说我只能帮你到这了。
void Tim3_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//TIM3
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_2);
//TIM2 CH2,CH3,CH4
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_Period = 65535 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI1,TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 15;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);
TIM3->SR = 0xfffe;
TIM_Cmd(TIM3, ENABLE);
}
这样吗?没有任何反应
你GPIOA外设时钟使能了吗?你TIMER3中断初始化了吗?
主要还是初始化配置的问题,中断处理比较简单,就是计数了。
GPIOA的时钟使能了,TIM3的中断初始化是说中断优先级的初始化吗/
看库函数使用手册,还是数据手册
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PB.06,07 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_DeInit(X_ENCODER_TIMER);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_Period = 40000-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(X_ENCODER_TIMER, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(X_ENCODER_TIMER, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = X_ICx_FILTER;
TIM_ICInit(X_ENCODER_TIMER, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(X_ENCODER_TIMER, TIM_FLAG_Update);
TIM_ITConfig(X_ENCODER_TIMER, TIM_IT_Update, ENABLE);
X_ENCODER_TIMER->SR = 0xfffe;
TIM_Cmd(X_ENCODER_TIMER, ENABLE);
我说我只能帮你到这了。
真的非常感谢~