void TIM1_Mode_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM1_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //TIM5时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_11; //GPIOC6/7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
//GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PAC
GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1); //PC6复用位定时器8
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_TIM1); //PC7复用位定时器8
TIM_TimeBaseStructure.TIM_Prescaler=0; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period=(390)-1; //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM1_ICInitStructure);
TIM1_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM1_ICInitStructure.TIM_ICFilter = 0x6;
TIM_ICInit(TIM1, &TIM1_ICInitStructure);
TIM1_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI2上
TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM1_ICInitStructure.TIM_ICFilter = 0x6;
TIM_ICInit(TIM1, &TIM1_ICInitStructure);
TIM_SetCounter(TIM1,0);
//TIM_ITConfig(TIM8,TIM_IT_Update|TIM_IT_CC1,ENABLE);//允许更新中断 ,允许CC1IE捕获中断
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);//允许更新中断 ,允许CC1IE捕获中断
TIM_Cmd(TIM1,ENABLE ); //使能定时器5
NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
direction1 = (TIM1->CR1 & TIM_CR1_DIR ? FORWARD1 : BACKWARD1);
// NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_TIM13_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级3
// NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级3
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
// NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
// TIM_ITConfig(TIM8, TIM_IT_CC1 |TIM_IT_CC2,ENABLE);
// NVIC_EnableIRQ(TIM8_CC_IRQn);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>