设备 | 参数 | 数量 | 备注 |
---|---|---|---|
上位机程序 | $Visual basic 6.0 | 1 | 用于控制电机运转 |
串口线 | RS232 | 1 | 传输数据:Visual basic6.0 →单片机 |
单片机 | STC89C52RC | 1 | 引脚控制电机 |
数据线 | 数根 | 传输0-1 | |
步进电机 | 42电机 | 1 | 可用42/57,均可 |
#include
#include
#include
#include
#define uint16 unsigned int
#define uchar8 unsigned char
uchar8 InputValue,k;
void UartInit();
void ReadInputValue();
void Timer1Init();
void Display();
void main()
{
Motor2Init();
timer0init();
UartInit();
MotorSetting();
while(1)
{
Display();
}
}
//
Uart初始化
void UartInit()
{
SCON=0x50; //10位异步收发(8位数据),由定时器1控制 SM1=1 REN=1(允许接收)
PCON&=0x7f; //修改倍率,使SMOD=0;
TMOD&=0x0f; //不改变定时器0的操作状态
TMOD|=0x20; //设置定时器1工作方式为2,8位自动装初值
TH1=0xfd; //波特率计算公式256-
TL1=0xfd;
EA=1; //打开总中断 (校长说学生放假)
ES=1; //串口允许中断 (班主任说学生放假)
ET1=0; //定时器1 关闭中断 (禁止其他人发言,放假不放假说了不算)
TR1=1; //定时器1 启动 (开启放假)
}
//
//Uart自定义
void intteruptUart() interrupt 4
{
if(RI==1)
{
RI=0;
InputValue=SBUF;
ReadInputValue();
}
if(TI==1)
{
TI=0;
}
}
//
//ReadInputValue()
void ReadInputValue()
{
if(InputValue>=100)
{
k=InputValue-100;
}
ValueTable[k]=InputValue;
MotorDIR=ValueTable[0];
RevolutionTarget=ValueTable[2];
MotorSpeed=SpeedTable[ValueTable[1]];
MotorFlag=ValueTable[sizeof ValueTable-1];
}
void Display()
{
shumaguan(0,i);
shumaguan(1,ValueTable[i]/10);
shumaguan(2,ValueTable[i]%10);
}
void delayms(unsigned int xms)
{
unsigned char y;
unsigned int x;
for(x=xms;x>0;x--)
{
for(y=110;y>0;y--)
{
}
}
}
sbit shumaguan1=P2^2;
sbit shumaguan2=P2^3;
sbit shumaguan3=P2^4;
unsigned char code tableduan[]={
0xAF,0x21,0xCD,0x6D,0x63,0x6E,0xEE,0x25,0xEF,0x6F, //10,0-9 without point
0xE7,0xEA,0x8E,0xE9,0xCE,0xC6, //6,A-F without point
0xBF,0x31,0xDD,0x7D,0x73,0x7E,0xFE,0x35,0xFF,0x7F //10,0-9 with point
};
void shumaguan(unsigned char dijiwei,unsigned char shuzi)
{
P0=0;
shumaguan1=1;
shumaguan2=1;
shumaguan3=1;
P0=tableduan[shuzi];
switch (dijiwei)
{
case 0:
shumaguan1=0;
break;
case 1:
shumaguan2=0;
break;
case 2:
shumaguan3=0;
break;
}
delayms(2);
}
bit MotorFlag;
sbit MotorPUL=P1^7;
sbit MotorDIR=P1^6;
sbit MotorENA=P1^5;
unsigned char Revolution,RevolutionTarget,MotorSpeed,i;
unsigned int Timer1Flag1,Timer0Flag2,num,Pulse;
unsigned int code PulseFrequency[]={2304,2560,1280,640};
unsigned int code SpeedTable[]={200,100,50,20,10,5,2};
unsigned char ValueTable[6];
void timer0init();
void Motor2Init();
void MotorSetting();
void MotorGo();
/
//1OnOff(1-on 0-off)
//2Dir(1- 0-)
//3Revolution(1-2-3-4-5 to 999)
//4(MotorSpeed1(10r/4s)-2-3-4)
void MotorSetting()
{
MotorDIR=0;
RevolutionTarget=1;
MotorSpeed=1;
MotorFlag=0;
}
void timer0init()
{
TMOD&=0xf0;
TMOD|=0x01;
TH0=(65536-922)/256;
TL0=(65536-922)%256;
EA=1;
ET0=1;
TR0=1;
}
void timer0() interrupt 1
{
TH0=(65536-922)/256;
TL0=(65536-922)%256;
Timer1Flag1++;
if(Timer1Flag1>=1000)
{
Timer1Flag1=0;
num++;
if(num>=1000)
{
num=0;
}
SBUF=num;
i++;
if(i>=sizeof ValueTable)
{
i=0;
}
}
Timer0Flag2++;
if(Timer0Flag2>=MotorSpeed)
{
Timer0Flag2=0;
MotorGo();
}
}
void Motor2Init()
{
MotorFlag=0;
MotorENA=0;
MotorDIR=0; //Forward default
}
void MotorGo()
{
if(MotorFlag==1&&MotorSpeed!=0&&RevolutionTarget!=0)
{
MotorENA=1;
MotorPUL=~MotorPUL;
Pulse++;
if(Pulse>=100)
{
Pulse=0;
Revolution++;
if(Revolution>=RevolutionTarget)
{
Revolution=0;
MotorFlag=0;
}
}
}
else
{
MotorENA=0;
}
}
答:没有串口线的同学可用USB转RS232,只要将程序下载到单片机即可。
答:没有STC89C52RC同学,可用STC89C52,或者其他8051单片机内核也行。
答:可以,只要产生脉冲信号就行。
答:小功率电机可用L293D,5V电源直接可以驱动,可不用TB6600。
答:小功率电机可用L293D,5V电源直接可以驱动,可不用TB6600。