循迹小车的C程序谁有? 用51单片机编写的

如题所述

第1个回答  2012-07-15
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit ls=P2^6; //左侧传感器检测信号
sbit rs=P2^7; //右侧传感器检测信号
sbit left=P1^2; //左电机控制端
sbit right=P1^5;//右电机控制端
void init1();
void init2();
uchar z,tt=0;
uchar scan();
uchar put();
void main()
{
init1();
while(1)
{
switch(scan())
{
case 1: P1=0xed;break; //直走
case 2: init2();
while(ls==1&&rs==0)
{
left=put();
right=1;
}
break; //左转
case 3: init2();
while(ls==0&&rs==1)
{
left=1;
right=put();
}
break; //右转
case 4: P1=0xc9; break; //停止
}
}
}
void init1()
{
ls=1;
rs=1;
P1=0xed;
}
void init2()
{
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
}
uchar scan() /*轨道检测*/
{
if(ls==1&&rs==1) z=1;//直走
else if(ls==1&&rs==0) z=2;//左转
else if(ls==0&&rs==1) z=3;//右转
else z=4;//停止
return(z);
}
uchar put() //PWM波模拟
{
uchar pwm ;
if(tt<=2)
pwm=1;
else
pwm=0;
if(tt==10)
tt=0;
return(pwm);
}
void timer0() interrupt 1
{
TH0=(65536-100)/256;
TL0=(65536-100)%256;
tt++;
}
第2个回答  2012-07-01
有什么要求吗 不同的硬件程序也不一样啊追问

我最近买了一块光电传感器ST188,小车底盘,比较器LM324及电机驱动L298N。一块52单片机。简单的实现循迹壁障。可以帮忙写一段程序,以及一张总的电路图吗?谢谢啊