K009 基于51/32 MAX1508双H桥电机驱动模块测试
一. 实现功能
按键 K_Start_Stop停止和启动电机,默认上电停止 按键 K_Syntropy_Reverse 切换两电机转向 按键 K_UP_Speed 加速按键,按下加速 按键 K_Down_Speed 减速按键,按下减速 此程序主要基于STM32F103C8T6驱动MAX1508电机一些动作,动作改变是用四个按键控制两个电机启动停止,同向反向和加减速。
二. 硬件清单
- MAX1508双H桥电机驱动模块
- STM32F103C8T6/STC89C52RC
- SWD或JLINK仿真器(直接用CH340串口模块烧录也行,不过注意配置BOOT)
- 直流电机马达(2)
- 4位按键模块
- 电源8V左右(这里用的是两节18650)
三. 资料清单
程序代码
文档资料
四. MAX1508双H桥电机驱动模块简介
- 双路1.5A*2,峰值电流可到2.5A, 内置过热保护电路,不用怕电机堵转烧坏,温度下降后自动恢复。
2.直流电机驱动逻辑真值表:
产品参数: 1.双路H桥电机驱动,可以同时驱动两路直流电机或者1个4线两相式步进电机; 2.模块供电电压2V-10V; 3.信号端输入电压1.8-7V; 4.单路工作电流1.5A,峰值电流可达2.5A,低待机电流 (小于 0.1uA); 5.内置防共态导通电路,输入端悬空时,电机不会误动作; 6.内置带迟滞效应的过热保护电路 (TSD),无需担心电机堵转; 7.产品尺寸: 24.7215mm (长宽高),超小体积,适合组装和车载; 8.安装孔直径:2 mm。 9.重量:5g 注意事项: 1.电源正极与负极接反肯定会造成电路损坏。 2.输出对地短路或输出端短路,还有电机堵转的情况下,芯片都会热保护的,但是在接近或者超过10V电压且峰值电流大大超过2.5A的情况下也会造成芯片烧毁。
五. 接线
六.代码说明
以下以32代码为例,
- IN1-IN4引脚配置
void GENERAL_TIM_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(GENERAL_TIM_CH1_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = GENERAL_TIM_CH1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GENERAL_TIM_CH1_PORT, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(GENERAL_TIM_CH2_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = GENERAL_TIM_CH2_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GENERAL_TIM_CH2_PORT, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(GENERAL_TIM_CH3_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = GENERAL_TIM_CH3_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GENERAL_TIM_CH3_PORT, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(GENERAL_TIM_CH4_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = GENERAL_TIM_CH4_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GENERAL_TIM_CH4_PORT, &GPIO_InitStructure);
}
- 定时器与硬件PWM配置
void GENERAL_TIM_Mode_Config(void)
{
GENERAL_TIM_APBxClock_FUN(GENERAL_TIM_CLK,ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period=GENERAL_TIM_Period;
TIM_TimeBaseStructure.TIM_Prescaler= GENERAL_TIM_Prescaler;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(GENERAL_TIM, &TIM_TimeBaseStructure);
uint16_t CCR1_Val = 0;
uint16_t CCR2_Val = 0;
uint16_t CCR3_Val = 0;
uint16_t CCR4_Val = 0;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OC1Init(GENERAL_TIM, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(GENERAL_TIM, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(GENERAL_TIM, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(GENERAL_TIM, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
TIM_Cmd(GENERAL_TIM, ENABLE);
}
void GENERAL_TIM_Init(void)
{
GENERAL_TIM_GPIO_Config();
GENERAL_TIM_Mode_Config();
}
- 按键引脚配置
void Key_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = K_Start_Stop;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = K_Syntropy_Reverse;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = K_UP_Speed;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = K_Down_Speed;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
- 按键扫描函数
void Scan_key(void)
{
uint16_t Key_Dat=0;
Key_Dat=GPIO_ReadInputData(GPIOB);
Key_Dat=Key_Dat&0x00F0;
Key_Dat=Key_Dat^0x00F0;
Key_Dat_Buf=Key_Dat;
if(Key_Dat==K_Start_Stop)
{
DelayMs(10);
if(Key_Dat==K_Start_Stop)
{
Flag_Start_Stop=~Flag_Start_Stop;
}
while(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4));
}
else if(Key_Dat==K_Syntropy_Reverse)
{
DelayMs(10);
if(Key_Dat==K_Syntropy_Reverse)
{
Flag_Syntropy_Reverse=~Flag_Syntropy_Reverse;
}
while(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5));
}
else if(Key_Dat==K_UP_Speed)
{
DelayMs(10);
if(Key_Dat==K_UP_Speed)
{
Speed=Speed+5;
if(Speed>=999)
{
Speed=999;
}
}
while(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6));
}
else if(Key_Dat==K_Down_Speed)
{
DelayMs(10);
if(Key_Dat==K_Down_Speed)
{
Speed=Speed-5;
if(Speed<=10)
{
Speed=10;
}
}
while(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7));
}
}
- 电机驱动函数
void turn_Syntropy()
{
TIM_SetCompare1(GENERAL_TIM, Speed);
TIM_SetCompare2(GENERAL_TIM, 0);
TIM_SetCompare3(GENERAL_TIM, Speed);
TIM_SetCompare4(GENERAL_TIM, 0);
}
void turn_Reverse()
{
TIM_SetCompare1(GENERAL_TIM, 0);
TIM_SetCompare2(GENERAL_TIM, Speed);
TIM_SetCompare3(GENERAL_TIM, 0);
TIM_SetCompare4(GENERAL_TIM, Speed);
}
void turn_Stop()
{
TIM_SetCompare1(GENERAL_TIM, 0);
TIM_SetCompare2(GENERAL_TIM, 0);
TIM_SetCompare3(GENERAL_TIM, 0);
TIM_SetCompare4(GENERAL_TIM, 0);
}
- 主函数
int main(void)
{
GENERAL_TIM_Init();
DelayInit();
Key_GPIO_Init();
turn_Stop();
while(1)
{
Scan_key();
if(Flag_Start_Stop)
{
if(!Flag_Syntropy_Reverse)
{
turn_Syntropy();
}
else
{
turn_Reverse();
}
}
else
{
turn_Stop();
}
}
}
七.资料获取
加群私聊群主可免费获得,群号:1041406448。
|