
#include #define uchar unsigned char #define uint unsigned int uchar Buffer[4] = {0}; //从串口接收的数据 uint i,j; sbit Left_Positive=P1^4; sbit Left_Negative=P1^5; sbit Right_Positive=P1^6; sbit Right_Negative=P1^7; sbit LeftLight=P2^1; sbit RightLight=P2^2; /******************************************************************** * 名称 : Delay_1ms() * 功能 : 延时子程序,延时时间为 1ms * x * 输入 : x (延时一毫秒的个数) * 输出 : 无 ***********************************************************************/ void Delay_1ms(uint i)//1ms延时 { uchar x,j; for(j=0;jfor(x=0;x<=148;x++); } /******************************************************************** * 名称 : Com_Int() * 功能 : 串口中断子函数 * 输入 : 无 * 输出 : 无 ***********************************************************************/ void Com_Int(void) interrupt 4 { EA = 0; if(RI == 1) //当硬件接收到一个数据时,RI会置位 { if(SBUF==65) //这里减去48是因为从电脑中发送过来的数据是ASCII码。 Buffer[0] = SBUF - 48; { Buffer[0] = 3; //A } if(SBUF==68) { Buffer[0] = 4; // D } if(SBUF==83) { Buffer[0] = 2; //S } if(SBUF==87) { Buffer[0] = 1; //W } if(SBUF==69) { Buffer[0] = 0; //E } 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; //开总中断 } /******************************************************************** * 名称 :qianjin() * 功能 : 电机1、2启动,都是前进,整车表现为前进。 * 输入 : 无 * 输出 : 无 问题 ***********************************************************************/ void qianjin() { Right_Negative=0; Left_Positive=0; Left_Negative=1; Right_Positive=1; } /******************************************************************** * 名称 :houtui() * 功能 : 电机1、2启动,都是后退,整车表现为后退。 * 输入 : 无 * 输出 : 无 ***********************************************************************/ void houtui() { Left_Positive=0; Right_Positive=0; Right_Negative=1; Left_Negative=1; } /******************************************************************** * 名称 :zuozhuan() * 功能 : 电机1后退,电机2前进,整车表现为左转。 * 输入 : 无 * 输出 : 无 *************************************************** ********************/ void zuozhuan() { Left_Negative=0; Right_Positive=0; Right_Negative=1; Left_Positive=1; } /******************************************************************** * 名称 :youzhuan() * 功能 : 电机1前进,电机2后退,整车表现为右转。 * 输入 : 无 * 输出 : 无 ***********************************************************************/ void youzhuan() { Right_Negative=0; Left_Negative=0; Left_Positive=1; Right_Positive=1; } /******************************************************************** * 名称 :tingche() * 功能 : 电机1停止,电机2停止,整车表现为停止。 * 输入 : 无 * 输出 : 无 ***********************************************************************/ void tingche() { Right_Negative=0; Left_Negative=0; Left_Positive=0; Right_Positive=0; } void main() { Delay_1ms(100); Com_Init();//串口初始化 while(1)//循环结构,选择函数控制小车的状态 { switch(Buffer[0]) { case 0: tingche(); break; case 1: qianjin(); break; case 2: houtui(); break; case 3: zuozhuan(); break; case 4: youzhuan(); break; default:break; } } }
