51单片机之温湿度检测系统(DTH11、OLED、LCD1602)

DHT11原理分析以及常见问题解析(附:32 51源码) DHT11原理分析,常见问题解析,51 32源码 阅读详情
AI摘要

博客深入介绍了LCD1602液晶屏的引脚功能、初始化步骤、显示字符原理及与DTH11温湿度传感器的配合使用方法,同时涵盖4针OLED屏的IIC协议通信、初始化显示流程。

目录

 

LCD1602

LCD1602引脚

LCD1602显示字符

写时序图和读时序图

 写时序图

检测忙标志是否忙

LCD1602初始化

在哪里显示,写入指令(地址)

显示什么字符(写入数据)

DTH11

 DTH11的通讯过程

获取空气中温湿度并发送到串口

DTH11获取空气中温湿度,数据在LCD1602显示 

4针OLED(IIC协议)

IIC协议

IIC特点

IIC构成 

IIC传输数据过程的三种信号

IIC发送一个字节数据

OLED屏

OLED写入指令或数据

OLED的寻址模式(在OLED上哪里显示字符)

OLED初始化

OLED显示一个点

OLED显示一个字母

OLED显示自己的名字

DTH11获取空气中温湿度,在OLED上显示

 

LCD1602

LCD1602引脚

b39838496aca45c2a145e2af960ccaac.png

  • 第 1 脚: VSS 为电源地
  • 第 2 脚: VDD 接 5V 正电源
  • 第 3 脚: VL 为液晶显示器对比度调整端,接正电源时对比度最弱,接地时对比度最高,对比度
  • 过高时会产生“鬼影”,使用时可以通过一个 10K 的电位器调整对比度。
  • 第 4 脚:RS 为寄存器选择,高电平时选择数据寄存器、低电平时选择指令寄存器。
  • 第 5 脚:R/W 为读写信号线,高电平时进行读操作,低电平时进行写操作。当 RS 和 R/W 共 同为低电平时可以写入指令或者显示地址,当 RS 为低电平 R/W 为高电平时可以读忙信号, 当 RS 为高电平 R/W 为低电平时可以写入数据。
  • 第 6 脚:E 端为使能端,当 E 端由高电平跳变成低电平时,液晶模块执行命令。
  • 第 7~14 脚:D0~D7 为 8 位双向数据线。
  • 第 15 脚:背光源正极。
  • 第 16 脚:背光源负极。

LCD1602显示字符

写时序图和读时序图

读时序图

a3860d5e6a8f44da8099038ed168cb9d.png

 写时序图

12a0f41817724c2792f2e0dc15d67400.png

时序图的参数 
54929672133f47a199e597108cd4b426.png

检测忙标志是否忙

  • 液晶显示模块是一个慢显示器件,所以在写入每条指令或数据之前一定要确认模块的忙标志为低电平,表示不忙,否则此指令失效。
  • 由于BF标志位为数据线的D7引脚,我们只关心D7引脚,而且BF标志位为高电平时,LCD1602表示忙,可以利用一个while(BF)循环卡住,然后不断读取数据线的D7引脚,等待LCD1602不忙时,硬件将BF标志位置0。
  • 读取LCD1602的数据,检测忙信号
    400b8cb07a8a4f98a6d5d294d9d67484.png

LCD1602初始化

8bit的LCD1602初始化

  1. 延时 15ms
  2. 写指令 38H(不检测忙信号)
  3. 延时 5ms
  4. 检测忙信号
  5. 写指令 38H:显示模式设置
  6. 写指令 08H:显示关闭
  7. 写指令 01H:显示清屏
  8. 写指令 06H:显示光标移动设置
  9. 写指令 0CH:显示开及光标设置

4bit的LCD1602初始化

  1. 延时 50ms
  2. 发送 0x03(4bit)(rs=0,rw=0)
  3. 延时 4.5ms
  4. 发送 0x03(4bit)(rs=0,rw=0)
  5. 延时 4.5ms
  6. 发送 0x03(4bit)(rs=0,rw=0)
  7. 延时 150μs
  8. 发送 0x02(4bit)(rs=0,rw=0)
  9. 写指令 28H(8bit)
  10. 写指令 0CH(8bit)
  11. 写指令 01H(8bit)
  12. 延时 2ms(8bit)
  13. 写指令 06H(8bit) 

在哪里显示,写入指令(地址)

LCD1602 内部显示地址

24fc406257264912b4c976f0bf343c62.png

由于写入指令或数据的时候,数据线D7恒定为高电平,即如果想要在0x40显示字符,则实际写入的地址为0x40 + 0x80。

写入指令(地址)

75ba303400d84dd59faf8712f31a8b7c.png

显示什么字符(写入数据)

LCD1602 模块字库表

75f28b3827054aea8993af3f85caddf0.png

由于字符A的ASCII码为65,即0100 0001,与LCD1602 模块字库表一致,因此在写入字符的时候,直接传入一个'A'即可。

写入数据(地址)

c1a9865e0b4340e7ae4c0415109aeeee.png

 LCD1602显示一个字符

#include "reg52.h"
#include <intrins.h>

#define dataBuf P0

sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^4;

void Delay5ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 9;
	j = 244;
	do
	{
		while (--j);
	} while (--i);
}

void Delay15ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 27;
	j = 226;
	do
	{
		while (--j);
	} while (--i);
}

//检测忙信号,读数据线的D7位
void checkBusySignal()
{
	char tmp = 0x80;
	dataBuf = 0x80;
	while(tmp & 0x80){  //当忙标志位(数据位D7)为高电平时,此时模块不能接收命令或者数据
		//根据读时序图,读LCD1602的数据
        RS = 0;
		RW = 1;
		EN = 0;
		_nop_();
		EN = 1;
		_nop_();
		_nop_();
		tmp = dataBuf;  //读八位数据线的数据,当不忙时,硬件会自动将该位置0,当检测到D7为低电平时将退出死循环
		EN = 0;
		_nop_();
	} 
}

//写指令
void writeCmd(char cmd)
{
	checkBusySignal();
	RS = 0;  //选择写指令寄存器
	RW = 0;
	EN = 0;
	_nop_();  //该函数为延时函数,延时一微秒
	dataBuf = cmd;  //将指令存入八位数据线中
	EN = 1;
	_nop_();
	_nop_();
	EN = 0;
	_nop_();
}

//写数据
void writeData(char myData)
{
	checkBusySignal();
	RS = 1;  //选择写数据寄存器
	RW = 0;
	EN = 0;
	_nop_();  //该函数为延时函数,延时一微秒
	dataBuf = myData;  //将数据存入八位数据线中
	EN = 1;
	_nop_();
	_nop_();
	EN = 0;
	_nop_();
}

//LCD1602初始化
void lcd1602Init()
{
	Delay15ms();  //延时 15ms
	writeCmd(0x38);  //写指令 38H(不检测忙信号) 
	Delay5ms();  //延时 5ms
	checkBusySignal();//以后每次写指令,读/写数据操作均需要检测忙信号
	writeCmd(0x38);  //写指令 38H:显示模式设置
	writeCmd(0x08);  //写指令 08H:显示关闭
	writeCmd(0x01);  //写指令 01H:显示清屏
	writeCmd(0x06);  //写指令 06H:显示光标移动设置
	writeCmd(0x0C);  //写指令 0CH:显示开及光标设置
}

void main()
{
	char displayAddress = 0x80 + 0x05;  
	char dsiplayData = 'J';  

	lcd1602Init();

	writeCmd(displayAddress);  //字符显示的地址
	writeData(dsiplayData);  //显示的字符
}

 LCD1602显示一行字符

#include "reg52.h"
#include <intrins.h>

#define dataBuf P0

sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^4;

void Delay5ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 9;
	j = 244;
	do
	{
		while (--j);
	} while (--i);
}

void Delay15ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 27;
	j = 226;
	do
	{
		while (--j);
	} while (--i);
}

//检测忙信号,读数据线的D7位
void checkBusySignal()
{
	char tmp = 0x80;
	dataBuf = 0x80;
	while(tmp & 0x80){  //当忙
STM32cubemx配置驱动DHT11模块 本篇文章将带大家学习使用DHT11湿温度模块,使用这个模块可以获取当前的温度和湿度数据。DHT11可以用于智能家居,蔬菜大棚等项目当中适用范围广。DHT11 是一种数字式温湿度传感器,由杜邦公司推出。它可以测量环境中的温度和湿度,并将测量结果以数字形式传输给微控制器或其他电子设备,适用于通过数字信号读取环境温湿度的应用场景。DHT11 传感器使用单总线数据交换协议,可以通过一个 I/O 引脚进行控制和数据传输。 阅读详情

相关推荐

基于51单片机的DTH11温湿度测量

这是一个基于STC90C52RC单片机的DTH11温湿度测量,可自行设置报警值,并将关键参数显示在LCD1602上的程序。

从半导体物理到实战:手把手教你测量MOS管体二极管的Vf和Trr

本文深入探讨了MOSFET体二极管的关键参数测量方法,重点讲解了正向压降(Vf)和反向恢复时间(Trr)的测量原理与实战步骤。通过搭建测试平台,使用万用表和示波器等工具,手把手指导如何量化这些影响开关损耗和效率的核心参数,并对比分析了硅基与碳化硅(SiC)器件在体二极管性能上的显著差异。

n7o8p的博客 345

使用AI编写测试用例——详细教程

现阶段AI还不能完全替代人工测试用例编写,但如果把AI当做一个提高效率的工具,它将会是一个很强大的辅助。

公众号|软件测试老吴 3万+

【低轨】Starlink星链-Ku波段OFDM信号同步机制与PNT应用潜力

本文深入解析了Starlink星链Ku波段OFDM信号的同步机制及其在定位、导航与授时(PNT)领域的应用潜力。研究发现,其信号帧内嵌的同步序列具有相位恒定特性,支持超长相干积分,可在极低信噪比下实现高精度伪距测量,为利用低轨卫星通信信号实现高精度PNT服务提供了新的技术路径。

weixin_29250403的博客 733

sylar学习系列一

开发环境 Centos7最小系统 gcc 9.1 cmake 环境可能存在的一些问题 可能存在的问题,linux最小系统没有一些常用命令,如果我们处于外网我们可以yum安装,但却遇到以下问题 can not find a valid baseurl for repo: base/7/x86-64 可修改网络配置 vi /etc/sysconfig/network-scripts/ifcfg-ens123 # 不是每个主机都是ens123 把ONBOOT=no,改为ONBOOT=yes 重启网络

wuguinianjing的博客 1020

AI快速变现之路,AI视频创作

初期投入$3000(工具订阅+样片制作),重点攻克跨境电商和本地生活服务市场。关注2025年AIGC视频工具的API降价趋势(如Pika企业版折扣可达50%),建议与云服务商签订长期合作协议。

qq_29896351的博客 1986

华为Ascend C算子开发能力认证(中级)(个人记录,有完整代码,已通过认证,仅供参考)

本文介绍了在昇腾NPU环境下实现Sigmoid算子的完整流程。首先需要在华为云ModelArts-Notebook或开发者套件上搭建环境,要求CANN版本为8.0.0.beta1。主要内容包括:1)实现SigmoidCustom算子的kernel侧核函数代码;2)完成host侧Tiling结构体实现;3)支持Float16数据类型。代码实现部分详细展示了核函数计算流程、数据拷贝和牛顿迭代优化方法。最后介绍了编译部署流程,包括环境配置、代码上传、编译执行等步骤,并提供了提交规范说明。该实现通过调用Ascend

qq_73044452的博客 1114

python基础知识补充

1. jupyter notebook 解释 2. ipython 魔法命令 3. latex in Markdown cell(在markdown里面编写数学公式) 4. jupyter notebook功能扩展:安装Nbextension 5. python解释与运行 6. 垃圾回收(计数机制与分代回收机制) 7. 名字空间与局部作用域L和全局作用域G 8. 小整数缓存区与字符驻留 9. Statement &Expression 10. 调试

weixin_43178406的博客 1万+

具身智能:原理、算法与系统 第1章 具身智能的本质与范畴

从学术定义的角度,具身智能可被界定为"与物理环境进行有目的的能量与信息交换"的智能形式。这一定义揭示了具身智能的两个本质维度:信息处理维度与能量交互维度。与传统人工智能系统仅在信息空间操作不同,具身智能系统必须同时处理信息流与能量流,在物理约束下实现感知、认知与行动的统一。具身智能体具有三个核心构成要素:物理身体(Physical Body)、感知系统(Sensory System)与效应器(Effectors)。这三者共同构成了感知与行动的基础,使智能体能够通过物理交互而非纯粹符号操作来理解世界。

VectorShift的博客 273

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 51单片机之串口通信、WiFi模块、蓝牙模块、4G模块
下一篇: 51单片机之智能小车(避障、跟随、循迹)
haozigegie
博客等级 码龄6年 5687粉丝 59原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址