41 基于单片机的小车行走加温湿度检测系统
目录
一、主要功能
基于51单片机,采样DHT11温湿度传感器检测温湿度,滑动变阻器连接数码转换器模拟电量采集传感器,
电机采样L298N驱动,各项参数通过LCD1602显示,如果电量低于阈值,电机停止转动,小车停止前进;
如果电流高于阈值,则电机转动,这个电机采样两个电机,一个负责纵向,一个负责横向,纵向横向来回
转换;
如果温度/湿度高于程序里设置好得阈值 湿度阈值70,则蜂鸣器发出警报;
二、硬件资源
基于KEIL5编写C++代码,PROTEUS8.15进行仿真,全部资源在页尾,提供安装包。
编辑
编辑
三、程序编程
#include <REGX52.H>#include<intrins.h>#include<stdio.h>#include "Delay.h"#include "LCD1602.h"#define uchar unsigned char#define uint unsigned int
sbit CS=P1^0; //adc0832引脚sbit CLK=P1^1;
sbit DIO=P1^2;
sbit beep=P3^4;
sbit out1 = P3^6;
sbit out2 = P3^7;
sbit out3 = P3^0;
sbit out4 = P3^1;
sbit Temp\_data=P2^6; //DHT11unsigned char rec\_dat\_lcd0\[6\];unsigned char rec\_dat\_lcd1\[6\];unsigned char rec\_dat\_lcd2\[6\];unsigned char rec\_dat\_lcd3\[6\];unsigned int rec\_dat\[4\];static double u,U;static uchar wd,sd;static int num,num1,flag,flag1=0;static int wdyz=40,sdyz=70,dlyz=30;void DHT11\_delay\_us(unsigned char n);void DHT11\_delay\_ms(unsigned int z);void DHT11\_start();unsigned char DHT11\_rec\_byte();void DHT11\_receive();void beep\_warning();void cshq();void xspxs();void motorzx();void motorhx();//延时msvoid DHT11\_delay_ms(unsigned int z){ unsigned int i,j; for(i=z; i>0; i--) for(j=110; j>0; j--);
}//延时us --2*n+5usvoid DHT11\_delay\_us(unsigned char n){ while(--n);
}//DHT11起始信号void DHT11_start(){
Temp\_data=1; DHT11\_delay_us(10);
Temp\_data=0; DHT11\_delay_ms(50);//这个延时不能过短,18ms以上,实际在仿真当中要想读到数据延时要在延时参数要在40以上才能出数据
Temp\_data=1; DHT11\_delay\_us(30);//这个延时不能过短}//接收一个字节unsigned char DHT11\_rec_byte(){ unsigned char i,dat=0; for(i=0; i<8; i++)
{ while(!Temp\_data); DHT11\_delay_us(8);
dat <<=1; if(Temp_data==1)
{
dat +=1;
} while(Temp_data);
} return dat;
}//接收温湿度数据void DHT11\_receive(){ unsigned int R\_H,R\_L,T\_H,T\_L; unsigned char RH,RL,TH,TL,revise; DHT11\_start();
Temp\_data=1; if(Temp\_data==0)
{ while(Temp_data==0); //等待拉高
DHT11\_delay\_us(40); //拉高后延时80us
R\_H=DHT11\_rec_byte(); //接收湿度高八位
R\_L=DHT11\_rec_byte(); //接收湿度低八位
T\_H=DHT11\_rec_byte(); //接收温度高八位
T\_L=DHT11\_rec_byte(); //接收温度低八位
revise=DHT11\_rec\_byte(); //接收校正位
DHT11\_delay\_us(25); //结束
if((R\_H+R\_L+T\_H+T\_L)==revise) //校正
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
} /*数据处理,方便显示*/
rec_dat\[0\]=RH;
rec_dat\[1\]=RL;
rec_dat\[2\]=TH;
rec_dat\[3\]=TL;
}
}void dht11(){
TR1 = 0;// DHT11\_delay\_ms(150);
DHT11\_receive(); sprintf(rec\_dat\_lcd0,"%d",rec\_dat\[0\]); sprintf(rec\_dat\_lcd1,"%d",rec\_dat\[1\]); sprintf(rec\_dat\_lcd2,"%d",rec\_dat\[2\]); sprintf(rec\_dat\_lcd3,"%d",rec\_dat\[3\]); DHT11\_delay_ms(100);
wd = rec\_dat\[3\]*10 + rec\_dat\[2\];
sd = rec\_dat\[1\]*10 + rec\_dat\[0\];
TR1 = 1;
}void Time1Init()//定时器1{
TMOD |= 0x10;
TH1 = (65536-57567)/256;
TL1 = (65536-57567)%256;
EA = 1;
ET1 = 1;
TR1 = 1;
}void Time1Start() interrupt 3 //定时器1中断{
TH1 = (65536-57567)/256;
TL1 = (65536-57567)%256;
num++;if(num == 2)//达到1s
{dht11(); //温湿度获取
cshq(); //参数获取
xspxs(); //显示屏显示
beep_warning(); //状态判断
num = 0;
}
num1++;if(num1 == 2)
{if(flag1 == 0)
{motorzx();//纵向行走
Delay(200);
motorhx(); //横向行走
Delay(200);
}else
{
out1=0;
out2=0;
out3=0;
out4=0;
}
num1 = 0;
}
}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;
}void beep_warning()//蜂鸣器警报并且电机转动{if(wd>wdyz || sd>sdyz || U < dlyz)
{
flag = 1;
}else
{
flag = 0;
}
if(U<dlyz)
{
flag1 = 1;
}else
{
flag1 = 0;
}if(flag == 1)
{
beep = 1;
}else
{
beep = 0;
}
}void main() //主函数{
LCD_Init(); //显示屏初始化
Time1Init(); //定时器
beep = 0;while(1)
{
}
}void motorzx() //纵向{
out1 = 1;
out2 = 0;
out3 = 0;
out4 = 0;
}void motorhx() //横向{
out1 = 0;
out2 = 0;
out3 = 1;
out4 = 0;
}void xspxs() //显示屏显示{ LCD_ShowString(1,1,"wd:");
LCD_ShowNum(1,4,wd,2); //温度
LCD_ShowString(1,6,"sd:");
LCD_ShowNum(1,9,sd,2); //湿度
LCD_ShowString(1,11,"dl:");
LCD_ShowNum(1,14,U,2); //电量
LCD_ShowString(2,1,"wd:");
LCD_ShowNum(2,4,wdyz,2); //温度
LCD_ShowString(2,6,"sd:");
LCD_ShowNum(2,9,sdyz,2); //湿度
LCD_ShowString(2,11,"dl:");
LCD_ShowNum(2,14,dlyz,2); //电量
}void cshq() //参数获取{
u=get\_AD\_Res();
U=(u/255)*99;
}
四、实现现象
具体动态效果看B站演示视频:
基于单片机的小车行走加温湿度检测系统
全部资料(源程序、仿真文件、安装包、原理图、演示视频):
百度网盘资料下载https://pan.baidu.com/s/1T3THUMoisAv-Bmc30W869g?pwd=rnzb
原文地址:https://blog.csdn.net/szq0419/article/details/144128123
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!