标签:
/*stepmotor.h*/ #ifndef _STEPMOTOR_H #define _STEPMOTOR_H #include "sys.h" #include "delay.h" #define AA PDout(8) #define aa PDout(9) #define BB PDout(10) #define bb PDout(11) //单4 #define ON_1 {AA=1;aa=0;BB=0;bb=0;} #define ON_2 {AA=0;aa=0;BB=0;bb=1;} #define ON_3 {AA=0;aa=1;BB=0;bb=0;} #define ON_4 {AA=0;aa=0;BB=1;bb=0;} //双4 #define ON_D1 {AA=0;aa=1;BB=1;bb=0;} #define ON_D2 {AA=1;aa=0;BB=1;bb=0;} #define ON_D3 {AA=1;aa=0;BB=0;bb=1;} #define ON_D4 {AA=0;aa=1;BB=0;bb=1;} //单双8拍 #define ON_DD1 {AA=0;aa=1;BB=0;bb=0;} #define ON_DD2 {AA=0;aa=1;BB=1;bb=0;} #define ON_DD3 {AA=0;aa=0;BB=1;bb=0;} #define ON_DD4 {AA=1;aa=0;BB=1;bb=0;} #define ON_DD5 {AA=1;aa=0;BB=0;bb=0;} #define ON_DD6 {AA=1;aa=0;BB=0;bb=1;} #define ON_DD7 {AA=0;aa=0;BB=0;bb=1;} #define ON_DD8 {AA=0;aa=1;BB=0;bb=1;} #define OFF_ALL {AA=0;aa=0;BB=0;bb=0;}
extern void Motor_GPIO_Init(void); extern void Motor_4(u16 time); extern void Motor_D4(u16 time); extern void Motor_8(u16 time); extern void Motor_8_back(u16 time); //反转 #endif
/*stepmotor.c*/ #include "stepmotor.h" void Motor_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); //GPIO_SetBits(GPIOD,GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11); } void Motor_4(u16 time) { ON_1; delay_ms(time); ON_2; delay_ms(time); ON_3; delay_ms(time); ON_4; delay_ms(time); } void Motor_D4(u16 time) { ON_D1; delay_ms(time); ON_D2; delay_ms(time); ON_D3; delay_ms(time); ON_D4; delay_ms(time); } void Motor_8(u16 time) //执行一次转7.2度 {//循环50次转360度,8拍每拍0.9度 ON_DD1; delay_us(time); ON_DD2; delay_us(time); ON_DD3; delay_us(time); ON_DD4; delay_us(time); ON_DD5; delay_us(time); ON_DD6; delay_us(time); ON_DD7; delay_us(time); ON_DD8; delay_us(time); } void Motor_8_back(u16 time) //反转 { ON_DD8; delay_us(time); ON_DD7; delay_us(time); ON_DD6; delay_us(time); ON_DD5; delay_us(time); ON_DD4; delay_us(time); ON_DD3; delay_us(time); ON_DD2; delay_us(time); ON_DD1; delay_us(time); }
标签:
原文地址:http://www.cnblogs.com/ht-beyond/p/4304649.html