下面是定时器初始化代码
- //定时器3初始化
- //arr:定时重装值
- //psc:分频值
- void TIM3_Init(uint16_t arr, uint16_t psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_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
- }
定时器中断处理函数
- void TIM3_IRQHandler(void)
- {
- static uint8_t i; //相序计数器
- if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
- {
- TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
- Motorcw(i); //电机正转
- i++;
- if(i==4)
- i = 0;
- }
- }
电机正传代码函数
- void Motorcw(uint8_t i)
- {
- GPIO_Write(GPIOC,phasecw[i]);
- }
这样的话就不能干扰到uip的正常事件处理了。
上一篇:LPC2103学习之GPIO
下一篇:STM32用IO口控制步进电机的简单程序
推荐阅读最新更新时间:2024-03-16 14:51