|
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit A0= P1^0;
sbit A1 =P1^1;
sbit B0 =P1^2;
sbit B1 =P1^3;
sbit P1_4=P1^4;
sbit P1_5=P1^5;
sbit P1_6=P1^6;
uchar su_du=9; //转速
bit fang_xiang=0; //方向
bit qi_dong=1; //启动标志
code uchar shu_ma[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,2,0xf8,0,0x10}; //数码管字段编码
void yanshi2(uint a) //延时程序
{
while(a--);
}
uchar an_jian() //按键检测
{
{
yanshi2(20000);
if((P1&0x0f)!=0x0f)
{
return P1&0x0f;
}
}
return 0x0f;
}
void yanshi() //步进电机延时时间
{
uint sd1=0;
sd1=8000+5753*(10-su_du);
while(sd1--)
{
if((P1&0x0f)!=0x0f)return; //在延时里面判断有没有按键按下,有就直接返回
}
}
void bu_jin() //每调用一次步进电机向指定方向走一步
{
static char j=1;
if(fang_xiang) //判断方向
{
j++;
if(j==5)j=1;
}
else
{
j--;
if(j==0)j=4;
} P1_6 = 0;
switch(j) //根据拍数选择不同的电压组合
{
case 1:
A0=1;B0=1;A1=0;B1=0;yanshi(); //ab
break;
case 2:
A0=0;B0=1;A1=1;B1=0;yanshi(); //a~b
break;
case 3:
A0=0;B0=0;A1=1;B1=1;yanshi(); //a~b~
break;
case 4:
A0=1;B0=0;A1=0;B1=1;yanshi(); //ab~
break;
}
}
void xian_shi() //显示子程序
{
P2=shu_ma[su_du];//把速度显示到数码管
}
void main()
{
while(1){
if (P1_4)
{
qi_dong=1;
fang_xiang=0;
if(su_du<9)su_du++;
}
else
{
qi_dong=0;
fang_xiang=1;
if(su_du<9)su_du--;
}
xian_shi(); //显示
if(qi_dong)
{
bu_jin(); //步进电机
}
}
}
|
|