简单实现前后左右
#include<AT89x51.H>
//HL-1小车驱动接线定义
#define A_left_moto_go {P2_0 = 1,P2_1 = 0;}
#define A_left_moto_back {P2_0 = 0,P2_1 = 1;}
#define A_left_moto_stop {P2_0 = 0,P2_1 = 0;}
#define A_right_moto_go {P2_2 = 1,P2_3 = 0;}
#define A_right_moto_back {P2_2 = 0,P2_3 = 1;}
#define A_right_moto_stop {P2_2 = 0,P2_3 = 0;}
#define B_left_moto_go {P1_0 = 1,P1_1 = 0;}
#define B_left_moto_back {P1_0 = 0,P1_1 = 1;}
#define B_left_moto_stop {P1_0 = 0,P1_1 = 0;}
#define B_right_moto_go {P1_2 = 1,P1_3 = 0;}
#define B_right_moto_back {P1_2 = 0,P1_3 = 1;}
#define B_right_moto_stop {P1_2 = 0,P1_3 = 0;}
#define uint unsigned int //重定义无符号整数类型
#define uchar unsigned char //重定义无符号字符类型
#define led P0_0
/************************************************************************/
//延时函数
void delay(unsigned int k)
{
unsigned int x,y;
for(x=0;x<k;x++)
for(y=0;y<2000;y++);
}
/************************************************************************/
void run()
{
A_left_moto_go;
A_right_moto_go;
B_left_moto_go;
B_right_moto_go;
}
void back_run()
{
A_left_moto_back;
A_right_moto_back;
B_left_moto_back;
B_right_moto_back;
}
void stop_run()
{
A_left_moto_stop;
A_right_moto_stop;
B_left_moto_stop;
B_right_moto_stop;
}
void left_run()
{
A_left_moto_back;
A_right_moto_go;
B_left_moto_back;
B_right_moto_go;
}
void right_run()
{
A_left_moto_go;
A_right_moto_back;
B_left_moto_go;
B_right_moto_back;
}
char uart_data; // 函数变量
char temp;
void main(void) //主函数
{
SCON=0X50; //设置串口方式为1 方式1
TMOD=0X20;//设置为定时器1,
TH1= 0x0FD; //赋初值
TL1= 0x0FD;
ES=1; //开串口中断
EA=1; //开中中断
TR1=1; //启动定时器1
led = 1; //p2.0置1,灯灭
while(1);
}
void serial_IT(void) interrupt 4 //中断程序
{
if(RI==1) //R1置1.向cpu 申请中断,循环
{
RI=0; //软件清零,取消申请
uart_data = SBUF; //蓝牙输入数据赋值
SBUF = uart_data; //返回终端
temp = uart_data; //赋值
if(temp == 0x00 ) led = ~led;
switch(temp)
{
case 'a' : run(); break;// 前进
case 'b' : back_run(); break;//后退
case 'c' : left_run(); break;//左转
case 'd' : right_run();break;//右转
case 'e' : stop_run(); break;//停止
}
}
else T1 = 0; //中断关闭
|