📄 sl3010_2.c
字号:
switch (remote_in)
{//检测遥控器是否有键按下
case 0x04: // < A >
turn_right(); //右转
break;
case 0x02: // < B >
backward(); //后退
break;
case 0x08: // < C >
stop(); //停止运动
break;
case 0x01: // < D >
turn_left(); //左转
break;
case 0x00:
forward(); //前行
break;
}
delay_ms(100);
}
}
//自走车 机器人走四方形路线,遥控器调节行走路线
unsigned int delay_time1,delay_time2;
void auto_car1()
{
port_init();//PA,PB,PC,PD 初始化
work_status = 0x51;//置对应的工作状态标志
PORTA = 0x44;//0100,0100
delay_time1=510; //转弯时间
delay_time2=1200; //前行时间
mic_startup();//等待声控启动
forward(); //前行
delay_ms(200);
while(1)
{
sw_touch(); //检测轻触开关
remote_auto_time(); //遥控器控制直行和转弯时间
turn_right(); //右转
delay_ms(delay_time1);
forward(); //前行
delay_ms(delay_time2);
turn_right(); //右转
delay_ms(delay_time1);
forward(); //前行
delay_ms(delay_time2);
}
}
//自走车 机器人走< 8 >字,遥控器调节行走路线
void auto_car8()
{
port_init(); //PA,PB,PC,PD 初始化
work_status = 0x50;//置对应的工作状态标志
PORTA = 0x88;//1000,1000
delay_time1=1000; //转弯时间
delay_time2=1200; //前行时间
mic_startup(); //等待声控启动
forward(); //前行
delay_ms(delay_time2);
while(1)
{
sw_touch(); //检测轻触开关
remote_auto_time(); //遥控器控制直行和转弯时间
turn_right(); //右转
delay_ms(delay_time1);
forward(); //前行
delay_ms(delay_time2);
turn_left(); //左转
delay_ms(delay_time1);
forward(); //前行
delay_ms(delay_time2);
}
}
//cny_car 探测白底黑线,沿着黑线运动
unsigned char cny_in;
#define cny_delayms 50
void cny_car()
{
unsigned char cny_times;
port_init();
PORTA = 0xc3;//1100,0011
mic_startup();
while(1)
{
sw_touch();
cny_in = PINC & 0xe0;
if(cny_in == 0x00 || cny_in == 0x40 || cny_in == 0xa0)
{//前行
forward(); //前行
}
if(cny_in == 0x20 || cny_in == 0x60)
{//慢速右转
turn_right_s(); //慢速右转
delay_ms(cny_delayms);
}
if(cny_in == 0x80 || cny_in == 0xc0)
{//慢速左转
turn_left_s(); //慢速左转
delay_ms(cny_delayms);
}
if(cny_in == 0xe0)
{//前行--探测多次都为 0xe0 ,停止运行
// stop(); //停止运动//熄灭指示灯
/*
PORTD = 0xA0; //前行//1010 0000
PORTA = 0xC3; //1100,0011
cny_times = 0;
delay_ms(cny_delayms);
while(1)
{
cny_in = PINC & 0xe0;
if(cny_in != 0xe0)
{
break;
}
if(cny_in == 0xe0)
{
cny_times++;
if(cny_times == 5)
{
while(1)
{
PORTD = 0xff; //停止运动
PORTA = 0xff; //熄灭指示灯
}
}
}
}
*/
}
}
}
//追光机器人
unsigned char cds;
#define light_delay 100
void trace_light()
{
port_init(); //PA,PB,PC,PD 初始化
PORTA = 0xbd;//1011,1101
work_status = 0x70; //置对应的工作状态标志
mic_startup(); //等待声控启动
while(1)
{
sw_touch(); //检测轻触开关
cds = PINC & 0x06; //0b0000,0110
if(cds == 0x00) //左右两侧都感测到光
{//直行
forward();
delay_ms(light_delay); //延时
}
if(cds == 0x02) //0b0000,0010___pc2(右侧)感测到光
{//慢速右转
turn_right_s();
delay_ms(light_delay); //延时
}
if(cds == 0x04) //0b0000,0100___pc1(左侧)感测到光
{//慢速左转
turn_left_s();
delay_ms(light_delay); //延时
}
if(cds == 0x06)
{//停止动作
PORTA = 0x55; //延时后停止动作,等待光照重新动作
delay_ms(light_delay*2);
stop();
}
}
}
void main()
{
unsigned char pd_select,remote_select;
port_init(); //PA,PB,PC,PD 初始化
work_status = 0x67; //置对应的工作状态标志
PORTA = 0x7e;
while(1)
{
/*
pd_select = PIND & 0x0f;
if(pd_select == 0x0e)//pd0=0,其它为高电平
{
while(1)
{
delay_ms(500);//消除抖动
pd_select = PIND & 0x0f;
if(pd_select == 0x0e)//pd0=0,其它为高电平
{remote_car();}//运行遥控车程序
else
break;
}
}
if(pd_select == 0x0d)//pd1=0,其它为高电平
{
while(1)
{
delay_ms(500);//消除抖动
pd_select = PIND & 0x0f;
if(pd_select == 0x0d)//pd1=0,其它为高电平
{xuanya_car();}//运行悬崖车程序
else
break;
}
}
if(pd_select == 0x0b)//pd2=0,其它为高电平
{
while(1)
{
delay_ms(500);//消除抖动
pd_select = PIND & 0x0f;
if(pd_select == 0x0b)//pd1=0,其它为高电平
{music_car();}//运行音乐车程序
else
break;
}
}
if(pd_select == 0x07)//pd3=0,其它为高电平
{
while(1)
{
delay_ms(500);//消除抖动
pd_select = PIND & 0x0f;
if(pd_select == 0x07)//pd1=0,其它为高电平
{led_car();}//运行霓虹车程序
else
break;
}
}
*/
//********************************************************//
// pd3~pd0 的短路块均接上 //
pd_select = PIND & 0x0f;
if(pd_select == 0x00) //pd0,pd1,pd2,pd3均为低电平
{
remote_select = PINB & 0x0f;//检测遥控器是否有键按下
if(remote_select == 0x04)// A
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x04)// A
{remote_car();}//运行遥控车程序
else
break;
}
}
if(remote_select == 0x02)// B
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x02)// B
{xuanya_car();}//运行悬崖车程序
else
break;
}
}
if(remote_select == 0x08)// C
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x08)// C
{music_car();}//运行音乐车程序
else
break;
}
}
if(remote_select == 0x01)// D
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x01)// D
{led_car();}//运行霓虹车程序
else
break;
}
}
}
//*******************************************************//
// pd3~pd0 的短路块没有全部接上 //
if(pd_select != 0x00) //pd0,pd1,pd2,pd3 不全为低电平
{
remote_select = PINB & 0x0f;//检测遥控器是否有键按下
if(remote_select == 0x04)// A
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x04)// A
{auto_car1();}//机器人走四方形路线
else
break;
}
}
if(remote_select == 0x02)// B
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x02)// B
{auto_car8();}//机器人走"8"字
else
break;
}
}
if(remote_select == 0x08)// C
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x08)// C
{cny_car();}//寻迹机器人,探测白底黑线,沿黑线运动
else
break;
}
}
if(remote_select == 0x01)// D
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x01)// D
{trace_light();}//追光机器人
else
break;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -