?? remote_car.c
字號:
#include <io8515v.h>
#include <macros.h>
#include <sl3010.c>
//聲控起動遙控車程序,未起動時,PA口的8 LED 呈廣告燈效果(霓虹燈)
//聲控起動后,機器人開始動作
//遙控器無按鍵,機器人直行
//按住 < A > 鍵,機器人右轉,同時演奏右轉指示音樂;
//按住 < B > 鍵,機器人后退,同時演奏后退指示音樂;
//按住 < C > 鍵,機器人停止,同時演奏停止指示音樂;
//按住 < D > 鍵,機器人左轉,同時演奏左轉指示音樂;
//釋放遙控器按鍵,音樂停止
//連續(xù) 按住 < C > 鍵時間超過 10s,遙控機器人復位,等待聲控重新起動
/* 音階表 */
const unsigned int TONETABLE[15]={DO,RE,MI,FA,SO,LA,TI,DO_H,
RE_H,MI_H,FA_H,SO_H,LA_H,TI_H,DO_HH};
/* 音階重裝常數 */
unsigned int toneconst;
unsigned int sound_add=0;
unsigned int delay_time1,delay_time2,led_delayms=600;
unsigned char temp,temp1;
unsigned char pa,pb,pc,pd,remote_in,timers,cny_in,cny_times,rst_time,cds,pd_select,remote_select;
unsigned char gzsl,t0_times;
unsigned char sound_1,sound_2;
const unsigned char led_data[]=
{//設置LED廣告燈數據表
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,
0x00,0x18,0x3c,0x7e,0xff,0x7e,0x3c,0x18,
0x00,0x18,0x3c,0x7e,0xff,0x7e,0x3c,0x18,
0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x8f,0xc7,0xe3,0xf1,
0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x8f,0xc7,0xe3,0xf1,
0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,
0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,
0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,
0xff,0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,
0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,
0x0a,0x0a
};
const unsigned char sound_none[]={0x28,0x27,0x36,0x16,0x45,0x47,0x46,0x46,0x46,0x23,0x25,0x46,0x4a,0x39,0x18,0x27,
0x28,0x48,0x48,0x28,0x27,0x36,0x16,0x45,0x27,0x26,0x46,0x46,0x46,0x46,0x46,0x46,0x00,0x00};
const unsigned char sound_right[]={0x23,0x25,0x46,0x4a,0x39,0x18,0x27,0x28,0x48,0x48,0x48,
0x28,0x27,0x36,0x16,0x45,0x47,0x46,0x46,0x46,0x00,0x00};
const unsigned char sound_back[]={0x2a,0x2c,0x4d,0x4d,0x4d,0x2a,0x2c,0x49,0x49,0x49,0x2a,0x2c,0x2d,0x4f,
0x2d,0x4c,0x1d,0x18,0x2a,0x49,0x49,0x49,0x00,0x00};
const unsigned char sound_stop[]={0x23,0x24,0x25,0x44,0x45,0x24,0x23,0x22,0x42,0x40,0x40,0x23,0x22,0x21,0x41,0x28,
0x48,0x23,0x25,0x45,0x40,0x40,0x00,0x00};
const unsigned char sound_left[]={0x68,0x26,0x25,0x23,0x22,0x13,0x12,0x41,0x41,0x41,0x23,0x25,
0x28,0x48,0x28,0x29,0x2a,0x29,0x26,0x45,0x45,0x45,0x00,0x00};
//TIMER0 initialisation - prescale:1024
// desired value: 25mSec
// actual value: 24.960mSec (0.2%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop timer
TCNT0 = 0x3D; //set count value
TCCR0 = 0x05; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIMER0_OVF
void timer0_ovf_isr(void)
{
TCNT0 = 0x3D; //reload counter value
t0_times++;
if(gzsl == 0x88)
{
if(t0_times == 10)
{
timers++;
t0_times = 0;
pa = led_data[timers];
if(pa==0x0a){timers=0;}
PORTA = pa;
}
}
if(gzsl == 0x33)
{
sw_touch();
if(t0_times == 8) //8*25ms=200ms
{
t0_times = 0;
remote_in = PINB & 0x0F;
sound_1 = remote_in;
if(sound_2 != sound_1)
{sound_add=0;sound_2 = sound_1;}
switch (remote_in)
{//檢測遙控器是否有鍵按下
case 0x00:
forward(); //前行
rst_time=0;
sound_add=0x00;
temp1=0x00;
break;
case 0x04: // < A >
turn_right(); //右轉
rst_time=0;
temp1=sound_right[sound_add];
break;
case 0x02: // < B >
backward(); //后退
rst_time=0;
temp1=sound_back[sound_add];
break;
case 0x08: // < C >
stop(); //停止運動
rst_time++;
temp1=sound_stop[sound_add];
break;
case 0x01: // < D >
turn_left(); //左轉
rst_time=0;
temp1=sound_left[sound_add];
break;
}
}
}
}
void stop_timer0()
{
CLI();
TCCR0 = 0x00;//停止timer0
MCUCR = 0x00;
GIMSK = 0x00;
TIMSK = 0x00;
SEI();
}
#pragma interrupt_handler timer1_ovf_isr:iv_TIMER1_OVF
void timer1_ovf_isr(void)
{
{
TCNT1=toneconst;
PORTC^=1;
}
}
void remote_car()
{//遙控車程序
port_init();//調用端口初始化函數
work_status = 0x60;//置對應的工作狀態(tài)標志
remote_rst:
PORTA = 0x0F;
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后,為音調持續(xù)時間
TIMSK&=~(1<<TOIE1);
sound_add++;
if(rst_time > 40)
{//當按住< C > 鍵的持續(xù)時間超過10S,遙控機器人復位,等待聲控重起運行
rst_time=0;
stop_timer0();
PORTA = 0x0f;
delay_ms(1000);
goto remote_rst;
}
}
goto n01;
}
void main()
{
remote_car();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -