|
簡單實現前后左右
#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; //中斷關閉
}
|
|