- #include <reg52.h>
- //#include <stdio.h>
- sbit Mr0 = P1^0;
- sbit Ml0 = P1^1;
- sbit Mr1 = P1^2;
- sbit Ml1 = P1^3;
- #define uint unsigned int
- #define uchar unsigned char
- #define card_go {Mr0=0,Ml0=1,Mr1=0,Ml1=1;}//前进
- #define card_back {Mr0=1,Ml0=0,Mr1=1,Ml1=0;}//后退
- #define Left_card_go {Mr0=0,Ml0=1,Mr1=1,Ml1=0;}//左转圈
- #define Right_card_go { Mr0=1,Ml0=0,Mr1=0,Ml1=1;}//右转圈
- #define stop_card { Mr0=0,Ml0=0,Mr1=0,Ml1=0;}//stop
- #define up 'A'
- #define down 'B'
- #define left 'C'
- #define right 'D'
- #define stop 'F'
- //#define guohua_left 'C'
- //#define guohua_right 'D'
- //#define wang_left '1'
- //#define wang_right '3'
- //#define guo_left '9'
- //#define guo_right '7'
- char code str[] = "收到指令,向前!\n";
- char code str1[] = "收到指令,向后!\n";
- char code str2[] = "收到指令,向左!\n";
- char code str3[] = "收到指令,向右!\n";
- char code str4[] = "收到指令,停止!\n";
- uchar i=0;
- uchar dat=0;
- uchar buff[3]=0; //缓冲字节
- bit flag_REC=0;
- bit flag =0;
- ////毫秒级延时函数定义
- ////void delay(uint z)
- ////{
- //// uint x,y;
- //// for(x = z; x > 0; x--)
- //// for(y = 114; y > 0 ; y--);
- ////}
- //
- ////字符串发送
- //// void send_str( )
- ////{
- //// unsigned char i = 0;
- //// while(str[i] != '\0')
- //// {
- //// SBUF = str[i];
- //// while(!TI);
- //// TI = 0;
- //// i++;
- //// }
- ////}
- //
- //// void send_str1( )
- ////{
- //// unsigned char i = 0;
- //// while(str1[i] != '\0')
- //// {
- //// SBUF = str[i];
- //// while(!TI);
- //// TI = 0;
- //// i++;
- //// }
- ////}
- ////
- //// void send_str2( )
- ////{
- //// unsigned char i = 0;
- //// while(str2[i] != '\0')
- //// {
- //// SBUF = str[i];
- //// while(!TI);
- //// TI = 0;
- //// i++;
- //// }
- ////}
- ////
- //// void send_str3( )
- ////{
- //// unsigned char i = 0;
- //// while(str3[i] != '\0')
- //// {
- //// SBUF = str[i];
- //// while(!TI);
- //// TI = 0;
- //// i++;
- //// }
- ////}
- ////
- //// void send_str4( )
- ////{
- //// unsigned char i = 0;
- //// while(str4[i] != '\0')
- //// {
- //// SBUF = str[i];
- //// while(!TI);
- //// TI = 0;
- //// i++;
- //// }
- ////}
- void sint() interrupt 4 //中断接收3字节
- {
- if(RI)
- {
- RI=0;
- P1 = 0;
- dat=SBUF;
- if(dat=='O'&&(i==0)) //数据第一帧
- {
- buff[i]=dat;
- flag=1;
- }
- else
- if(flag==1)
- {
- i++;
- buff[i]=dat;
- if(i>=2)
- {i=0;flag=0;flag_REC=1 ;}
- }
- }
- }
- //串口初始化
- void UART_INIT()
- {
- SM0 = 0; SM1 = 1;//串口工作方式1
- REN = 1;//允许串口接收
- EA = 1;//开总中断
- ES = 1;//开串口中断
- TMOD = 0x20;//8位自动重装模式
- TH1 = 0xfd;
- TL1 = 0xfd;//9600波特率
- TR1 = 1;//启动定时器1
- }
- void main()
- {
- UART_INIT(); //串口初始化
- while(1)
- {
- if(flag_REC==1) //中断接收完成
- {
- flag_REC=0;
- if(buff[0]=='O'&&buff[1]=='N') //第一个字节0,第二个N,第三个控制码
- switch(buff[2])
- {
- case up : card_go; break;
- case down : card_back; break;
- case left : Left_card_go; break;
- case right : Right_card_go; break;
- case stop : stop_card; break;
-
-
- }
- }
- }
- }
- /*************************************
- 备注:
- 使用printf()puts()前都应软件置位TI = 1;
- printf()puts() 使用putchar函数发送字节
- void putchar(uchar sbyte )
- {
- while(!TI); //等待发送完
- SBUF=sbyte;
- }
- 所以第一次调用putchar前没TI=1 永远等待
- *************************************/
复制代码
|