//请根据自己马达的控制 来改变程序
#include<reg52.h>
#include<math.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar Buffer =0;
//从串口接收的数据
uint URTAReceivedCount=0,n=1;
uchar data Tempdatatable[5],CommandDatatable[5];//数据包
uchar serVal[2];
//uint pwm[]={1120,1190,1382,1382,1382,1382,1382,1382}; //初始90度,(实际是1382.4,取整得1382)
//uchar pwm_flag=0;
uint code ms0_5Con=461; //0.5ms计数 (实际是460.8,取整得461)
uint code ms2_5Con=2304; //2.5ms计数
bit key_stime_ok;
void Delay_1ms(uint i)//1ms延时
{
uchar x,j;
for(j=0;j
for(x=0;x<=148;x++);
}
void Send_Data(uchar type,uchar cmd,uchar dat)
{
uchar data Buffer[5];//构建数据包
uchar *p;
uint Send_Count=0;
p = Buffer;
Buffer[0]=0XFF;
Buffer[1]=type;
Buffer[2]=cmd;
Buffer[3]=dat;
Buffer[4]=0XFF;
while(1)
{
if(*p==0XFF)
{
Send_Count++; //0XFF标志统计位
}
SBUF = *p; //发送
while(!TI) //如果发送完毕,硬件会置位TI,等待发送完毕
{
_nop_();
}
p++;
TI = 0;
if(Send_Count == 2) //当统计到两次出现0XFF,则认为一个数据包发送完毕,跳出循环
{
TI = 0;
break;
}
}
}
void Com_Int(void) interrupt 4
{
uchar temp;
ES=0; //关串口中断
RI=0; //软件清除接收中断
temp=SBUF;
if(temp==0XFF && URTAReceivedCount<3)
{
Tempdatatable[0]==0XFF; //包头
URTAReceivedCount++;
}
else
{
Tempdatatable[n]=temp;
n++;
if(URTAReceivedCount==0&&n==2)
n=1;
}
if(URTAReceivedCount==2)//包尾
{
Tempdatatable[0]=0XFF;
Tempdatatable[4]=0XFF;
n=1;
URTAReceivedCount=0; //组包完毕
temp=" ";
// Send_Data(Tempdatatable[1],Tempdatatable[2],Tempdatatable[3]); //发送组成的数据包回去
}
CommandDatatable[0]=Tempdatatable[0];
CommandDatatable[1]=Tempdatatable[1];
CommandDatatable[2]=Tempdatatable[2];
CommandDatatable[3]=Tempdatatable[3];
CommandDatatable[4]=Tempdatatable[4];
ES=1;//开串口中断
}
void Com_Init(void)
{
TMOD = 0x21;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFd; //设置波特率 9600
TL1 = 0xFd;
TR1 = 1; //启动定时器1
ES = 1; //开串口中断
EA = 1; //开总中断
IT0=0;
EX0=1;
}
void main()
{
Delay_1ms(200);
Com_Init();//串口初始化
Timer0Init();//舵机PWM中断初始化
while(1)
{
if(CommandDatatable[0]==0XFF && CommandDatatable[4]==0XFF)
{
switch (CommandDatatable[1]) //根据键值不同,执行不同的内容
{
case 0X00: //类型位0X00,表明是控制数据包,进入控制数据case
switch(CommandDatatable[2]) //根据数据位的值来进行选择执行不同的动作
{
case 0X00:Moto_Stop();break;
case 0X01:Moto_Forward();break;
case 0X02:Moto_Backward();break;
case 0X03:Moto_TurnLeft();break;
case 0X04:Moto_TurnRight();break;
case 0X05:Moto_ForLeft();break;
case 0X06:Moto_ForRight();break;
case 0X07:Moto_BackLeft();break;
case 0X08:Moto_BackRight();break;
default : break;
}
break;
default : break;
}
}
}
}
#include<reg52.h>
#include<math.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar Buffer =0;
uint URTAReceivedCount=0,n=1;
uchar data Tempdatatable[5],CommandDatatable[5];//数据包
uchar serVal[2];
//uint pwm[]={1120,1190,1382,1382,1382,1382,1382,1382}; //初始90度,(实际是1382.4,取整得1382)
//uchar pwm_flag=0;
uint code ms0_5Con=461; //0.5ms计数 (实际是460.8,取整得461)
uint code ms2_5Con=2304; //2.5ms计数
bit key_stime_ok;
void Delay_1ms(uint i)//1ms延时
{
uchar x,j;
for(j=0;j
for(x=0;x<=148;x++);
}
void Send_Data(uchar type,uchar cmd,uchar dat)
{
}
void Com_Int(void) interrupt 4
{
uchar temp;
ES=0; //关串口中断
RI=0; //软件清除接收中断
temp=SBUF;
}
void Com_Init(void)
{
}
void main()
{
}