分享~
单片机源程序如下:
- /*-----------------------------------------------
- 名称: 直流电机控制
- 工作室: 翼嵌电子工作室
- 编写: Bin.Lee
- 日期: 2018.6.10
- 版本: 0.1
- 内容: 直流电机的PWM速度控制程序
- 端口: IN_1 = P2^0;
- IN_2 = P2^1;
- IN_3 = P2^2;
- IN_4 = P2^3;
- EN_A = P2^4;
- EN_B = P2^5;
- ------------------------------------------------*/
- /* 电机控制函数 index-电机号(1,2); speed-电机速度(-100—100) direction-正反转(1,0)*/
- #include "Motor.h"
- sbit IN_1 = P2^0; /* L298的IN_1 */
- sbit IN_2 = P2^1; /* L298的IN_2 */
- sbit IN_3 = P2^2; /* L298的IN_3 */
- sbit IN_4 = P2^3; /* L298的IN_4 */
- sbit EN_A = P2^4; /* L298的EN_A */
- sbit EN_B = P2^5; /* L298的EN_B */
- /**********电机驱动****************/
- uchar count = 0; /* 中断计数器 */
- uchar Speed_1 = 0, Speed_2 = 0; /* 电机1/2速度值 */
- uchar Speed_Now_1 = 0, Speed_Now_2 = 0; /* 电机1/2当前速度值 */
- void Motor_Delay_ms(unsigned int time)
- {
- uchar i,j;
- for(i=time;i>0;i++)
- for(j=112;j>0;j--)
- {;}
- }
- void Motor(uchar index, char speed,uchar direction)
- {
- if(index == 1) /* 电机1的处理 */
- {
- if(direction == 1)
- {
- Speed_1 = speed;
- IN_1 = 1;
- IN_2 = 0;
- }
- else
- {
- Speed_1 = speed;
- IN_1 = 0;
- IN_2 = 1;
- }
- }
- if(index == 2) /* 电机2的处理 */
- {
- if(direction == 1)
- {
- Speed_2 = speed;
- IN_3 = 1;
- IN_4 = 0;
- }
- else
- {
- Speed_2 = speed; /* 电机2的速度控制 */
- IN_3 = 0;
- IN_4 = 1;
- }
- }
- }
- void Motor_Control_1(char speed, uchar direction)/* 电机1的控制 */
- {
- Motor(1, speed, direction);
- }
- void Motor_Control_2(char speed, uchar direction)/* 电机2的控制 */
- {
- Motor(2, speed, direction);
- }
- void Motor_Init(void)
- {
- IN_1 = 1;
- IN_2 = 1;
- IN_3 = 1;
- IN_4 = 1;
- TMOD = 0x02; /* 设定T0的工作模式为2 */
- TH0 = 0xa3; /* 装入定时器的初值 */
- TL0 = 0xa3; //0.1ms
- EA = 1; /* 开中断 */
- ET0 = 1; /* 定时器0允许中断 */
- TR0 = 1; /* 启动定时器0 */
- }
- /*********************行驶状态**************************/
- void Motor_Run(void)
- {
- TR0 = 1; /* 启动定时器0 */
- Motor_Delay_ms(2);
- Motor_Control_1(13, 1);
- Motor_Control_2(13, 0);
- }
- void Motor_Back(void)
- {
- TR0 = 1; /* 启动定时器0 */
- Motor_Delay_ms(2);
- Motor_Control_1(30, 0);
- Motor_Control_2(30, 1);
- }
- void Motor_Stop(void)
- {
- Motor_Delay_ms(2);
- TR0 = 0; /* 关闭定时器0 */
- EN_A = 0;
- EN_B = 0;
- IN_1 = 0;
- IN_2 = 0;
- IN_3 = 0;
- IN_4 = 0;
- }
- void Motor_TurnRight(void) //右转
- {
- TR0 = 1;
- Motor_Delay_ms(2);
- Motor_Control_1(15, 1);
- Motor_Control_2(10, 1);
- }
- void Motor_TurnLeft(void) //左转
- {
- TR0 = 1;
- Motor_Delay_ms(2);
- Motor_Control_1(10, 0);
- Motor_Control_2(15, 0);
- }
- void Motor_Around(void) //打转
- {
- TR0 = 1; /* 启动定时器0 */
- Motor_Delay_ms(2);
- Motor_Control_1(10, 1);
- Motor_Control_1(10, 1);
- }
- void TIME_0(void) interrupt 1 /* T0中断服务程序 */
- {
- if(count == 0) /* 1个PWM周期完成后才会接受新数值 */
- {
- Speed_Now_1 = Speed_1;
- Speed_Now_2 = Speed_1;
- }
- /* 产生电机1的PWM信号 */
- if(count < Speed_1) EN_A = 1;
- else EN_A = 0;
- /* 产生电机2的PWM信号 */
- if(count < Speed_2) EN_B = 1;
- else EN_B = 0;
- count++;
- if(count >= 100) count = 0; /* 1个PWM信号由100次中断产 */
- }
复制代码
所有资料51hei提供下载:
L298电机驱动程序.rar
(36.61 KB, 下载次数: 63)
|