当前位置:文档之家› 小车走迷宫程序

小车走迷宫程序

//这是迷宫小车传感器测试小程序
#include <mega8515.h>
#include <delay.h>
#include <stdio.h>
bit front;
bit L=1; //红外线检测到阻挡为 0
bit R=1;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
//产生红外线传感器发射所要的交流信号(频率:50mhz ~ 500mhz) TCNT0=225;
PORTB.0=!PORTB.0;
PORTB.2=!PORTB.2;
PORTB.4=!PORTB.4;
}
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
//检测装在PB.5口上的传感器
if(PINB.5){ R=0; PORTA=0xff; }
if(PINB.1){ L=0; PORTA=0xff; }
}
/*******************************************
// 蜂鸣器BB程序
*/////////////////////////////////////////// void buzzer(void)
{ unsigned int i=512;
for(;i--;) { PORTB.3=!PORTB.3; delay_us(250); } }
/*******************************************
// 小车“杀车”程序
*/////////////////////////////////////////// void black(void)
{
PORTD.6=!PORTD.6;
PORTD.4=!PORTD.4;
delay_ms(30);
OCR1AL=0; OCR1BL=0;
delay_ms(100);
}
/*******************************************
// 小车启动子程序
// speed=0~255 0为停止走动
// front=1 ,向前 front=0, 向后
*////////////////////////////////////////////// void RUN(unsigned char speed)
{ OCR1AL=speed;
OCR1BL=speed;
if (front) {PORTD.6=1; PORTD.4=1; }
else { PORTD.6=0; PORTD.4=0; }
}
/*********************************************
//转圈子程序
// p=‘R’垂直右转,p='L' 垂直左转,
// p='r' 以右轮为支点右转,p='l'以左轮为支点左转// m转向数值
*//////////////////////////////////////////////// void L_R(unsigned char p,unsigned char m)
{ unsigned char a_temp, b_temp;
switch ( p )
{
case 'R' : { PORTD.6=1; PORTD.4=0; while (m--)delay_ms(10); break; } case 'L' : { PORTD.6=0; PORTD.4=1; while (m--)delay_ms(10); break; } case 'r' : { PORTD.6=1; PORTD.4=0;
OCR1AL=0x00; while (m--)delay_ms(10); break; }
case 'l' : { PORTD.6=0; PORTD.4=1;
OCR1BL=0x00; while (m--)delay_ms(10); break; }
}
black();
OCR1AL=a_temp; OCR1BL=b_temp;
}
void test232(void)
{ //unsigned char i;
putchar(0x0d);
putchar(0x0a);
putchar(0x0d);
putchar(0x0a);
putsf("***********************************************\r");
putsf("* RB-101 机器人测试程序 *\r");
putsf("* *\r");
putsf("* 逻辑电子DIY 机器人制作天地 *\r");
putsf("* *\r");
putsf("* 简易灭火测试 *\r");
putsf("***********************************************\r");
//putsf("按任何一个键继续\r");
//i=getchar();
//putchar(i);
putchar(0x0d);
putchar(0x0a);
}
void main(void)
{
PORTA=0x00;
DDRA=0xff; //打开七段数码管,全部点亮
PORTB=0x00;
DDRB=0B00011101; //设置红外线传感器端口
PORTC=0x00;
DDRC=0xff;
PORTD=0b01010000;
DDRD=0b01110000;
PORTE=0x00;
TCCR0=0x04;
TCNT0=0x00;
OCR0=0x00;
TCCR1A=0xA1;
TCCR1B=0x03;
TCNT1H=0xff;
TCNT1L=0xff;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
EMCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x1A;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off // Analog Comparator Output: Off
ACSR=0x80;
#asm("sei")
//打开所有的前后LED
PORTB.6=1;
PORTB.7=1;
PORTC.6=1;
PORTC.7=1;
delay_ms(1000);
PORTA=0;
test232();
buzzer(); //蜂鸣器 delay_ms(100);
buzzer();
delay_ms(1000);
front=1; //前进
RUN(255); //开始行走 delay_ms(1000);
L=R=1;
while(1)
{ while(R*L);
if(!L) L_R('L',50);
if(!R) L_R('R',50); RUN(255);
R=1;L=1;PORTA=0;
#asm("wdr");
}
// run(0); //停止。

相关主题