目录
一、主要功能
基于51单片机,避障,低于50CM报警,LED灯亮起,自动翻晒用光敏电阻,光照强度大,电机转动,相当于翻晒粮食,远程控制用按键代替,按下去电机就转动,相当于翻晒粮食,然后光照强度和超声波的距离都在LCD1602显示屏显示出来。
二、硬件资源
基于KEIL5编写C++代码,PROTEUS8.15进行仿真,全部资源在页尾,提供安装包。
1、51单片机
2、超声波传感器
3、光照传感器
4、ADC0832数模转换器
5、L298N驱动电机模块
6、LCD1602显示模块
7、灯光报警模块
8、按键模块
仿真图:
编辑
三、程序编程
cpp
/*所有代码资源看页尾*/
#include <REGX52.H>
#include<intrins.h>
#include<stdio.h>
#include "Delay.h"
#include "LCD1602.h"
#define uchar unsigned char
#define uint unsigned int
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
sbit CS=P1^0; //51单片机引脚设置
sbit CLK=P1^1;
sbit DIO=P1^2;
sbit Motor1_IN1 = P3^3; //电机IN1口
sbit Motor1_IN2 = P3^4;
sbit Motor1_EN = P3^2; //电机使能端1
sbit Motor2_IN1 = P3^5; //电机IN1口
sbit Motor2_IN2 = P3^6;
sbit Motor2_EN = P3^7; //电机使能端2
sbit LED = P1^5; //LED灯定义
sbit key = P1^4;
sbit key1 = P1^6;
sbit BEEP = P1^3;
sbit Tr=P3^0;//触发信号
sbit Ec=P3^1;//回响信号
uchar Recv_Buffer\[4\];
uint Voltage\[\]={'0','0','0','0'};
bit bdata IIC_ERROR;
unsigned char count;
unsigned int distance;
static uint temp;
uint temp;
static unsigned char num;
static int flag = 0, flag1 = 0;
unsigned int Read_value(void);//读值函数
void Delay10us(void);//10us延时函数
void Time0_Init() //定时器初始化
{
TMOD = 0x01;
TH0 = 0x00;
TL0 = 0x00;
TR0 = 0;//先关闭定时器0
}
void Time0_Int() interrupt 1 //中断程序
{
TH0 = 0xfe; //重新赋值
TL0 = 0x33;
}
uchar get\_AD\_Res() //ADC0832启动读取函数
{
uchar i, data1=0, data2=0;
CS=0;
CLK=0;DIO=1;\_nop\_();
CLK=1;\_nop\_();
CLK=0;DIO=1;\_nop\_();
CLK=1;\_nop\_();
CLK=0;DIO=0;\_nop\_();
CLK=1;\_nop\_();
CLK=0;DIO=1;\_nop\_();
for(i=0; i<8; i++)
{
CLK=1;\_nop\_();
CLK=0;\_nop\_();
data1=(data1<<1)|(uchar)DIO;
}
for(i=0; i<8; i++)
{
data2=data2|(uchar)DIO<<i;
CLK=1;\_nop\_();
CLK=0;\_nop\_();
}
CS=1;
return(data1 == data2)?data1:0;
}
unsigned int Read_value()
{
uint result;
Tr=1;//触发引脚发出11us的触发信号(至少10us)
Delay10us();
Tr=0;
while(!Ec);//度过回响信号的低电平
TR0=1;//开启定时器0
while(Ec);//度过回响信号高电平
TR0=0;//关闭定时器0
result=((TH0\*256+TL0)\*0.034)/2; // 距离cm=(时间us * 速度cm/us)/2
return result + 2; //+2修正补偿
}
void main() //主函数
{
uchar u;
Tr=0; //出发引脚首先拉低
LCD_Init(); //显示屏初始化
LCD_ShowString(1,1,"Distance:"); //显示字符串Distance
LCD_ShowString(2,1,"GZQD:"); //显示字符串GZQD 光照强度
Time0_Init(); //定时初始化
Motor2_EN = 1; //顺时针
Motor2_IN1 = 1;
Motor2_IN2 = 0;
LED = 0;
BEEP = 0;
while(1)
{
u=get\_AD\_Res(); //读取光照强度
distance = Read_value();//读值
distance-=1;
LCD_ShowNum(1,10,distance,3); //显示距离
LCD_ShowNum(2,6,u,3); //显示光照强度
if( (distance>=50) && (distance<= 100))
{
Motor2_EN = 1; //逆时针
Motor2_IN1 = 0;
Motor2_IN2 = 1;
LED = 0; //LED灯熄灭
BEEP = 0; //蜂鸣器关掉
}
else if(distance < 50) //超声波距离判断
{
LED = 1; //LED灯亮起
BEEP = 1; //蜂鸣器响起
}
else if(distance > 100)
{
Motor2_EN = 1; //顺时针
Motor2_IN1 = 1;
Motor2_IN2 = 0;
}
if(u > 80) //光照强度判断
{
flag = 1;
}
else
{
flag = 0;
}
if(key == 0)
{
Delay(100); //消除抖动
if(key == 0)
{
flag1 = 1;
}
}
if(key1 == 0)
{
Delay(100);//消除抖动
if(key1 == 0)
{
flag1 = 0;
}
}
if(flag == 1)
{
Motor1_EN = 1; //电机端使能
Motor1_IN1 = 1; //给IN1口赋值高电平
Motor1_IN2 = 0; //给IN2口赋值低电平,形成顺时针转动
}
else if(flag1 == 1)
{
Motor1_EN = 1; //电机端使能
Motor1_IN1 = 1; //给IN1口赋值高电平
Motor1_IN2 = 0; //给IN2口赋值低电平,形成顺时针转动
}
else
{
Motor1_EN = 0;
Motor1_IN1 = 0;
Motor1_IN2 = 0;
}
}
}
void Delay10us()
{
TL0=0xF5;
TH0=0xFF;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
四、实现现象
具体动态效果看B站演示视频:
基于51单片机的太阳能自动谷物翻晒机器人控制系统_哔哩哔哩_bilibili
全部资料(源程序、仿真文件、安装包、演示视频):
链接:https://pan.baidu.com/s/1Ewj05XL8mORAxrL9C3kejA
提取码:w4e9
--来自百度网盘超级会员V4的分享