#include<reg52.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
#define output P2
uchar Buffer[4]={0};//从串口接收的数据
uint i,j;
//sbit Left_Positive=P1^0;
//sbit Left_Negative=P1^1;
//sbit Right_Positive=P1^2;
//sbit Right_Negative=P1^3;
//uchar excite;
uchar flag_1,flag_2,temp;
//char k;
uchar code table[]={0x11,0x33,0x22,0x66,0x44,0xcc,0x88,0x99};
sbit LeftLight=P0^1;
sbit RightLight=P0^0;
/****************************************************
*名称:Delay_1ms()
*功能:延时子程序,延时时间为 1ms*x
*输入:x(延时一毫秒的个数)
*输出:无
****************************************************/
void Delay_1ms(uint i) //1ms延时
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
/***************************************************
*名称:Com_int()
*功能:串口中断子函数
*输入:无
*输出:无
***************************************************/
void Com_int(void) interrupt 4
{
EA=0;
if(RI == 1) //当硬件接收到一个数据时,RI会置位
{
Buffer[0]=SBUF-48; //这里减去48是因为从电脑中发送过来的数据时ASCII码
RI=0;
}
EA=1;
}
/***************************************************
*名称:Com_Init()
*功能:串口初始化,晶振11.0592,波特率是9600,使串口中断
*输入:无
*输出:无
***************************************************/
void Com_Init(void)
{
TMOD=0x20;
PCON=0x00;
SCON=0x50;
TH1=0xfd; //设置波特率为9600
TL1=0xfd;
TR1=1; //启动定时器1
ES=1; //开串口中断
EA=1; //开总中断
}
///*************************************************
//*名称:RightLightTurnOn()
//*功能;打开右车灯
//*输入:无
//*输出:无
//*************************************************/
//void RightLightTurnOn()
//{
// LeftLight=1;
// RightLight=0;
//}
//
///*************************************************
//*名称:LeftLightTurnOn()
//*功能:打开左车灯
//*输入:无
//*输出:无
//*************************************************/
//
//void LeftLightTurnOn()
//{
// LeftLight=0;
// RightLight=1;
//}
///*************************************************
//*名称:LightTurnOff()
//*功能:关灯
//*输入:无
//*输出:无
//*************************************************/
//
//void LightTurnOff()
//{
// LeftLight=1;
// RightLight=1;
//}
///*************************************************
//*名称:LightWarning
//*功能:两盏灯同时亮
//*输入:无
//*输出:无
//*************************************************/
//
//void LightWarning()
//{
// LeftLight=0;
// RightLight=0;
//}
/***************************************************
*名称:Moto_Forward()
*功能:电机1,2启动,都是前进,整车表现为前进
*输入:无
*输出:无
***************************************************/
void Moto_Forward()
{
// Right_Negative=0;
// Left_Positive=0;
// Left_Negative=1;
// Right_Positive=1;
// excite=0x01;
//
// for(k=0;k<4;k++)
// {
// output=excite;
// Delay_1ms(80);
// excite<<=1;
// }
//Delay_1ms(100);
// LightTurnOff();
temp=P2;
//temp2=P2;
output=(temp&0xf0)|(table[flag_1]&0x0f);
Delay_1ms(80);
flag_1++;
if(flag_1==8)
{
flag_1=0;
}
output=(temp&0x0f)|(table[flag_2]&0xf0);
Delay_1ms(80);
flag_2++;
if(flag_2==8)
{
flag_2=0;
}
}
/**************************************************
*名称:Moto_Backward()
*功能:电机1,2启动,都是后退,整车表现为后退
*输入:无
*输出:无
**************************************************/
void Moto_Backward()
{
// Left_Positive=0;
// Right_Positive=0;
// Right_Negative=1;
// Left_Negative=1;
//
// Delay_1ms(100);
// LightWarning();
temp=P2;
output=(temp&0xf0)|(table[flag_1]&0x0f);
Delay_1ms(80);
if(flag_1==0)
{
flag_1=8;
}
flag_1--;
output=(temp&0x0f)|(table[flag_2]&0xf0);
Delay_1ms(80);
if(flag_2==0)
{
flag_2=8;
}
flag_2--;
}
/*************************************************
*名称:Moto_TurnLeft()
*功能:电机1后退,电机2前进,整车表现为左转
*输入:无
*输出:无
*************************************************/
void Moto_TurnLeft()
{
// Left_Negative=0;
// Right_Positive=0;
// Right_Negative=1;
// Left_Positive=1;
//
// Delay_1ms(100);
//LeftLightTurnOn();
temp=P0;
output=(temp&0x0f)|(table[flag_1]&0xf0);
Delay_1ms(80);
flag_1++;
if(flag_1==8)
{
flag_1=0;
}
}
/*************************************************
*名称:Moto_TurnRight()
*功能:电机1前进,电机2后退,整车表现为右转
*输入:无
*输出:无
*************************************************/
void Moto_TurnRight()
{
// Right_Negative=0;
// Left_Negative=0;
// Left_Positive=1;
// Right_Positive=1;
//
// Delay_1ms(100);
// RightLightTurnOn();
temp=P0;
output=(temp&0xf0)|(table[flag_2]&0x0f);
Delay_1ms(80);
flag_2++;
if(flag_2==8)
{
flag_2=0;
}
}
/*************************************************
*名称:Moto_Stop()
*功能:电机1停止,电机2停止,整车表现为停止
*输入:无
*输出:无
*************************************************/
void Moto_Stop()
{
// Right_Negative=0;
// Left_Negative=0;
// Left_Positive=0;
// Right_Positive=0;
//
// Delay_1ms(100);
//LightTurnOff();
temp=P0;
output=temp;
}
void main()
{
Delay_1ms(100);
Com_Init(); //串口初始化
//P1=0;
while(1)
{
switch(Buffer[0])
{
case 0:Moto_Stop();break;
case 1:Moto_Forward();break;
case 2:Moto_Backward();break;
case 3:Moto_TurnLeft();break;
case 4:Moto_TurnRight();break;
// case 8:LightTurnOn();break;
// case 9:LightTurnOff();break;
default:break;
// case 1:P1=2;break;
// case 2:P1=4;break;
// case 3:P1=8;break;
// case 4:P1=16;break;
// case 4:Moto_TurnRight();break;
// //case 8:LightTurnOn();break;
// //case 9:LightTurnOff();break;
// default:break;
}
}
}