硬件设计
链接:https://pan.baidu.com/s/1SPLieDrdBp6VHkx697xINw
提取码:1rjf
代码设计
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code FFW[]=
{
0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09
}; //正转
uchar code REV[]=
{
0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01
}; //反转
sbit K1 = P3^0;
sbit K2 = P3^1;
sbit K3 = P3^2;
void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void SETP_MOTOR_FFW(uchar n) //电机正转函数,n为圈数
{
uchar i,j;
for(i=0;i<5*n;i++)
{
for(j=0;j<8;j++)
{
if(K3 == 0) break; //检测K3是否按下,如果按下则停止转动
P1 = FFW[j];
DelayMS(25);
}
}
}
void SETP_MOTOR_REV(uchar n) //电机反转函数,n为圈数
{
uchar i,j;
for(i=0;i<5*n;i++)
{
for(j=0;j<8;j++)
{
if(K3 == 0) break;
P1 = REV[j];
DelayMS(25);
}
}
}
void main()
{
uchar N = 3; //转3圈
while(1)
{
if(K1 == 0)
{
P0 = 0xfe; //正转指示灯点亮
SETP_MOTOR_FFW(N);
if(K3 == 0) break;
}
else if(K2 == 0)
}
}