直接贴一个,没用算法的循迹小车
#include <iom128v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
void delay_ms(int time)
{
int i;
for(;time>0;time--)
for(i=0;i<1140;i++);
}
void InitDC(void)
{
DDRE|=(BIT(0)|BIT(1)|BIT(2)|BIT(3));
PORTE|=(BIT(0)|BIT(1)|BIT(2)|BIT(3));
}
void run(void)
{
PORTE=0x96;
}
void behind(void)
{
PORTE=0x69;
}
void right(void)
{
PORTE=0xa5;
}
void left(void)
{
PORTE=0x5a;
}
void stop(void)
{
PORTE=0xff;
}
void scan_xunji(void)
{
uchar j;
j=PINB;
j&=0x07;
switch(j)
{
case 0x00: stop(); break;
case 0x01: left(); break;
case 0x02: break;
case 0x03: left(); break;
case 0x04: right(); break;
case 0x05: run(); break;
case 0x06: right(); break;
case 0x07: stop(); break;
default: break;
}
}
void main(void)
{
DDRB=0x00;
PORTB=0xff;
DDRE=0xff;
PORTE=0xff;
while(1)
{
scan_xunji();
}
}
|