📄 motor_driver.c
字号:
//####################################################################################
//###### name || writer || time || place ||#########################
//######-----------------------------------------------------#########################
//######motor_driver.c || freela || 2008/10/14|| 暨南大学 ||#########################
//####################################################################################
#include <at89x52.h>
#define left 0
#define right 1
#define high 1
#define low 0
#define uchar unsigned char
#define uint unsigned int
/***************************/
void mdelay(uint i);
/*****步进电机驱动函数*******/
void step_motor_run(uchar direction,uchar steps,uint ms_per_step,uchar HL)
{
//direction 为方向,steps为步数,ms_per_step为每步用时ms数
// HL选择用P2口的高4位还是低4位控制。
static uchar i=0;
static uchar j=4;
uchar code data1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
if(HL==low)
{
if(direction==0)
{
for(;steps!=0;steps--)
{ P2=data1[i];
mdelay(ms_per_step);
if(i==3)i=0;
else i++;
}
}
else
{ for(;steps!=0;steps--)
{
P2=data1[i];
mdelay(ms_per_step);
if(i==0)i=3;
else i--;
}
}
}
else
{
if(direction==0)
{
for(;steps!=0;steps--)
{
P2=data1[j];
mdelay(ms_per_step);
if(j==7)j=4;
else j++;
}
}
else
{
for(;steps!=0;steps--)
{
P2=data1[j];
mdelay(ms_per_step);
if(j==4)j=7;
else j--;
}
}
}
P2=0xff;
}
/*****P3_7/P3_6控制直流电机*******/
void DCmotor1(uchar direction,uint ms_time)
{
if(direction==right)
{
P1_0=0;
P1_1=1;
mdelay(ms_time);
}
else
{ P1_1=0;
P1_0=1;
mdelay(ms_time);
}
P1_1=P1_0;
}
/******P3_5/P3_4***************/
void DCmotor2(uchar direction,uint ms_time)
{
if(direction==right)
{
P3_5=0;
P3_4=1;
mdelay(ms_time);
}
else
{ P3_4=0;
P3_5=1;
mdelay(ms_time);
}
P3_4=P3_5;
}
/******P1_0/P1_1***************/
void DCmotor3(uchar direction,uint ms_time)
{
if(direction==right)
{
P3_0=0;
P3_1=1;
mdelay(ms_time);
}
else
{ P3_1=0;
P3_0=1;
mdelay(ms_time);
}
P3_0=P3_1;
}
/*****1ms延时函数*****/
void mdelay(uint i)
{
uchar j=0;
for(;i!=0;i--)
for(j=124;j!=0;j--);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -