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