/* 两个ULN2003驱动两个5线4相步进电机 */
/* 步进电机四相端口*/
#define pMotor_A_A_Init() GPIO_Init(GPIOC,GPIO_PIN_7,GPIO_MODE_OUT_PP_LOW_FAST)
#define pMotor_A_B_Init() GPIO_Init(GPIOC,GPIO_PIN_6,GPIO_MODE_OUT_PP_LOW_FAST)
#define pMotor_A_C_Init() GPIO_Init(GPIOC,GPIO_PIN_5,GPIO_MODE_OUT_PP_LOW_FAST)
#define pMotor_A_D_Init() GPIO_Init(GPIOC,GPIO_PIN_3,GPIO_MODE_OUT_PP_LOW_FAST)
#define pMotor_B_A_Init() GPIO_Init(GPIOD,GPIO_PIN_0,GPIO_MODE_OUT_PP_LOW_FAST)
#define pMotor_B_B_Init() GPIO_Init(GPIOD,GPIO_PIN_2,GPIO_MODE_OUT_PP_LOW_FAST)
#define pMotor_B_C_Init() GPIO_Init(GPIOD,GPIO_PIN_3,GPIO_MODE_OUT_PP_LOW_FAST)
#define pMotor_B_D_Init() GPIO_Init(GPIOD,GPIO_PIN_4,GPIO_MODE_OUT_PP_LOW_FAST)
/* A AB B BC C CD D DA */
uint8_t Beat_A_Code[8] = {0x80,0xC0,0x40,0x60,0x20,0x28,0x08,0x88}; //步进电机A四相八拍驱动
/* A AB B BC C CD D DA */
uint8_t Beat_B_Code[8] = {0x01,0x05,0x04,0x0C,0x08,0x18,0x10,0x11}; //步进电机B四相八拍驱动
int16_t motor_A_Beats = 0;
int16_t motor_B_Beats = 0;
uint8_t index_A = 0;
uint8_t index_B = 0;
#pragma vector=0x19
__interrupt void TIM4_UPD_OVF_IRQHandler(void) //定时中断 1ms
{
/* 步进电机A转动控制流量值大于4L */
if(motor_A_Beats != 0)
{
if(motor_A_Beats > 0) //正转
{
index_A++;
index_A &= 0x07;
motor_A_Beats--;
}
else //反转
{
index_A--;
index_A &= 0x07;
motor_A_Beats++;
}
temp0 = GPIOC->ODR;
temp0 &= 0x17;
temp0 |= Beat_A_Code[index_A];
GPIOC->ODR = temp0;
}
else
{
GPIOC->ODR &= 0x17;
}
/* 步进电机B转动控制流量值小于等于4L */
if(motor_B_Beats != 0)
{
if(motor_B_Beats > 0) //正转
{
index_B++;
index_B &= 0x07;
motor_B_Beats--;
}
else //反转
{
index_B--;
index_B &= 0x07;
motor_B_Beats++;
}
temp2 = GPIOD->ODR;
temp2 &= 0xE2;
temp2 |= Beat_B_Code[index_B];
GPIOD->ODR = temp2;
}
else
{
GPIOD->ODR &= 0xE2;
}
TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
} |