專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> STM32 >> 瀏覽文章

STM32 利用systick控制步進(jìn)電機(jī)

作者:龍麗嫦   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年05月07日   【字體:

原來步進(jìn)電機(jī)不適合用PWM控制,我用的是systick產(chǎn)生標(biāo)準(zhǔn)延時(shí),然后驅(qū)動(dòng)4個(gè)IO口產(chǎn)生交替變換的脈沖,先上程序,這次我用的是stm32f407discovery開發(fā)板。

 

#include "main.h"

void delay_ms(u32 count);

int main (void)
{
 

 
  GPIO_InitTypeDef GPIO_InitStructure;
 
 SystemInit();
 
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
 
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
 
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
 
  GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
 
  GPIO_Init(GPIOA,&GPIO_InitStructure);

 while(1)
 
{
 
 GPIO_SetBits(GPIOA,GPIO_Pin_0); 
 
 GPIO_ResetBits(GPIOA,GPIO_Pin_1);
 
 GPIO_ResetBits(GPIOA,GPIO_Pin_2);
 
 GPIO_SetBits(GPIOA,GPIO_Pin_3); 
 
 delay_ms(2);
 
 GPIO_SetBits(GPIOA,GPIO_Pin_0); 
 
 GPIO_ResetBits(GPIOA,GPIO_Pin_2);
 
 GPIO_ResetBits(GPIOA,GPIO_Pin_3);
 
 GPIO_SetBits(GPIOA,GPIO_Pin_1); 
 
 delay_ms(2);
 
 GPIO_SetBits(GPIOA,GPIO_Pin_1); 
 
 GPIO_ResetBits(GPIOA,GPIO_Pin_0);
 
 GPIO_ResetBits(GPIOA,GPIO_Pin_3);
 
 GPIO_SetBits(GPIOA,GPIO_Pin_2); 
 
 delay_ms(2);
 
 GPIO_SetBits(GPIOA,GPIO_Pin_2); 
 
 GPIO_ResetBits(GPIOA,GPIO_Pin_0);
 
 GPIO_ResetBits(GPIOA,GPIO_Pin_1);
 
 GPIO_SetBits(GPIOA,GPIO_Pin_3); 
 
  GPIO_ResetBits(GPIOA,GPIO_Pin_0);
 
  delay_ms(2);
 

 }
}
void delay_ms(u32 count)
{
 

 
int temp;
 
SysTick->CTRL=0x01;
 
SysTick->LOAD=25000*count;
 
SysTick->VAL=0x00;
 

 
do
 
{
 
 temp=SysTick->CTRL;
 
}while((temp&0x01)&&(!(temp&(1<<16))));
 

}

不知怎么搞的 stm32f4xx無法用軟件進(jìn)行仿真,下面的仿真圖是我用stm32f103進(jìn)行測(cè)試的,對(duì)于stm32開發(fā)板我弄了個(gè)視頻。


關(guān)閉窗口