|
我是单片机新手,自学中,请给我赐教!我想做成按K1一下,就一直正转,按K2一下就一直反转,按K3停止!现在只有点动,我不知如何让正转或反转无限循环。
/*******************************************************
ULN2003驱动5V减速步进电机程序
Target:STC89C52RC-40C
Crystal:12MHz
Platform:51&avr单片机最小系统板+ULN2003步进电机驱动套件
*******************************************************
接线方式:
IN1 ---- P00
IN2 ---- P01
IN3 ---- P02
IN4 ---- P03
+ ---- +5V
- ---- GND
*********************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define MotorData P0 //步进电机控制接口定义
sbit K1=P1^0 ;
sbit K2=P1^1;
sbit K3=P1^2;
//uchar phasecw[4] ={0x08,0x04,0x02,0x01};//正转 电机导通相序 D-C-B-A
//uchar phaseccw[4]={0x01,0x02,0x04,0x08};//反转 电机导通相序 A-B-C-D
uchar phasecw[8] ={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};//正转 电机导通相序 AD-D-CD-C-BC-B-AB-A
uchar phaseccw[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};//反转 电机导通相序 A-AB-B-BC-C-CD-D-DA
//ms延时函数
void Delay_xms(uint x)
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<112;j++);
}
//顺时针转动
void MotorCW(void)
{
uchar i;
for(i=0;i<8;i++)
{
MotorData=phasecw[i];
Delay_xms(4);//转速调节
}
}
//逆时针转动
void MotorCCW(void)
{
uchar i;
for(i=0;i<8;i++)
{
MotorData=phaseccw[i];
if (i>8) i=0;
Delay_xms(4);//转速调节
}
}
//停止转动
void MotorStop(void)
{
MotorData=0x00;
}
//主函数
void main(void)
{
Delay_xms(50);//等待系统稳定
while(1)
{
if (K1==1)
{
Delay_xms(50);
if (K1==1)//for(i=0;i<500;i++)
MotorCW(); //顺时针转动
}
if (K3==1 )
{
Delay_xms(50);
if (K3==1) MotorStop(); //停止转动
}
if (K2==1 )
{
Delay_xms(50);
if (K2==1)
MotorCCW(); //逆时针转动
}
}
}
|
|