STM32在定時器中控制步進電機
下面是定時器初始化代碼
本文引用地址:http://m.butianyuan.cn/article/201611/319101.htm- //定時器3初始化
- //arr:定時重裝值
- //psc:分頻值
- voidTIM3_Init(uint16_tarr,uint16_tpsc)
- {
- TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
- NVIC_InitTypeDefNVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//開啟TIM3時鐘
- //定時器3配置,重載值、分頻值、分頻系數(shù)、計數(shù)模式
- 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
- }
定時器中斷處理函數(shù)
- voidTIM3_IRQHandler(void)
- {
- staticuint8_ti;//相序計數(shù)器
- if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)
- {
- TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
- Motorcw(i);//電機正轉(zhuǎn)
- i++;
- if(i==4)
- i=0;
- }
- }
電機正傳代碼函數(shù)
- voidMotorcw(uint8_ti)
- {
- GPIO_Write(GPIOC,phasecw[i]);
- }
這樣的話就不能干擾到uip的正常事件處理了。
評論