51单片机的头文件可以直接在15单片机上使用吗?例如15使用“reg51.h”头文件时需要注意哪些事项。
我写了一个智能车的红外跟随程序 大神帮忙看看哪里出错了
#include <REGX52.H>
sbit LeftIR=P0^0; //定义前方左侧红外探头端口
sbit RightIR=P0^1; //定义前方右侧红外探头端口
sbit ZhongIR=P0^2; //定义中间红外探头端口
sbit xiao=P0^3; //根据此红外信号有无,判断跟随距离是否过近
sbit M1A=P1^0; //电机左右引脚口
sbit M1B=P1^1;
sbit M2A=P1^2;
sbit M2B=P1^3;
sbit ENA0=P1^4; //电机控制使能端
sbit ENA1=P1^5;
unsigned char Xinhao;
/**********毫秒延时**********/
void delay_nms(unsigned int t) //毫秒延时
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<120;j++);
}
/**********信号处理**********/
void Xhao(Xinhao)
{
if(xiao==0)
{
Xinhao=0;
}
if( LeftIR ==1 && ZhongIR==1 && RightIR ==1 )
{
Xinhao=1;
}
if( LeftIR ==0 && ZhongIR==0 && RightIR ==0 )
{
Xinhao=2;
}
if( LeftIR ==0 && ZhongIR==1 && RightIR ==1 )
{
Xinhao=3;
}
if( LeftIR ==1 && ZhongIR==0 && RightIR ==1 )
{
Xinhao=4;
}
if( LeftIR ==1 && ZhongIR==1 && RightIR ==0 )
{
Xinhao=5;
}
if( LeftIR ==0 && ZhongIR==0 && RightIR ==1 )
{
Xinhao=6;
}
if( LeftIR ==0 && ZhongIR==1 && RightIR ==0 )
{
Xinhao=7;
}
if( LeftIR ==1 && ZhongIR==0 && RightIR ==0 )
{
Xinhao=8;
}
}
/**********电机运行方式**********/
void tingzhi()
{
M1A=0; //将M1电机A端初始化为0
M1B=0; //将M1电机B端初始化为0
M2A=0; //将M2电机A端初始化为0
M2B=0;
}
void qianjin()
{
M1A=1;
M1B=0;
M2A=1;
M2B=0;
}
void houtui()
{
delay_nms(30);
M1A=0;
M1B=1;
M2A=0;
M2B=1;
}
void zuozhuan()
{
M1A=1;
M1B=0;
M2A=0;
M2B=0;
}
void youzhuan()
{
M1A=0;
M1B=0;
M2A=1;
M2B=0;
}
/**********信号电机综合方式**********/
void Yunxing(unsigned char Yxing)
{
switch(Yxing)
{
case 0:
{
tingzhi();
break;
}
case 1:
{
tingzhi();
break;
}
case 2:
{
qianjin();
break;
}
case 3:
{
zuozhuan();
break;
}
case 4:
{
qianjin();
break;
}
case 5:
{
youzhuan();
break;
}
case 6:
{
zuozhuan();
break;
}
case 7:
{
qianjin();
break;
}
case 8:
{
youzhuan();
break;
}
}
}
/**********主程序**********/
void main()
{
Yunxing(0);
while(1)
{
switch(Xinhao)
{
case 0:
{
Yunxing(0);
break;
}
case 1:
{
Yunxing(1);
break;
}
case 2:
{
Yunxing(2);
break;
}
case 3:
{
Yunxing(3);
break;
}
case 4:
{
Yunxing(4);
break;
}
case 5:
{
Yunxing(5);
break;
}
case 6:
{
Yunxing(6);
break;
}
case 7:
{
Yunxing(7);
break;
}
case 8:
{
Yunxing(8);
break;
}
}
}
}
|