基于51单片机智能小车设计循迹+避障超声波红外(Proteus仿真+Keil源码+设计文档+AD原理图等)DS18B20 附下载链接!

提供对应的Proteus仿真电路图,Keil程序源码(C语言),AD原理图,模块资料、元件清单、程序流程图等电子资料参考学习,文末附全部资料的下载链接

01设计要求

设计一个基于51单片机智能小车设计循迹+避障超声波红外DS18B20系统:

  1. 小车双超声波避障功能;

  2. 小车红外循迹功能;

  3. 小车DS18B20温度采集功能;

  4. 按键控制车左转右转功能;

  5. LCD1602液晶屏显示距离、温度等信息功能。

02Proteus仿真

03示例代码

#include <reg52.h>
#include <intrins.h>
​
typedef unsigned int uint;
typedef int int;
​
// 全局引脚/变量声明(省略底层驱动定义)
sbit Trig_P  = P1^0;    // 左超声波触发脚
sbit Trig_P1 = P1^1;    // 右超声波触发脚
sbit Mode_SET= P3^0;    // 自动/手动模式切换
sbit RUN_left= P3^1;    // 手动左转按键
sbit RUN_right=P3^2;    // 手动右转按键
sbit LED     = P2^0;    // 报警指示灯
sbit BEEP    = P2^1;    // 蜂鸣器
float gSpeed;           // 超声波声速
​
/*********************************************************
* 主函数:双模式小车控制
* Mode_SET=1:自动避障(双超声波+红外循迹)
* Mode_SET=0:手动按键控制
*********************************************************/
void main(void)
{
    uint hw_l, hw_r;            // 左右红外循迹AD采样值
    uint dist_l, dist_r;        // 左右超声波测距结果
    int temp;                   // DS18B20采集温度
    int t = 0;                  // 循环计数分频变量
​
    Trig_P  = 0;
    Trig_P1 = 0;
​
    LcdInit();                  // LCD1602液晶初始化
    TMOD = 0x11;                // T0/T1设为16位定时器,用于超声波计时
​
    // 液晶固定标题显示
    LcdGotoXY(0, 0);
    LcdPrintStr("T:  ");
    LcdGotoXY(1, 0);
    LcdPrintStr("L:  cm  R:  cm");
​
    // 等待DS18B20传感器稳定(上电默认850为无效值)
    while(DS18B20_ReadTemp() == 850)
        DelayMs(10);
​
    while(1)
    {
        // 每3次循环刷新温度、声速、双超声波距离
        if(t % 3 == 0)
        {
            temp = DS18B20_ReadTemp();
            LcdGotoXY(0, 2);
            LcdPrintTemp(temp);
​
            // 声速公式 v=0.607*T+331.4,转换单位 cm/us
            gSpeed = 0.607 * (temp / 10) + 331.4;
            gSpeed = gSpeed / 10000;
​
            // 读取左超声波距离并刷新LCD
            dist_l = GetDistance();
            LcdGotoXY(1, 2);
            LcdPrintNum(dist_l);
            // 读取右超声波距离并刷新LCD
            dist_r = GetDistance1();
            LcdGotoXY(1, 0x0a);
            LcdPrintNum(dist_r);
        }
​
        // ========== 自动避障模式 ==========
        if(Mode_SET == 1)
        {
            // 每5次循环刷新左右红外循迹数值
            if(t % 5 == 0)
            {
                hw_l = adc08322_2(0) * 99 / 255;
                LcdGotoXY(0, 0x09);
                LcdPrintNum(hw_l);
​
                hw_r = adc08322_2(1) * 99 / 255;
                LcdGotoXY(0, 0x0c);
                LcdPrintNum(hw_r);
            }
​
            // 左右无障碍物、红外居中:直行
            if((dist_l >= 30) && (hw_l == hw_r) && (dist_r >= 30))
                MotorFront(20);
            // 左侧近障/红外左偏:右转
            if((dist_l >= 10 && dist_l < 30) || (hw_l > hw_r) && (dist_r >= 30))
                MotorRight(30);
            // 右侧近障/红外右偏:左转
            if((dist_r >= 10 && dist_r < 30) || (hw_l < hw_r) && (dist_l >= 30))
                MotorLeft(30);
            // 任意一侧障碍物小于10cm:立即停车
            if((dist_l < 10) || (dist_r < 10))
                MotorStop();
​
            // 10~30cm中等距离障碍:声光交替报警
            if((dist_l >= 10 && dist_l < 30) || (dist_r >= 10 && dist_r < 30))
            {
                LED = 0;
                BEEP = 0;
                DelayMs(300);
                BEEP = 1;
                DelayMs(300);
            }
            else // 无近距离障碍,关闭报警
            {
                LED = 1;
                BEEP = 1;
            }
​
            DelayMs(100);
            t++;
            if(t > 10000) t = 0; // 防止计数溢出
        }
        // ========== 手动按键控制模式 ==========
        else if(Mode_SET == 0)
        {
            if(RUN_left == 0)
                MotorLeft(30);
            else if(RUN_right == 0)
                MotorRight(30);
            else
                MotorFront(20);
        }
    }
}

04原理图

注:

  1. 源码使用Keil4或Keil5打开

  2. 仿真使用的是Proteus8.13及以上版本,版本过低可能会打不开。

05资料获取

1)项目完整资料获取链接:下载链接

2)Keil、Proteus各个版本安装包: 下载链接

posted @ 2026-06-29 10:58  _Passion  阅读(81)  评论(0)    收藏  举报