一、硬件部分
热敏打印机附带一个步进电机,通过驱动步进电机可以让打印头进行滚动,步进电机转动的快慢影响打印机打印的速度。
本打印机驱动采用TC1508S芯片进行驱动,原理图如下,
四个引脚分别连接到MCU上,
OUTA+ --》PA11
OUTA- --》PA12
OUTB+ --》PB8
OUTB- --》PB9
二、软件部分
这里使用的是直接IO控制的,没有用到PWM
void bsp_stepperMotor_gpioConfig(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB2PeriphClk(BSP_STEPPER_MOTOR_A_RCC|BSP_STEPPER_MOTOR_B_RCC|BSP_STEPPER_MOTOR_C_RCC|BSP_STEPPER_MOTOR_D_RCC, ENABLE);
GPIO_InitStruct(&GPIO_InitStructure);
GPIO_InitStructure.Pin = BSP_STEPPER_MOTOR_A_PIN;
GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
GPIO_InitStructure.GPIO_Pull = GPIO_No_Pull;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitPeripheral(BSP_STEPPER_MOTOR_A_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = BSP_STEPPER_MOTOR_B_PIN;
GPIO_InitPeripheral(BSP_STEPPER_MOTOR_B_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = BSP_STEPPER_MOTOR_C_PIN;
GPIO_InitPeripheral(BSP_STEPPER_MOTOR_C_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = BSP_STEPPER_MOTOR_D_PIN;
GPIO_InitPeripheral(BSP_STEPPER_MOTOR_D_PORT, &GPIO_InitStructure);
}
typedef struct
{
uint8_t runningState;
}STEPPER_MOTOR_STRUCT;
static STEPPER_MOTOR_STRUCT stepperMotorStruct =
{
.runningState = 0,
};
uint8_t dri_stepperMotor_get_runningState(void)
{
return stepperMotorStruct.runningState;
}
void dri_stepperMotor_set_runningState(uint8_t state)
{
stepperMotorStruct.runningState = state;
}
void dri_stepperMotor_init(void)
{
bsp_stepperMotor_gpioConfig();
}
static void dri_stepperMotor_selectOn(uint8_t index)
{
switch(index)
{
case 0:
BSP_STEPPER_MOTOR_A_WRITE(1);
BSP_STEPPER_MOTOR_B_WRITE(0);
BSP_STEPPER_MOTOR_C_WRITE(0);
BSP_STEPPER_MOTOR_D_WRITE(0);
break;
case 1:
BSP_STEPPER_MOTOR_A_WRITE(0);
BSP_STEPPER_MOTOR_B_WRITE(1);
BSP_STEPPER_MOTOR_C_WRITE(0);
BSP_STEPPER_MOTOR_D_WRITE(0);
break;
case 2:
BSP_STEPPER_MOTOR_A_WRITE(0);
BSP_STEPPER_MOTOR_B_WRITE(0);
BSP_STEPPER_MOTOR_C_WRITE(1);
BSP_STEPPER_MOTOR_D_WRITE(0);
break;
case 3:
BSP_STEPPER_MOTOR_A_WRITE(0);
BSP_STEPPER_MOTOR_B_WRITE(0);
BSP_STEPPER_MOTOR_C_WRITE(0);
BSP_STEPPER_MOTOR_D_WRITE(1);
break;
case 0:
BSP_STEPPER_MOTOR_A_WRITE(1);
BSP_STEPPER_MOTOR_B_WRITE(0);
BSP_STEPPER_MOTOR_C_WRITE(1);
BSP_STEPPER_MOTOR_D_WRITE(0);
break;
case 1:
BSP_STEPPER_MOTOR_A_WRITE(1);
BSP_STEPPER_MOTOR_B_WRITE(0);
BSP_STEPPER_MOTOR_C_WRITE(0);
BSP_STEPPER_MOTOR_D_WRITE(1);
break;
case 2:
BSP_STEPPER_MOTOR_A_WRITE(0);
BSP_STEPPER_MOTOR_B_WRITE(1);
BSP_STEPPER_MOTOR_C_WRITE(0);
BSP_STEPPER_MOTOR_D_WRITE(1);
break;
case 3:
BSP_STEPPER_MOTOR_A_WRITE(0);
BSP_STEPPER_MOTOR_B_WRITE(1);
BSP_STEPPER_MOTOR_C_WRITE(1);
BSP_STEPPER_MOTOR_D_WRITE(0);
break;
}
}
void dri_stepperMotor_runTaskHandler(void)
{
static uint8_t step = 0;
if(stepperMotorStruct.runningState)
{
dri_stepperMotor_selectOn(step++);
if(step == 4)
step = 0;
}else
{
step = 0;
}
}
void dri_stepperMotor_runSpecifyTurnNum(uint16_t speed,uint8_t turnNum)
{
static uint8_t step = 0;
while(turnNum)
{
dri_stepperMotor_selectOn(step++);
DRI_PRINTER_DELAY_US(speed);
if(step == 4)
step = 0;
turnNum --;
}
}
`
三、现象
通过调用
dri_stepperMotor_runSpecifyTurnNum(1500,2);
可以观察到步进电机就会转起来。
|