#include "MY_REG52.H"
//#include<at89x52.h>
//#include "MMC.h"
#define uchar unsigned char
#define uint unsigned int
//#define RELOAD_COUNT 0xf3
#define addrch1 0x00 //en sty flag stepmode ad xifen baoliu
#define addrch2 0x04 //addr为地址,其余为命令
#define addrch3 0x08 //
#define addrzh1 0x03 //命令时,255/255为最高占空比,0/255为最低
#define addrzh2 0x07
//#define addrzh3 0x0b
#define addrsam 0x0c //同步模式寄存器地址
#define run 0xc0 //相当于使能,正转,正反转的控制在bit5位,0为正传 bit7 H:run L:hold
#define zhiliu 0x40 //直流电机
#define hold 0x40 //电机保留,只是停住
#define back 0xe0 //直流,反转,注意!反转时的占空比与正转时互补为100%
#define nosame 0x00 //ch1,ch2,ch3都不同步
#define same_12 0x01 //ch1,ch2同步
//#define same13 0x02 //ch1,ch3同步
#define same 0x03 //ch1,ch2,ch3都同步
#define Ture 0x01
#define False 0x00
#define TB8H SCON=0Xd8
#define TB8L SCON=0Xd0
sbit LED=P2^3;
sbit reset_MMC = P2^0;
sbit sleep=P2^1;
uchar array[9]={0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
bit sending=0;
void delay1ms();
void delayms(uchar t);
void delays(uchar t);
void serial_port_initial();
void send_UART(uchar dat);
//uchar rec_UART();
uchar read_MMC(uchar adress);
void command_MMC(uchar adress,uchar dat);
void initialMMC();
void front_ch1();
void front_ch2();
void back_ch1();
void back_ch2();
void hold_all();
void hold_ch1();
void hold_ch2();
void same12();
void notsame();
void chv_ch1(uchar dat);
void chv_ch2(uchar dat);
void delay1ms()
{ uchar a,b;
for(a=0;a<100;a++)
for(b=0;b<6;b++);
}
void delayms(uchar t)
{ while(t--)
delay1ms();
}
void delays(uchar t)
{ while(t--)
{ delayms(200);
delayms(200);
delayms(200);
delayms(200);
}
}
void serial_port_initial()
{ SCON=0XD0; //带奇偶校验位的通信方式
TMOD=0X20;
PCON|=0x80;
AUXR=0XD1; //BRTR=1即启动BRT,此处在bit4即高4位的最低位
//BRT=0XFF; //BAUD~~128000
BRT=256-13; //9600baud
REN=1;
TR1=0;
ES=1;
EA=1;
}
void send_UART(uchar dat)
{ SBUF=dat;
sending=1;
while(sending);
TI=0;
}
/*
uchar rec_UART()
{ uchar k;
while(RI==0);
RI=0;
k=SBUF;
return k;
}
*/
void main(void)
{ uchar tp=0;
serial_port_initial();
// initialMMC();
while(1)
{ for(tp=0;tp<9;tp++)
{
send_UART(array[tp]);
delays(5);
}
/* p23=1;
chv_ch1(0x90);
delays(10);
hold_ch1();
delays(10);
front_ch1();
delays(10); //real_time~~1500ms
hold_ch1();
delays(20);
back_ch1();
delays(20);
chv_ch1(0xff);
delays(2);
back_ch1();
delays(20);
hold_all();
delays(20);
notsame();
delays(20);
notsame();
delays(20);
*/
}
}
void UartISR(void) interrupt 4
{// uchar k;
//ES=0;
if(RI==1) //收到数据
{ RI=0; //清中断请求
/* k=SBUF;
if(k==0x08)
p35=1;
else
p35=0;
P1=k; */
}
else //发送完一字节数据
{
TI=0;
sending=0; //清正在发送标志
}
//ES=1;
}
/*
uchar read_MMC(uchar address)
{ uchar dat;
bit pp=0;
send_UART(address|0xa0);
_nop_();
dat=rec_UART();
if(dat==0x40)
pp=dat%2;
else
pp=dat%2+1;
if(RB8!=pp)
return 0x00;
else
return dat;
}
void command_MMC(uchar address,uchar dat)
{ if(address==0x04)
TB8=0;
else
TB8=address%2+1;
send_UART(address|0x50);
delayms(10);
if(dat==0x40)
TB8=0;
else
TB8=dat%2+1;
send_UART(dat);
}
*/
void initialMMC()
{ reset_MMC=0; //复位MMC
delayms(40); //稍延时>=10us
reset_MMC=1; //复位完成
sleep=1; //取消睡眠
delayms(10);
//chv_ch2(0xf0);
same12(); //ch1和ch2同步
delayms(10);
chv_ch1(0xa0); // 11110000/11111111 fast!
delayms(10);
hold_ch1(); //通道1为直流电机
delayms(10);
//command_MMC(addrch2,zhiliu); //通道2为直流电机。此处不用再设置ch2,因为同步后ch2的设置不被保留
}
void same12()
{ TB8H;
send_UART(0x5c);
delayms(20);
TB8L;
send_UART(0x01);
}
void chv_ch1(uchar dat)
{ SCON|=0x08;
send_UART(0x53);
delayms(20);
send_UART(dat);
}
void chv_ch2(uchar dat)
{ TB8L;
send_UART(0x57);
delayms(20);
TB8H;
send_UART(dat);
}
void notsame()
{ TB8H;
send_UART(0X5C);
delayms(20);
send_UART(0X00);
}
void front_ch1()
{ TB8H;
send_UART(0X50);
delayms(20);
send_UART(0XC0);
}
void front_ch2()
{ TB8L;
send_UART(0X54);
delayms(20);
TB8H;
send_UART(0xc0);
}
void back_ch1()
{ TB8H;
send_UART(0x50);
delayms(20);
TB8L;
send_UART(0Xe0);
}
void back_ch2()
{ TB8L;
send_UART(0X54);
delayms(20);
//TB8H;
send_UART(0xe0);
}
void hold_ch1()
{ TB8H;
send_UART(0x50);
delayms(20);
TB8L;
send_UART(0x40);
}
void hold_ch2()
{ TB8L;
send_UART(0x54);
delayms(20);
send_UART(0x40);
}
void hold_all()
{ same12();
delayms(20);
hold_ch1();
}
/*
void front_ch3()
{ TB8L;
send_UART(0X58);
delayms(20);
TB8H;
send_UART(0XC0);
}*/
/*
void sleeping()
{ sleep=0;
}
void read_ch1()
{ P1=read_MMC(addrch1);
}
void read_ch2()
{ P1=read_MMC(addrch2);
}
void read_zh1()
{ P1=read_MMC(addrzh1);
}
void read_zh2()
{ P1=read_MMC(addrzh2);
}
void read_sam()
{ P1=read_MMC(addrsam);
}
*/