#include<reg52.h> #include<stdio.h> #include<stdlib.h> #define uint unsigned int; #define uchar unsigned char; /*sbit L_on=P1^4; sbit L1=P0^1; sbit L2=P0^2; sbit L3=P0^3; sbit L4=P0^4; sbit L5=P0^5; sbit L6=P0^6; */ sbit left_in1=P1^0; sbit left_in2=P1^1; sbit right_in3=P1^2; sbit right_in4=P1^3; sbit ENB_left= P1^4; sbit ENB_right= P1^5; sbit OUT_LED_Left=P1^6; sbit OUT_LED_Right=P1^7; #define left_forward left_in1=1;left_in2=0 #define left_back left_in1=0;left_in2=1 #define left_stop left_in1=0;left_in2=0 #define right_forward right_in3=1;right_in4=0 #define right_back right_in3=0;right_in4=1 #define right_stop right_in3=0;right_in4=0 uint ZKB_Left = 0; uint ZKB_Right = 0; int ZKB_A = 70; int ZKB_B = 30; uint count=0; //计数器 char Buffer; void Bluetooth_Move(); /******************************************************************** * 名称 : Delay_1ms() * 功能 : 延时子程序,延时时间为 1ms * x * 输入 : x (延时一毫秒的个数) * 输出 : 无 ***********************************************************************/ void Delay_1ms(int i)//1ms延时 { int 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会置位串口有发送和接收两种不一样的状况 //(不管是发送仍是接收,只要发送或接收完成,都会置位串口中断标志位(发送就置位TI,接收则置位RI), //因此=1表示的是两个标志位只要有一个为1(便是两种状况有一种发生)且此时系统容许串口中断, //则会转向中断服务程序,在服务程序里判断中断是TI/RI哪一个为1触发的,相应的作出处理,如能够把缓冲区的数据读出来。) { Buffer=SBUF;//SBUF:51串口通讯寄存器 RI = 0; //关闭串口中断 } EA = 1; if(count<ZKB_Left) ENB_left=1; else ENB_left=0; if(count<ZKB_Right) ENB_right=1; else ENB_right=0; count++; if(count>=100) count=0; } /******************************************************************** * 名称 : Com_Init() * 功能 : 串口初始化,晶振11.0592,波特率9600,使串口中断 * 输入 : 无 * 输出 : 无 ***********************************************************************/ void Com_Init(void) { TMOD = 0x20; //选择使用8位定时器1 PCON = 0x00; //是设置波特率不加倍 SCON = 0x50; //选择8位异步通讯方式,波特率可变,由定时器控制;容许接受;发送中断标志、接收中断标志分别为0、0 TH1 = 0xfd; //设置波特率 9600= 11.0592*1000000/12/(0x100-0xfd)/32 TL1 = 0xfd; TR1 = 1; //启动定时器1 ES = 1; //开串口中断 EA = 1; //开总中断 } void main() { // L_on = 0; Com_Init(); while(1) { Bluetooth_Move(); } } void Bluetooth_Move(){ if('0'<=Buffer<='9'){ switch(Buffer){ case '0': ZKB_A = 10;ZKB_B = 10;break; case '1': ZKB_A = 20;ZKB_B = 10;break; case '2': ZKB_A = 30;ZKB_B = 10;break; case '3': ZKB_A = 40;ZKB_B = 10;break; case '4': ZKB_A = 50;ZKB_B = 10;break; case '5': ZKB_A = 60;ZKB_B = 20;break; case '6': ZKB_A = 70;ZKB_B = 30;break; case '7': ZKB_A = 80;ZKB_B = 40;break; case '8': ZKB_A = 90;ZKB_B = 50;break; case '9': ZKB_A = 100;ZKB_B = 60;;break; } } if('A'<=Buffer<='a') { switch(Buffer){ case NULL: left_stop;right_stop; break; case 0x41: ZKB_Left=ZKB_A;ZKB_Right=ZKB_A; left_forward;right_forward;break; //前进 case 0x42: ZKB_Left=ZKB_A;ZKB_Right=ZKB_A; left_back;right_back; break; //后退 case 0x43: ZKB_Left=ZKB_B; ZKB_Right=ZKB_A; left_back;right_forward;break; //左上 case 0x44: ZKB_Left=ZKB_A;ZKB_Right=ZKB_B; left_forward;right_back;break; //右上 case 0x45: ZKB_Left=ZKB_B; ZKB_Right=ZKB_A; left_forward;right_back; break; //左下 case 0x46: ZKB_Left=ZKB_A;ZKB_Right=ZKB_B; left_back;right_forward; break; //右下 case 0x61: left_stop;right_stop; break; //手离开屏幕中止 default : left_stop;right_stop; break; } } }