下面是定时器初始化代码
- //定时器3初始化
- //arr:定时重装值
- //psc:分频值
- voidTIM3_Init(uint16_tarr,uint16_tpsc)
- {
- TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
- NVIC_InitTypeDefNVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//开启TIM3时钟
- //定时器3配置,重载值、分频值、分频系数、计数模式
- TIM_TimeBaseStructure.TIM_Period=arr;
- TIM_TimeBaseStructure.TIM_Prescaler=psc;
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
- TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//允许定时更新中断
- //中断配置
- NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- TIM_Cmd(TIM3,ENABLE);//使能定时器3
- }
定时器中断处理函数
- voidTIM3_IRQHandler(void)
- {
- staticuint8_ti;//相序计数器
- if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)
- {
- TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
- Motorcw(i);//电机正转
- i++;
- if(i==4)
- i=0;
- }
- }
电机正传代码函数
- voidMotorcw(uint8_ti)
- {
- GPIO_Write(GPIOC,phasecw[i]);
- }
这样的话就不能干扰到uip的正常事件处理了。