📄 xuanya_car._c
字号:
// 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来作为电机驱动端
#include <io8515v.h>
#include <macros.h>
#include <eeprom.h>
#include <sl3010.c>
#define xuanya_delayms 300
//声控起动避悬崖,壁障碍运行程序,未起动时,PA口的8 LED 呈广告灯效果(霓虹灯)
//声控起动后,机器人开始动作
//探测到边缘,后退、转弯,并以伴有音乐提示。
#pragma data:eeprom
unsigned char sound[]={ //乐曲数据表
//铃儿响叮当
0x2a,0x2a,0x4a,0x2a,0x2a,0x4a,0x2a,0x2c,0x28,0x29,
0x4a,0x4a,0x2b,0x2b,0x2b,0x2b,0x2b,0x2a,0x2a,0x2a,
0x2a,0x29,0x29,0x28,0x49,0x4c,0x2a,0x2a,0x4a,0x2a,
0x2a,0x4a,0x2a,0x2c,0x28,0x29,0x4a,0x4a,0x2b,0x2b,
0x2b,0x2b,0x2b,0x2a,0x2a,0x2a,0x2c,0x2c,0x2b,0x29,
0x48,0x48,
0x00,0x00
};
#pragma data:data
/* 音阶表 */
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 rst_time,delay_time1,delay_time2,led_delayms=600;
unsigned char temp,temp1;
unsigned char pa,pb,pc,pd,remote_in,cny_in,cny_times,cds,pd_select,remote_select;
unsigned char gzsl,t0_times,timers;
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_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};
//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 == 0x44)
{
sw_touch();
pc = PINC & 0xE0;
if(pc == 0xE0)
{
stop();
t0_times = 0;timers = 0x22;
temp1=EEPROMread((int)&sound[sound_add]);
}
if(pc == 0x40 || pc == 0x80 || pc == 0x20 || pc == 0xc0 || pc == 0x60)
{
timers=0x55;
t0_times = 0;
}
if(timers == 0x55)
{
if(t0_times < 16) //16*25ms=400ms
{
backward();
temp1=sound_back[sound_add];
}
if(t0_times > 16 && t0_times < 24) //8*25ms=200ms
{
turn_left();
temp1=sound_back[sound_add];
}
if(t0_times == 24)
{
timers = 0;
t0_times = 0;
}
}
if(timers != 0x55 && timers != 0x22)
{
forward();
temp1 = 0;
}
}
}
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 xuanya_car(void)
{
port_init(); //PA,PB,PC,PD 初始化
work_status = 0x20;//置对应的工作状态标志
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=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 main()
{
xuanya_car();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -