原來(lái)步進(jìn)電機(jī)不適合用PWM控制,我用的是systick產(chǎn)生標(biāo)準(zhǔn)延時(shí),然后驅(qū)動(dòng)4個(gè)IO口產(chǎn)生交替變換的脈沖,先上程序,這次我用的是stm32f407discovery開(kāi)發(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無(wú)法用軟件進(jìn)行仿真,下面的仿真圖是我用stm32f103進(jìn)行測(cè)試的,對(duì)于stm32開(kāi)發(fā)板我弄了個(gè)視頻。
