|
//P1.0~P1.7分別對(duì)應(yīng)小車1N1~1N8。
#include <REGX52.H> //頭文件
#define uint unsigned int //宏定義
#define uchar unsigned char
#define left_moto_go {P1_0=1,P1_1=0,P1_2=1,P1_3=0;} //小車方位定義
#define left_moto_back {P1_0=0,P1_1=1,P1_2=0,P1_3=1;}
#define left_moto_stop {P1_0=0,P1_1=0,P1_2=0,P1_3=0;}
#define right_moto_go {P1_4=1,P1_5=0,P1_6=1,P1_7=0;}
#define right_moto_back {P1_4=0,P1_5=1,P1_6=0,P1_7=1;}
#define right_moto_stop {P1_4=0,P1_5=0,P1_6=0,P1_7=0;}
//延時(shí)
void delay_ms(uint a) //1ms
{
uint x,y;
for(x=0;x<a;x++)
for(y=0;y<115;y++)
;
}
//小車前進(jìn)
void go(void)
{
left_moto_go ;
right_moto_go ;
}
//小車后退
void back(void)
{
left_moto_back ;
right_moto_back ;
}
//小車停止
void stop(void)
{
left_moto_stop;
right_moto_stop;
}
//小車左轉(zhuǎn)
void left(void)
{
left_moto_stop;
right_moto_go ;
}
//小車右轉(zhuǎn)
void right(void)
{
left_moto_go ;
right_moto_stop ;
}
//主函數(shù)
void main()
{
while(1)
{
go();
delay_ms(500);
right();
delay_ms(500);
stop();
delay_ms(500);
left();
delay_ms(500);
back();
}
} |
|