?? diy0206_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 = 0x60;//快速左轉//0110 0000//PORTA = 0x33;//0011,0011
//PORTD = 0x20;//慢速左轉//0010 0000//PORTA = 0xF3;//1111,0011
//PORTD = 0x90;//快速右轉//1001 0000//PORTA = 0xCC;//1100,1100
//PORTD = 0x80;//慢速右轉//1000 0000//PORTA = 0xCF;//1100,1111
//PORTD = 0x50;//后退 //0101 0000//PORTA = 0x3C;//0011,1100
//PB2聲控啟動
// 用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=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 & 0xf0;//檢測遙控器是否有鍵按下
if(remote_select == 0x20)// A
{
while(1)
{
delay_ms(500);//消除抖動
remote_select = PINB & 0xf0;
if(remote_select == 0x20)// A
{
stop_timer0();
remote_car();//運行遙控車程序
}
else
break;
}
}
if(remote_select == 0x40)// B
{
while(1)
{
delay_ms(500);//消除抖動
remote_select = PINB & 0xf0;
if(remote_select == 0x40)// B
{
stop_timer0();
cny_car();//尋跡機器人,探測白底黑線,沿黑線行進
}
else
break;
}
}
if(remote_select == 0x10)// C
{
while(1)
{
delay_ms(500);//消除抖動
remote_select = PINB & 0xf0;
if(remote_select == 0x10)// C
{
stop_timer0();
xuanya_car();//機器人避障礙、避懸崖行進
}
else
break;
}
}
if(remote_select == 0x80)// D
{
while(1)
{
delay_ms(500);//消除抖動
remote_select = PINB & 0xf0;
if(remote_select == 0x80)// D
{
stop_timer0();
auto_car();//機器人按程序設定路線行走
}
else
break;
}
}
}
sound_1 = 0x88;
goto n01;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -