51两轮循迹小车代码
时间: 2023-12-20 22:31:05 浏览: 179
循迹小车51做的代码
以下是51单片机两轮循迹小车的代码:
```c
#include <reg52.h>
sbit STBY1 = P1^0; //定义STBY1引脚
sbit STBY2 = P1^1; //定义STBY2引脚
sbit R1 = P2^0; //定义R1引脚
s R2 = P2^1; //定义R2引脚
sbit M = P2^2; //定义M引脚
sbit L1 = P2^3; //定义L1引脚
sbit L2 = P2^4; //定义L2引脚
void Motor_start() //启动
{
STBY1 = 1; //右两轮
STBY2 = 1; //后两轮
}
void Motor_Stop() //停止
{
STBY1 = 0; //右两轮
STBY2 = 0; //后两轮
}
void Motor_Forward() //前进
{
P1 = 0x0a; //右两轮正转,左两轮反转
}
void Left_Motor_Forward() //左转
{
P1 = 0x02; //右两轮正转,左两轮停止
}
void Right_Motor_Forward() //右转
{
P1 = 0x08; //右两轮停止,左两轮反转
}
void Speed(int speed) //设置速度
{
TH0 = 256 - speed; //设置定时器初值
TL0 = 256 - speed; //设置定时器初值
}
void main()
{
while(1)
{
if((R1==0)&&(M==1)&&(L1==0)) //前进
{
Speed(2);
Motor_Forward();
}
else if((R1==1)&&(R2==1)&&(M==1)&&(L1==1)&&(L2==1)) //停止
{
Motor_Stop();
}
else if((R1==1)&&(R2==0)&&(M==1)&&(L1==0)&&(L2==1)) //左转
{
Speed(2); Left_Motor_Forward();
}
else if((R1==0)&&(R2==1)&&(M==1)&&(L1==1)&&(L2==0)) //右转
{
Speed(2);
Right_Motor_Forward();
}
}
}
```
阅读全文