驱动六路电机 开关和走动 因为要驱动6个电机 所以不能控制一个电机直接关掉ET0 所以想用flag来当中断开关
#include<STC12C5A.h>
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE flag1;
BYTE cache,count;
sbit DIR=P0^5;
//sbit SLP=P1^0;
sbit flag = P2^7;
sbit STEP=P0^4;
sbit EN = P0^0;
sbit M1 = P0^1;
sbit M2 = P0^2;
sbit M3 = P0^3;
//sbit EN=P1^3;
void Timer0Init(void) //100微秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xAE; //设置定时初值
TH0 = 0xFB; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=1;
// ET0=1;
}
void UartInit(void) //9600bps@11.0592MHz
{
PCON &= 0x7F; //波特率不倍速
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //清除定时器1模式位
TMOD |= 0x20; //设定定时器1为8位自动重装方式
TL1 = 0xDC; //设定定时初值
TH1 = 0xDC; //设定定时器重装值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
ES = 1;
}
void init()
{
EN = 0;
M1 = 1;
M2 = 0;
M3 = 0;
}
void delay(WORD n)
{
WORD x;
while (n--)
{
x = 5000;
while (x--);
}
}
void D1()
{
if(cache == 0xa5)
{
TR0 = 0;
ET0 = 0;
cache = 0;
}
if(cache ==0x50)
{
TR0 = 1;
ET0 = 1;
DIR = 1;
cache = 0;
flag1 = 0;
}
if(!flag )
{
DIR = 0; //向电机方向移动
}
}
void send(BYTE dat)
{
SBUF = dat; //发送当前数据
while (!TI); //等待前一个数据发送完成
TI = 0; //清除发送标志
}
void main()
{
Timer0Init();
init();
UartInit();
while(1)
{
D1();
// DIR = 1;
// flag1= 1;
/* if(flag1)
{
if(!flag)
{
delay(1);
if(!flag)
{
DIR = 1;
delay(500);
}
}
} */
}
}
void timer0() interrupt 1
{
// count++;
/* if(flag1 == 1)
{
STEP=0;
}
if(flag1 == 2)
{
STEP=~STEP;
} */
STEP=~STEP;
// if(count >= 100)
// {
// count = 0;
// send(0x52);
// }
/*if(flag==1)
{
t1++;
if(t1>=35000)
{
t1=0;
flag=2; //计时完成=2
}
}*/
}
void io() interrupt 4
{
if(RI)
{
RI = 0;
cache = SBUF;
}
}
|