📄 sl3010_4._c
字号:
#pragma interrupt_handler timer1_ovf_isr:iv_TIMER1_OVF
void timer1_ovf_isr(void)
{
TCNT1=toneconst;
PORTC^=1;
}
void music_car(void)
{
TCCR1A = 0x00;
TCCR1B =(1<<CS11);//8分频
temp1=EEPROMread((int)&sound[sound_add]);
while(temp1!=0)
{
temp=temp1;
temp&=0x0f;
if(temp!=0)
{
TIMSK|=(1<<TOIE1);//根据SOUNDTABLE中数据的低四位选择音调频率
temp--;
toneconst=TONETABLE[temp];
TCNT1=toneconst;
}
temp=temp1;
temp>>=4;
temp&=0x0f;
delay_ms(temp*129);//根据SOUNDTABLE中数据的高四位*5后,为音调持续时间
TIMSK&=~(1<<TOIE1);
sound_add++;
temp1=EEPROMread((int)&sound[sound_add]);
}
delay_ms(1000);
}
// 电机驱动信号 led灯指示电机状态
//PORTD = 0xA0;//前行//1010 0000 //PORTA = 0xC3;//1100,0011
//PORTD = 0x90;//左转//1001 0000快速左转//PORTA = 0x33;//0011,0011
//PORTD = 0x80;//左转//1000 0000慢速左转//PORTA = 0xF3;//1111,0011
//PORTD = 0x60;//右转//0110 0000快速右转//PORTA = 0xCC;//1100,1100
//PORTD = 0x20;//右转//0010 0000慢速右转//PORTA = 0xCF;//1100,1111
//PORTD = 0x50;//后退//0101 0000 //PORTA = 0x3C;//0011,1100
//PC3声控启动
// 用PD7~PD4来作为电机驱动端
void xuanya_car(void)
{
port_init(); //PA,PB,PC,PD 初始化
PORTA = 0xa5;//1010,0101
delay_ms(1000);
work_status = 0x20;//置对应的工作状态标志
gzsl=0x88;timers=0;t0_times=0;
CLI(); //disable all interrupts
timer0_init();
MCUCR = 0x00;
GIMSK = 0x00;
TIMSK|=(1<<TOIE0);
SEI(); //re-enable interrupts
mic_startup();//等待声控启动函数
gzsl=0x44;t0_times=0;
TCCR1A = 0x00;
TCCR1B =(1<<CS11);//8分频
xuanya_car_rst:
temp=temp1 = 0;
sound_add = 0;
while(temp1!=0)
{
temp=temp1;
temp&=0x0f;
if(temp!=0)
{
TIMSK|=(1<<TOIE1);//根据SOUNDTABLE中数据的低四位选择音调频率
temp--;
toneconst=TONETABLE[temp];
TCNT1=toneconst;
}
temp=temp1;
temp>>=4;
temp&=0x0f;
delay_ms(temp*129);//根据SOUNDTABLE中数据的高四位*5后,为音调持续时间
TIMSK&=~(1<<TOIE1);
sound_add++;
}
goto xuanya_car_rst;
}
void remote_car()
{//遥控车程序
port_init();//调用端口初始化函数
work_status = 0x60;//置对应的工作状态标志
remote_rst:
PORTA = 0x0F;
delay_ms(1000);
gzsl=0x88;timers=0;t0_times=0;
CLI(); //disable all interrupts
timer0_init();
MCUCR = 0x00;
GIMSK = 0x00;
TIMSK|=(1<<TOIE0);
SEI(); //re-enable interrupts
mic_startup();//等待声控启动函数
gzsl=0x33;t0_times=0;
TCCR1A = 0x00;
TCCR1B =(1<<CS11);//8分频
n01:
temp=temp1 = 0;
sound_add = 0;
while(temp1!=0)
{
temp=temp1;
temp&=0x0f;
if(temp!=0)
{
TIMSK|=(1<<TOIE1);//根据SOUNDTABLE中数据的低四位选择音调频率
temp--;
toneconst=TONETABLE[temp];
TCNT1=toneconst;
}
temp=temp1;
temp>>=4;
temp&=0x0f;
delay_ms(temp*129);//根据SOUNDTABLE中数据的高四位*5后,为音调持续时间
TIMSK&=~(1<<TOIE1);
sound_add++;
}
goto n01;
}
void auto_car()
{
port_init();//PA,PB,PC,PD 初始化
work_status = 0x51;//置对应的工作状态标志
PORTA = 0x44;//0100,0100
delay_ms(1000);
delay_time1=22; //转弯时间 22*25ms = 550ms
delay_time2=48; //前行时间 48*25ms = 1200ms
gzsl=0x88;timers=0;t0_times=0;
CLI(); //disable all interrupts
timer0_init();
MCUCR = 0x00;
GIMSK = 0x00;
TIMSK|=(1<<TOIE0);
SEI(); //re-enable interrupts
mic_startup();//等待声控启动
gzsl=0x11;timers=0;t0_times=0;
forward(); //前行
delay_ms(200);
while(1)
{
music_car();
}
}
//cny_car 探测白底黑线,沿着黑线运动
void cny_car()
{
port_init(); //PA,PB,PC,PD 初始化
PORTA = 0xc3; //1100,0011
delay_ms(1000);
cny_car_start:
gzsl=0x88;timers=0;t0_times=0;
CLI(); //disable all interrupts
timer0_init();
MCUCR = 0x00;
GIMSK = 0x00;
TIMSK|=(1<<TOIE0);
SEI(); //re-enable interrupts
mic_startup(); //等待声控启动
while(1)
{
sw_touch();
cny_in = PINC & 0xe0;
if(cny_in == 0x00 || cny_in == 0x40 || cny_in == 0xa0)
{//前行
forward(); //前行
cny_times = 0x00;
}
if(cny_in == 0x20 || cny_in == 0x60)
{//慢速右转
turn_right_s(); //慢速右转
delay_ms(cny_delayms);
cny_times = 0x00;
}
if(cny_in == 0x80 || cny_in == 0xc0)
{//慢速左转
turn_left_s(); //慢速左转
delay_ms(cny_delayms);
cny_times = 0x00;
}
if(cny_in == 0xe0)
{//前行--探测多次都为 0xe0 ,停止运行
forward();
cny_times++;
delay_ms(10);
if(cny_times == 0x64)
{
stop(); //停止运动//熄灭指示灯
goto cny_car_start;
}
}
}
}
void main()
{
port_init(); //PA,PB,PC,PD 初始化
work_status = 0x67; //置对应的工作状态标志
PORTA = 0x7e;
delay_ms(2000);
CLI(); //disable all interrupts
timer0_init();
MCUCR = 0x00;
GIMSK = 0x00;
TIMSK|=(1<<TOIE0);
SEI(); //re-enable interrupts
gzsl=0x88;t0_times=0;sound_1 = 0x88;
mic_startup(); //等待声控起动
gzsl=0x55;t0_times=0;sound_1 = 0x88;
TCCR1A = 0x00;
TCCR1B =(1<<CS11);//8分频
n01:
temp=temp1 = 0;
sound_add = 0;
while(temp1!=0)
{
temp=temp1;
temp&=0x0f;
//PA口为音乐灯
if(temp==1 || temp==2 || temp==3 || temp==4){PORTA = 0xfe;}
if(temp==5 || temp==6){PORTA = 0xfc;}
if(temp==7 || temp==8 ){PORTA = 0xf8;}
if(temp==9){PORTA = 0xf0;}
if(temp==10){PORTA = 0xe0;}
if(temp==11){PORTA = 0xc0;}
if(temp==12){PORTA = 0x80;}
if(temp==13||temp==14 || temp==15){PORTA = 0x00;}
if(temp!=0)
{
TIMSK|=(1<<TOIE1);//根据SOUNDTABLE中数据的低四位选择音调频率
temp--;
toneconst=TONETABLE[temp];
TCNT1=toneconst;
}
temp=temp1;
temp>>=4;
temp&=0x0f;
delay_ms(temp*129);//根据SOUNDTABLE中数据的高四位*5后,为音调持续时间
TIMSK&=~(1<<TOIE1);
sound_add++;
/******************************************************/
pd_select = PIND & 0x01;
if(pd_select == 0x00)//pd0=0,其它为高电平
{
while(1)
{
delay_ms(500);//消除抖动
pd_select = PIND & 0x0f;
if(pd_select == 0x0e)//pd0=0,其它为高电平
{
stop_timer0();
remote_car();//运行遥控车程序
}
else
break;
}
}
pd_select = PIND & 0x02;
if(pd_select == 0x00)//pd1=0,其它为高电平
{
while(1)
{
delay_ms(500);//消除抖动
pd_select = PIND & 0x0f;
if(pd_select == 0x0d)//pd1=0,其它为高电平
{
stop_timer0();
cny_car();//寻迹机器人,探测白底黑线,沿黑线行进
}
else
break;
}
}
pd_select = PIND & 0x04;
if(pd_select == 0x00)//pd2=0,其它为高电平
{
while(1)
{
delay_ms(500);//消除抖动
pd_select = PIND & 0x0f;
if(pd_select == 0x0b)//pd2=0,其它为高电平
{
stop_timer0();
xuanya_car();//机器人避障碍、避悬崖行进
}
else
break;
}
}
pd_select = PIND & 0x08;
if(pd_select == 0x00)//pd3=0,其它为高电平
{
while(1)
{
delay_ms(500);//消除抖动
pd_select = PIND & 0x0f;
if(pd_select == 0x07)//pd3=0,其它为高电平
{
stop_timer0();
auto_car(); //机器人按程序设定路线行走
}
else
break;
}
}
/********************************************************/
remote_select = PINB & 0x0f;//检测遥控器是否有键按下
if(remote_select == 0x04)// A
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x04)// A
{
stop_timer0();
remote_car();//运行遥控车程序
}
else
break;
}
}
if(remote_select == 0x02)// B
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x02)// B
{
stop_timer0();
cny_car();//寻迹机器人,探测白底黑线,沿黑线行进
}
else
break;
}
}
if(remote_select == 0x08)// C
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x08)// C
{
stop_timer0();
xuanya_car();//机器人避障碍、避悬崖行进
}
else
break;
}
}
if(remote_select == 0x01)// D
{
while(1)
{
delay_ms(500);//消除抖动
remote_select = PINB & 0x0f;
if(remote_select == 0x01)// D
{
stop_timer0();
auto_car();//机器人按程序设定路线行走
}
else
break;
}
}
}
sound_1 = 0x88;
goto n01;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -