原來步進電機不適合用PWM控制,我用的是systick產生標準延時,然后驅動4個IO口產生交替變換的脈沖,先上程序,這次我用的是stm32f407discovery開發板。
#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無法用軟件進行仿真,下面的仿真圖是我用stm32f103進行測試的,對于stm32開發板我弄了個視頻。
