#include <reg51.h>
#define uint unsigned int;
#define uchar unsigned char
//以下定义LED片选信号
sbit LED_1 = P3^0;
sbit LED_2 = P3^1;
sbit LED_3 = P1^6;
sbit LED_4 = P1^7;
//以下定义M1驱动信号
sbit M1_1 = P2^0;
sbit M1_2 = P2^1;
sbit M1_3 = P2^2;
sbit M1_4 = P2^3;
//以下定义M2驱动信号
sbit M2_1 = P2^4;
sbit M2_2 = P2^5;
sbit M2_3 = P2^6;
sbit M2_4 = P2^7;
//以下定义M3驱动信号
sbit M3_1 = P3^4;
sbit M3_2 = P3^5;
sbit M3_3 = P3^6;
sbit M3_4 = P3^7;
//定义输入按键的序号
sbit K_1 = P1^0;
sbit K_2 = P1^1;
sbit K_3 = P1^2;
sbit K_4 = P1^3;
sbit K_5 = P1^4;
sbit K_6 = P1^5;
//以下定义共阴极LED的显示代码,0~9
uchar code tab[10] =
{ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f };
//以下定义电机正转代码
uchar code zheng[9] =
{ 0x00,0x03,0x02,0x06,0x04,0x0c,0x08,0x09,0x01 };
//以下定义电机反转代码
uchar code fan[9] =
{ 0x01,0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x00 };
//下面定义全局变量
uchar m1_zheng = 0;//主轴正转,用1表示,停止用0表示
uchar m1_zheng_val = 0;//正转的步进
uchar m1_fan = 0;//主轴反转,用2表示,停止用0表示
uchar m1_fan_val = 0;//反转步进
uchar m2_mode = 0;//m2状态 X轴增加时用0,递减时用1
uchar m2_zheng_val = 0;//m2 X轴递增时的位置
uchar m2_fan_val = 0;//m2 X轴递减时的位置
uchar m2_pre_mode = 0;//存储m2前一次的状态,递增为0,递减为1
uchar m3_mode = 0;//m3状态 Y轴增加时用0,递减时用1
uchar m3_zheng_val = 0;//m3 Y轴递增时的位置
uchar m3_fan_val = 0;//m3 Y轴递减时的位置
uchar m3_pre_mode = 0;//存储m3前一次的状态,递增为0,递减为1
uchar inc_dec = 0;//递增为0,递减为1
uchar choose_motor = 0;//电机驱动选择,1为驱动M1,2为驱动M2,3为驱动M3
//下面是 函数声明
extern void delay(uchar time);//延时程序
void init_cpu(void);//初始化51单片机
void led_show(void);//LED显示程序
void int0_isr();//中断处理程序
void motor_driver();//电机控制程序
void key_1();//KEY_1处理程序,既主轴正转程序
void key_2();//主轴反转程序
void key_3();//M2处理程序,既X轴电机
void key_4();//M3处理程序,既Y轴电机
void key_5();//设置X轴或Y轴位置递增或递减程序
void key_6();//未定义,留待功能扩展
//主函数开始
void main()
{
init_cpu();//初始化单片机
while(1)
{
led_show(); //显示当前电机状态
}
}
void init_cpu()
{
EA = 0;//关所有中断
IT0 = 0;//下降沿触发
EX0 = 1;//允许INT0中断
EA = 1;//开所有中断
}
void led_show()
{
uchar i;
LED_1 = 0;//显示第一个LED,既M1的状态
if(m1_zheng == 1) //如果是正传,i = 1
i = 1;
else if (m1_fan == 2)//如果是反转,i = 2
i = 2;
else
i = 0; //如果是停止,i = 0
P0 = tab[i];
delay(20);//延时20ms
LED_1 = 1;
LED_2 = 0;//显示第二个LED,既M2的状态
i = m2_mode;
P0 = tab[i];
delay(20);//延时20ms
LED_2 = 1;
LED_3 = 0;//显示第三个LED,既M3的状态
i = m3_mode;
P0 = tab[i];
delay(20);
LED_3 = 1;
}
void int0_isr() interrupt 0 using 0
{ EX0 = 0;
K_1 = 1;//读数前先置1
if (K_1 == 0) key_1();//key_1按下,则调用key_1()
K_2 = 1;//读数前先置1
if (K_2 == 0) key_2();//key_2按下,则调用key_2()
K_3 = 1;//读数前先置1
if (K_3 == 0) key_3();//key_3按下,则调用key_3()
K_4 = 1;//读数前先置1
if (K_4 == 0) key_4();//key_4按下,则调用key_4()
K_1 = 1;//读数前先置1
if (K_5 == 0) key_5();//key_5按下,则调用key_5()
K_6 = 1;//读数前先置1
if (K_6 == 0) key_6();//key_6按下,则调用key_6()
EX0 = 1;
}
void key_1()
{
m1_zheng = 1;//表示主轴正转
m1_fan = 0;//清除反转标志
while( K_1 == 0 )//KEY_1键一直按着,则一直循环显示正转信号和主轴正转
{
led_show();
choose_motor = 1;//驱动M1
motor_driver();
if (m1_zheng_val <= 7 )
m1_zheng_val++;
else
m1_zheng_val = 0;
}
}
void key_2()
{
m1_zheng = 0;//清除正转标志
m1_fan = 2;//表示反转
while( K_2 == 0 )//KEY_2键一直按着,则循环显示反转信号和主轴反转
{
led_show();
choose_motor = 1;//驱动M1
motor_driver();
if (m1_fan_val <= 7 )
m1_fan_val++;
else
m1_fan_val = 0;
}
}
void key_3()
{
if( inc_dec == 0)//如果是递增的,
m2_mode = 0;//m2X轴位置递增
else
m2_mode = 1;//m2X轴位置递减
if ( m2_mode != m2_pre_mode ) //判断前一次和这次不都是递增或者递减
{
if (inc_dec == 0) m2_zheng_val=0;//这次递增,前一次递减则m2_zheng_val清0
else m2_fan_val = 0;//这次递减,前一次递增,则m2_fan_val清0
}
led_show();
choose_motor = 2;//驱动M2
motor_driver();
if( (inc_dec == 0) && (m2_zheng_val <= 8 ) )//如果是递增的,且m2_zheng_val <= 7
m2_zheng_val++;
if ( m2_zheng_val == 9 )m2_zheng_val = 0;//m2_zheng_val等于9则清0
if( (inc_dec == 1 ) && (m2_fan_val <= 8 ))//如果是 递减,且m2_fan_val <= 7
m2_fan_val++;
if ( m2_fan_val == 9 )m2_fan_val = 0;//m2_fan_val等于9则清0
while( K_3 == 0 ){;}//当KEY_3还按着时,等待,直到KEY_3松开
}
void key_4()
{
if (inc_dec == 0 )//如果是递增的,则m3_y加1,否则减1
m3_mode = 0;//m3 y轴位置递增
else
m3_mode = 1;//m3 y轴位置递减
led_show();
choose_motor = 3;//驱动M3
motor_driver();
if( (inc_dec == 0) && (m3_zheng_val <= 8 ) )//如果是递增的,且m3_zheng_val <= 7
m3_zheng_val++;
if ( m3_zheng_val == 9 )m3_zheng_val = 0;//m3_zheng_val等于9则清0
if( (inc_dec == 1 ) && (m3_fan_val <= 8 ))//如果是 递减,且m3_fan_val <= 7
m3_fan_val++;
if ( m3_fan_val == 9 )m3_fan_val = 0;//m3_fan_val等于9则清0
while( K_4 == 0 );//KEY_4未松开,等待
}
void key_5()
{
uchar i ;
i = (inc_dec ^ 0x0ff );//inc_dec中的值与0x0ff异或,值变反
inc_dec = i;
while( K_5 == 0 ){;}//KEY_5未松开,等待
}
void key_6()
{
;}
void motor_driver()
{
uchar j;
if (choose_motor == 1 )//驱动M1
{
if(m1_zheng == 1)//如果是 正转
j = zheng[m1_zheng_val];
if(m1_fan == 2 )//如果是 反转
j = fan[m1_fan_val];
M1_1 = j^0;
M1_2 = j^1;
M1_3 = j^2;
M1_4 = j^3;
}
if (choose_motor == 2 )//驱动M2
{
if(m2_mode == 0 )//正转,+X
j = zheng[m2_zheng_val];
if(m2_mode == 1 )//反转,-X
j = fan[m2_fan_val];
M2_1 = j^0;
M2_2 = j^1;
M2_3 = j^2;
M2_4 = j^3;
}
if (choose_motor == 3 )//驱动M3
{
if(m3_mode == 0 )//正转,+X
j = zheng[m3_zheng_val];
if(m3_mode == 1 )//反转,-X
j = fan[m3_fan_val];
M3_1 = j^0;
M3_2 = j^1;
M3_3 = j^2;
M3_4 = j^3;
}
}