STM32使用IIC总线通讯协议在OLED屏幕上显示字符串、汉字、单总线获取DHT11模块温湿度并通过IIC显示到屏幕(软件IIC)

参考:基于stm32软件IIC的oled显示温湿度
作者:ZPZ DayUp
发布时间: 2021-07-25 20:52:43
网址:https://blog.csdn.net/m0_56197680/article/details/119077076?spm=1001.2014.3001.5501

目录

  • 软件模拟IIC时序(起始、停止、应答、发送、清屏、显示)在oled上显示字符串和汉字
    • oled.c代码
    • oled.h文件
    • oledfont.h字库头文件
  • DHT11温湿度模块
    • 供电电压:3.3 - 5.5V直流电
    • 与MCU接线
    • 数据格式
    • 温湿度传感器时序
  • 软件IIC显示温湿度
    • DHT11.c
    • DHT11.h
    • main.c
    • 显示效果

软件模拟IIC时序(起始、停止、应答、发送、清屏、显示)在oled上显示字符串和汉字

oled.c代码

#include "stm32f10x.h"
#include "oled.h"
#include "oledfont.h"
#include "delay.h"		//系统定时器

static void OLED_GPIO_Init(void)		
{
	
	 GPIO_InitTypeDef   	oled_GPIOstruct;		//oled管脚初始化
	 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
     //PB0 ->SCL    PB1 ->SDA		
	 oled_GPIOstruct.GPIO_Mode = GPIO_Mode_Out_OD;	//开漏输出
	 oled_GPIOstruct.GPIO_Pin =GPIO_Pin_0 | GPIO_Pin_1 ; 
	 oled_GPIOstruct.GPIO_Speed = GPIO_Speed_50MHz;
	 GPIO_Init( GPIOB, &oled_GPIOstruct);

	 OLED_SCLK_Set();//头文件宏定义
	 OLED_SDIN_Set();// 时钟线和数据线全部拉高 处于空闲状态
}

//模拟IIC起始信号
static void OLED_IIC_Start(void)
{
	OLED_SCLK_Set();
	OLED_SDIN_Set();		
	delay_us(1);
	OLED_SDIN_ReSet();	//数据线拉低,产生下降沿
	delay_us(1);
	OLED_SCLK_ReSet();	//时钟线再数据线产生下降沿之后保持一段时间高电平后将其拉低,起始信号模拟完毕
	delay_us(1);
}

//IIC模拟停止信号
static void OLED_IIC_Stop(void)
{
	OLED_SDIN_ReSet();
	OLED_SCLK_ReSet();
	delay_us(1);
	OLED_SCLK_Set();	//时钟线先拉高
	delay_us(1);
	OLED_SDIN_Set();	//数据线拉高,产生上升沿,至此停止信号模拟完毕
	delay_us(1);
}

//IIC模拟读取从机应答信号
static unsigned char IIC_Wait_Ack(void)
{
	unsigned char Ask;
	OLED_SCLK_ReSet();		//时钟线拉低
	delay_us(1);
	OLED_SDIN_Set();		//数据线拉高
	delay_us(1);
	OLED_SCLK_Set();		//时钟线拉高
    //应答信号,在时钟线拉高之前产生下降沿并在时钟线拉高的情况下确保为低电平
	if(OLED_READ_SDIN())	//读取数据线电平为高电平
	{
		ack = IIC_NO_Ask;	//对应的应答信号为非应答信号
	}
	else
	{
		ack = IIC_Ask;		//对应信号为应答信号
	}
	
	OLED_SCLK_ReSet();		//接收完应答信号,时钟线拉低
	delay_us(1);
	return ack;
}

//IIC写入(发送)一个字节byte  时钟高电平期间数据要保持稳定,时钟低电平时数据才可以改变
static void Write_IIC_Byte(unsigned char IIC_Byte)
{
	unsigned char i=0;
	for(i=0;i<8;i++)	         //从高到低循环写入
	{
		OLED_SCLK_ReSet();		//拉低时钟线,让后面数据可以改变
		delay_us(1);
		
		if(IIC_Byte & 0x80)		//1000 0000 读取第八位
			OLED_SDIN_Set();	//最高位为1,把数据线置高(通过高低电平传输数据)
		else
			OLED_SDIN_ReSet(); 	//最高位为0,把数据线拉低
		IIC_Byte <<= 1; 		//数据左移1位,次高位变成最高位
		
		delay_us(1);
		OLED_SCLK_Set();		//传输并确认一位数据之后,时钟线拉高,读取这一位数据
		delay_us(1);
	}
	
	    OLED_SCLK_ReSet();		//8位数据全部读完之后时钟线拉低		
		delay_us(1);
		while(IIC_Wait_Ack());	//等待应答信号通过!	
}

//IIC写命令
static void Write_IIC_Command(unsigned char IIC_Command)
{
	OLED_IIC_Start();			//起始信号		
	Write_IIC_Byte(0x78);		//第一次发送从机地址
	Write_IIC_Byte(0x00);		//第二次发送命令地址(写入命令)
	Write_IIC_Byte(IIC_Command);//写入指定命令	
	OLED_IIC_Stop();			//发送停止信号
}

//IIC写数据
static void Write_IIC_Data(unsigned char IIC_Data)
{
	OLED_IIC_Start();				
	Write_IIC_Byte(0x78);		//第一次发送从机地址
	Write_IIC_Byte(0x40);		//第二次发送数据地址(写入数据)
	Write_IIC_Byte(IIC_Data);	//写入指定数据	
	OLED_IIC_Stop();			//发送停止信号
}


//对OLED写入一个字节
void OLED_Write_Byte(unsigned char dat,unsigned char cmd)
{
	if(cmd)
	{
		Write_IIC_Data(dat);	//写数据
	}
	else
	{
		Write_IIC_Command(dat);//写命令
	}
}

//设置OLED起点坐标
void SetPoint(unsigned char x,unsigned char y)
{	//x:列 
    //y:页 起始页的地址0-7
	OLED_Write_Byte(0xb0+y,OLED_CMD);		//写入页地址    写命令 起始页地址0xb0
	OLED_Write_Byte((x&0xf0>>4),OLED_CMD);	//列地址高四位  写命令
	OLED_Write_Byte(x&0x0f|0x10,OLED_CMD);	//列地址低四位  写命令
}
//开启屏幕显示
void OLED_Display()
{
	OLED_Write_Byte(0x8D,OLED_CMD);	//设置电荷泵
	OLED_Write_Byte(0xAF,OLED_CMD);//开启屏幕
	OLED_Write_Byte(0x14,OLED_CMD);//开启电荷泵
}
//关闭屏幕显示
void OLED_OFF()
{
	OLED_Write_Byte(0x8D,OLED_CMD);	//设置电荷泵
	OLED_Write_Byte(0xAE,OLED_CMD);//关闭屏幕
	OLED_Write_Byte(0x10,OLED_CMD);//关闭电荷泵
}
//清屏操作
void OLED_Clear()
{
	unsigned char i,n;
	for(i=0;i<8;i++)//总共有8页
	{
		OLED_Write_Byte(0xb0+i,OLED_CMD);//从0-7页依次写入	
		OLED_Write_Byte(0x00,OLED_CMD);	//列低地址	
		OLED_Write_Byte(0x10,OLED_CMD);	//列高地址		
		for(n=0;n<128;n++)
		{
			OLED_Write_Byte(0,OLED_Data);//往128*64个像素点全部写入数据0	
		}
	}
}
//OLED显示字符函数
void OLED_Showcharactor(unsigned char x,unsigned char y,unsigned char chr)
{	//传入参数 x,y分别表示列和页  chr表示字符
	unsigned char c=0,i=0;
	c = chr - ' ';		//这里结合字库以及ASCII码可以知道 获取字符的偏移量	 
	if(x>MAX_Column)	//列超过最大长度 就从下两页的第0列开始
	{
		x = 0;		
		y = y+2;	  	//字符8x16大小,故页要一次性加两行
	}
	if(SIZE == 16)		//字符大小如果为16 = 8*16
	{
		OLED_SetPoint(x,y);	//设置OLED起点坐标	
		for(i=0;i<8;i++)
			OLED_Write_Byte(F8X16[c*16+i],OLED_Data);//找出字符C的数组位数,先把第一列画完
		OLED_SetPoint(x,y+1);
		for(i=8;i<16;i++)//先显示8个 再显示8个
			OLED_Write_Byte(F8X16[c*16+i],OLED_Data);
	}
	else				// 6*8
	{
		OLED_SetPoint(x,y);
		for(i=0;i<6;i++)
			OLED_Write_Byte(F6x8[c][i],OLED_Data);
	}
}
	
//OLED显示字符串函数
void OLED_ShowString(unsigned char x,unsigned char y,unsigned char *chr)
{
	unsigned char j = 0;
	while(chr[j]!='\0')	//循环遍历字符串 判断是不是最后一个字符
	{
		OLED_Showcharactor(x,y,chr[j]);	//调用之前的显示单个字符函数	
		x+=8;//列数+8  一个字符占8			
		if(x>=128)
		{
			x=0;//第一列显示
			y+=2;//换页
		}
		j++;
	}
}
//计算m^n次方函数
unsigned int OLED_Pow(unsigned char m,unsigned char n)
{
	unsigned int result = 1;
	while(n--)
		result*=m;
	return result;
}
//OLED显示数字函数
void OLED_ShowNum(unsigned char x,unsigned char y,unsigned int num,unsigned char len,unsigned char size)
{//传入参数 x,y 列和页;  num 对应数字;  len 数字长数;  size 字体大小
	unsigned char t,temp;
	unsigned char enshow = 0;	//是否为第一个数是否为0的标志
	for(t=0;t<len;t++)
	{
		temp = (num/OLED_Pow(10,len-t-1))%10;	//从高到低依次获取数据
		if(enshow==0&&t<(len-1))//enshow:是否为第一个数;t<(len-1):判断是否为最后一个数
		{	
			if(temp==0)	//该数为0	
			{	
				OLED_Showcharactor(x+(size/2)*t,y,' ');//显示 0 ;x+(size2/2)*t根据字体大小偏移的列数(8)
				continue;	//跳出当前循环,避免重复显示
			}
			else 
				enshow=1;
		}
		OLED_Showcharactor(x+(size/2)*t,y,temp+'0');//显示一个位;x+(size2/2)*t根据字体大小偏移的列数(8)
	}
}

//OLED显示汉字函数
void OLED_ShowChinese(unsigned char x,unsigned char y,unsigned char no)
{//参数 x,y 列和页;	no 代表某个汉字在汉字库中位于第几个
	unsigned char t,adder=0;
	OLED_SetPoint(x,y);
	for(t=0;t<16;t++)
	{					   //一个字是两个16位
		OLED_Write_Byte(Hzk[2*no][t],OLED_Data);//画 no在数组位置的第一页16列的点
		adder+=1;//数组地址+1	
	}
	OLED_SetPoint(x,y+1);//画第二页,下半部分
		for(t=0;t<16;t++)
	{                      //一个字是两个16位
		OLED_Write_Byte(Hzk[2*no+1][t],OLED_Data);//画 no在数组位置的第二页16列的点
		adder+=1;		
	}
}

void OLED_Init(void)///复制过来直接用
{
	OLED_GPIO_Init();	
 
	delay_ms(200);	

	OLED_Write_Byte(0xAE,OLED_CMD);	

	OLED_Write_Byte(0x00,OLED_CMD);	
	OLED_Write_Byte(0x10,OLED_CMD);	
	OLED_Write_Byte(0x40,OLED_CMD);	
	OLED_Write_Byte(0xB0,OLED_CMD);	

	OLED_Write_Byte(0x81,OLED_CMD); 	
	OLED_Write_Byte(0xFF,OLED_CMD);

	OLED_Write_Byte(0xA1,OLED_CMD);	
	OLED_Write_Byte(0xA6,OLED_CMD);	

	OLED_Write_Byte(0xA8,OLED_CMD);	
	OLED_Write_Byte(0x3F,OLED_CMD);	
	
	OLED_Write_Byte(0xC8,OLED_CMD);	

	OLED_Write_Byte(0xD3,OLED_CMD);	
	OLED_Write_Byte(0x00,OLED_CMD);	
	
	OLED_Write_Byte(0xD5,OLED_CMD);	
	OLED_Write_Byte(0x80,OLED_CMD);	
	
	OLED_Write_Byte(0xD9,OLED_CMD);	
	OLED_Write_Byte(0xF1,OLED_CMD);	
	
	OLED_Write_Byte(0xDA,OLED_CMD);	
	OLED_Write_Byte(0x12,OLED_CMD);	
	
	OLED_Write_Byte(0xDB,OLED_CMD);	
	OLED_Write_Byte(0x40,OLED_CMD);
	
	OLED_Write_Byte(0x8D,OLED_CMD);
	OLED_Write_Byte(0x14,OLED_CMD);
	
	OLED_Write_Byte(0xAF,OLED_CMD);
	OLED_Clear();       
	OLED_SetPoint(0,0); 	 
}

oled.h文件

#ifndef _OLED_H_
#define _OLED_H_
#include "stm32f10x.h"//防止头文件重复编译 包含


#define OLED_SCLK_Set()      GPIO_SetBits(GPIOB,GPIO_Pin_0)		
#define OLED_SCLK_ReSet()  	 GPIO_ResetBits(GPIOB, GPIO_Pin_0) 
#define OLED_SDIN_Set()		 GPIO_SetBits(GPIOB,GPIO_Pin_1)	 
#define OLED_SDIN_ReSet()    GPIO_ResetBits(GPIOB,GPIO_Pin_1)	 
#define OLED_READ_SDIN()     GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)


#define IIC_Ask     0		
#define IIC_NO_Ask  1  

#define OLED_CMD	0		
#define OLED_Data   1		
#define MAX_Column  128	
#define SIZE		16		

static void OLED_GPIO_Init(void);		
static void OLED_IIC_Start(void);
static void OLED_IIC_Stop(void);
static unsigned char IIC_Wait_Ack(void);
static void Write_IIC_Byte(unsigned char IIC_Byte);
static void Write_IIC_Command(unsigned char IIC_Command);
static void Write_IIC_Data(unsigned char IIC_Data);
void OLED_Write_Byte(unsigned char dat,unsigned char cmd);
void OLED_SetPoint(unsigned char x,unsigned char y);
void OLED_Display(void);			
void OLED_OFF(void)	;					
void OLED_Clear(void);
void OLED_Fill(void);
void OLED_Showcharactor(unsigned char x,unsigned char y,unsigned char chr);
void OLED_Init(void);
void OLED_ShowString(unsigned char x,unsigned char y,unsigned char *chr);
unsigned int OLED_Pow(unsigned char m,unsigned char n);
void OLED_ShowNum(unsigned char x,unsigned char y,unsigned int num,unsigned char len,unsigned char size);
void OLED_ShowChinese(unsigned char x,unsigned char y,unsigned char no);
#endif

oledfont.h字库头文件

#ifndef __OLEDFONT_H
#define __OLEDFONT_H 	   
//常用ASCII表
//偏移量32
//ASCII字符集
//偏移量32
//大小:12*6
/************************************6*8的点阵************************************/
const unsigned char  F6x8[][6] =		
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp
0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// !
0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// "
0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14,// #
0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12,// $
0x00, 0x62, 0x64, 0x08, 0x13, 0x23,// %
0x00, 0x36, 0x49, 0x55, 0x22, 0x50,// &
0x00, 0x00, 0x05, 0x03, 0x00, 0x00,// '
0x00, 0x00, 0x1c, 0x22, 0x41, 0x00,// (
0x00, 0x00, 0x41, 0x22, 0x1c, 0x00,// )
0x00, 0x14, 0x08, 0x3E, 0x08, 0x14,// *
0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,// +
0x00, 0x00, 0x00, 0xA0, 0x60, 0x00,// ,
0x00, 0x08, 0x08, 0x08, 0x08, 0x08,// -
0x00, 0x00, 0x60, 0x60, 0x00, 0x00,// .
0x00, 0x20, 0x10, 0x08, 0x04, 0x02,// /
0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
0x00, 0x00, 0x42, 0x7F, 0x40, 0x00,// 1
0x00, 0x42, 0x61, 0x51, 0x49, 0x46,// 2
0x00, 0x21, 0x41, 0x45, 0x4B, 0x31,// 3
0x00, 0x18, 0x14, 0x12, 0x7F, 0x10,// 4
0x00, 0x27, 0x45, 0x45, 0x45, 0x39,// 5
0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
0x00, 0x01, 0x71, 0x09, 0x05, 0x03,// 7
0x00, 0x36, 0x49, 0x49, 0x49, 0x36,// 8
0x00, 0x06, 0x49, 0x49, 0x29, 0x1E,// 9
0x00, 0x00, 0x36, 0x36, 0x00, 0x00,// :
0x00, 0x00, 0x56, 0x36, 0x00, 0x00,// ;
0x00, 0x08, 0x14, 0x22, 0x41, 0x00,// <
0x00, 0x14, 0x14, 0x14, 0x14, 0x14,// =
0x00, 0x00, 0x41, 0x22, 0x14, 0x08,// >
0x00, 0x02, 0x01, 0x51, 0x09, 0x06,// ?
0x00, 0x32, 0x49, 0x59, 0x51, 0x3E,// @
0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,// A
0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,// B
0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,// C
0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C,// D
0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,// E
0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,// F
0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,// G
0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H
0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,// I
0x00, 0x20, 0x40, 0x41, 0x3F, 0x01,// J
0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,// K
0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,// L
0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F,// M
0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F,// N
0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,// O
0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,// P
0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
0x00, 0x7F, 0x09, 0x19, 0x29, 0x46,// R
0x00, 0x46, 0x49, 0x49, 0x49, 0x31,// S
0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,// T
0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,// U
0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,// V
0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F,// W
0x00, 0x63, 0x14, 0x08, 0x14, 0x63,// X
0x00, 0x07, 0x08, 0x70, 0x08, 0x07,// Y
0x00, 0x61, 0x51, 0x49, 0x45, 0x43,// Z
0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,// [
0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55,// 55
0x00, 0x00, 0x41, 0x41, 0x7F, 0x00,// ]
0x00, 0x04, 0x02, 0x01, 0x02, 0x04,// ^
0x00, 0x40, 0x40, 0x40, 0x40, 0x40,// _
0x00, 0x00, 0x01, 0x02, 0x04, 0x00,// '
0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a
0x00, 0x7F, 0x48, 0x44, 0x44, 0x38,// b
0x00, 0x38, 0x44, 0x44, 0x44, 0x20,// c
0x00, 0x38, 0x44, 0x44, 0x48, 0x7F,// d
0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e
0x00, 0x08, 0x7E, 0x09, 0x01, 0x02,// f
0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g
0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h
0x00, 0x00, 0x44, 0x7D, 0x40, 0x00,// i
0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,// j
0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,// k
0x00, 0x00, 0x41, 0x7F, 0x40, 0x00,// l
0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m
0x00, 0x7C, 0x08, 0x04, 0x04, 0x78,// n
0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o
0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p
0x00, 0x18, 0x24, 0x24, 0x18, 0xFC,// q
0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r
0x00, 0x48, 0x54, 0x54, 0x54, 0x20,// s
0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t
0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C,// u
0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,// v
0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C,// w
0x00, 0x44, 0x28, 0x10, 0x28, 0x44,// x
0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y
0x00, 0x44, 0x64, 0x54, 0x4C, 0x44,// z
0x14, 0x14, 0x14, 0x14, 0x14, 0x14,// horiz lines
};
/****************************************8*16的点阵************************************/
const unsigned char  F8X16[]=	  
{
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
  0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
  0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
  0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
  0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
  0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
  0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
  0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
  0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
  0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
  0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
  0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
  0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
  0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
  0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
  0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
  0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
  0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
  0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
  0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
  0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
  0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
  0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
  0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
  0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
  0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
  0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
  0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
  0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
  0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
  0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
  0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
  0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
  0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
  0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
  0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
  0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
  0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
  0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
  0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
  0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
  0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
  0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
  0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
  0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
  0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
  0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
  0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
  0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
  0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
  0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
  0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
  0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
  0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
  0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
  0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
  0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
  0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
  0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
  0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
  0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
  0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
  0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
  0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
  0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
  0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
  0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
  0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
  0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
  0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
  0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
  0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
  0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
  0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
  0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
  0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
  0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
  0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
  0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
  0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
  0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
  0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
  0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
  0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
  0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
  0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
  0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
  0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};
const unsigned char  Hzk[][32]={

{0x10,0x10,0x10,0xFF,0x10,0x90,0x08,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x08,0x00},
{0x04,0x44,0x82,0x7F,0x01,0x80,0x80,0x40,0x43,0x2C,0x10,0x28,0x46,0x81,0x80,0x00},/*"技",0*/

{0x40,0x44,0x54,0x65,0xC6,0x64,0x54,0x44,0x00,0xFC,0x44,0x44,0xC4,0x42,0x40,0x00},
{0x20,0x12,0x4A,0x82,0x7F,0x02,0x0A,0x92,0x60,0x1F,0x00,0x00,0xFF,0x00,0x00,0x00},/*"新",1*/

{0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x88,0xF8,0x00,0x00,0x00},
{0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,0x00},/*"电",2*/

{0x80,0x82,0x82,0x82,0x82,0x82,0x82,0xE2,0xA2,0x92,0x8A,0x86,0x82,0x80,0x80,0x00},
{0x00,0x00,0x00,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"子",3*/

{0x24,0x24,0xA4,0xFE,0xA3,0x22,0x00,0x22,0xCC,0x00,0x00,0xFF,0x00,0x00,0x00,0x00},
{0x08,0x06,0x01,0xFF,0x00,0x01,0x04,0x04,0x04,0x04,0x04,0xFF,0x02,0x02,0x02,0x00},/*"科",4*/

{0x10,0x10,0x10,0xFF,0x10,0x90,0x08,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x08,0x00},
{0x04,0x44,0x82,0x7F,0x01,0x80,0x80,0x40,0x43,0x2C,0x10,0x28,0x46,0x81,0x80,0x00},/*"技",5*/


{0x00,0x00,0x00,0x10,0x20,0x60,0x00,0xFC,0x04,0x40,0xA0,0x98,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x01,0x41,0x49,0x49,0x29,0x25,0x25,0x24,0x3F,0x01,0x00,0x00,0x00},/*"当",6*/

{0x00,0x20,0x20,0xA0,0xA0,0xA4,0xA8,0x20,0x20,0x98,0x16,0xF0,0x10,0x10,0x10,0x00},
{0x00,0x00,0x00,0x1F,0x0A,0x2A,0x7F,0x00,0x00,0x0F,0x20,0x6F,0x30,0x00,0x00,0x00},/*"前",7*/


{0x00,0x00,0x40,0x08,0x18,0x00,0x08,0xF8,0xA8,0x68,0x68,0x74,0x18,0x00,0x00,0x00},
{0x00,0x00,0x30,0x0E,0x22,0x20,0x3E,0x22,0x1E,0x12,0x2E,0x22,0x1F,0x12,0x00,0x00},/*"温",8*/

{0x00,0x00,0x00,0x00,0xF0,0x90,0xB0,0xF0,0x56,0xD8,0x28,0x48,0x00,0x00,0x00,0x00},
{0x00,0x20,0x10,0x0C,0x43,0x40,0x40,0x2D,0x13,0x1E,0x22,0x60,0x40,0x40,0x40,0x00},/*"度",9*/


{0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x31,0x31,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*":",10*/

{0x00,0x0C,0x12,0x0C,0x00,0xC0,0x70,0x10,0x08,0x08,0x08,0x08,0x10,0x30,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x0F,0x18,0x30,0x20,0x20,0x20,0x20,0x30,0x1C,0x00,0x00},/*"℃",11*/

{0x00,0x00,0x40,0x08,0x08,0x08,0xF8,0xA8,0xA8,0x58,0x48,0x7C,0x08,0x00,0x00,0x00},
{0x00,0x00,0x38,0x06,0x20,0x20,0x2C,0x3D,0x23,0x21,0x3F,0x24,0x23,0x20,0x20,0x00},/*"湿",12*/

{0x00,0xF0,0xF8,0x08,0x88,0xF8,0xF0,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x00},
{0x00,0x00,0x00,0x21,0x18,0x0C,0x02,0x01,0x00,0x1E,0x3F,0x21,0x21,0x1E,0x1E,0x00},/*"%",13*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",14*/


{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x10,0x30,0x30,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",15*/


};

#endif

DHT11温湿度模块

STM32使用IIC总线通讯协议在OLED屏幕上显示字符串、汉字、单总线获取DHT11模块温湿度并通过IIC显示到屏幕(软件IIC)_第1张图片

供电电压:3.3 - 5.5V直流电

输出为单总线数字信号
温度测量范围0-50度(精度正负2度,分辨率1度)
湿度测量范围为20-90%RH(精度为正负5%,分辨率1%)

与MCU接线

STM32使用IIC总线通讯协议在OLED屏幕上显示字符串、汉字、单总线获取DHT11模块温湿度并通过IIC显示到屏幕(软件IIC)_第2张图片

数据格式

采用单总线双向串行通信协议,每次采集都要由单片机发起开始信号,然后DHT11会向单片机发送响应并开始传输40位数据帧,高位在前。 数据格式40帧(40bit)

  • 湿度整数部分+湿度小数部分 8bit + 8bit
  • 温度整数部分+温度小数部分 8bit + 8bit
  • 校验位部分 8bit

数据校验
湿度和温度的整数小数部分全部加起来判断是否与校验位部分相等,若相等,则取出数据;否之,数据重新获取湿度小数部分和温度小数部分单片机都默认为0,即校验位只会判断两者的整数部分和

例如
湿度 0101 0011 + 0000 0000
温度 0011 0010 + 0000 0000
校验位 1000 0101

此时湿度和温度的整数部分之和恰好等于校验位的,所以这组数据符合要求。

温湿度传感器时序


STM32使用IIC总线通讯协议在OLED屏幕上显示字符串、汉字、单总线获取DHT11模块温湿度并通过IIC显示到屏幕(软件IIC)_第3张图片

主机:单片机
从机:传感器

  • 总线空闲状态高电平

  • 主机将总线拉低,要求大于18ms,等待DHT11响应 (主机发送起始信号)

  • 从机(DHT11模块)接收主机起始信号后,等待主机起始信号结束

  • DHT11发送80us的低电平响应信号

  • 主机发送起始信号结束后,延时20-40us后读取DHT11的响应信号

  • 主机发送完开始信号,可以切换到输入模式或者接上拉电阻置高电平

软件IIC显示温湿度

DHT11.c

#include "stm32f10x.h"
#include "DHT11.h"
#include "delay.h"
#include "stdio.h"

uint16_t Rxbuff[5];

/*单总线,双向串行输出协议,故配置对应端口输入输出*/
void DHT11_GPIO_Init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
	//PB11 ->DHT11模块数据输出口	
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,  ENABLE);

	GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed =  GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    GPIO_SetBits(GPIOB,  GPIO_Pin_11);

}

void DHT11_GPIO_Init1(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
	//PB11->DHT11模块数据输入口
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,  ENABLE);

	GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_IPD;//下拉输入
	GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed =  GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    GPIO_SetBits(GPIOB,  GPIO_Pin_11);

}
//温湿度模块响应信号
static uint8_t DHT11_Back()
{
   uint8_t i =200;
	
	 while(read_data && i--);   //等待低电平的到来
	 i= 200;					//低电平延迟一会(80us)
	 while(!read_data && i--);  //等待高电平的到来
   return 0;
}

//起始信号
void DHT11_Start(void)
{
     data0;//置低
	 delay_ms(20);//主机拉低总线超过18ms	
     data1;//置高
	 delay_us(10);
	 
     DHT11_GPIO_Init1();//此时单片机由输出转成输入模式,等待模块相应
     while(DHT11_Back());
}

//DHT11模块读取数据函数
void DHT11_ReceptionBuff()
{
     uint8_t y=1;
	 uint16_t i;
	 uint8_t x;
 
   for(x=0;x<5;x++)//模块一共发送5个字节
	{
	    i=0;
		for(y=1;y<9;y++)//每个字节8位
		{
		   while(read_data)//接收低电平
			 {
			    __nop();//空操作			 
			 }
		   delay_us(40);
		   while(!read_data)//接收高电平
			 {
			    __nop();			 
			 }
		   i= i<<1;
		   delay_us(30);//看上面的时序,高电平持续26-28us内代表0  超过则为1,所以这里选择30us分界线
           
           
           if(read_data)//判断是否为1
			 {
			    i |=1;			 
			 }
		   while(read_data);//等待低电平 进行下一次循环			
		}	
	   Rxbuff[x] =i;//检验在后面main函数进行的
	}
}

void DHT11_UpdateData()
{
 	DHT11_GPIO_Init();
	DHT11_Start();
	DHT11_ReceptionBuff();
}

DHT11.h

#ifndef _DHT11_H_
#define _DHT11_H_
#include "stm32f10x.h"

#define data1 GPIO_SetBits(GPIOB,  GPIO_Pin_11)			
#define data0 GPIO_ResetBits(GPIOB,  GPIO_Pin_11)		
#define read_data GPIO_ReadInputDataBit(GPIOB,  GPIO_Pin_11) 

void DHT11_GPIO_Init_IN(void);		
void DHT11_GPIO_Init_OUT(void);		
void DHT11_Start(void);
void DHT11_ReceptionBuff(void);
void DHT11_UpdataData(void);
extern uint16_t Rxbuff[5];
#endif

main.c

#include "stm32f10x.h"
#include "main.h"
#include "oled.h"
#include "delay.h"
#include "sys.h"
#include "DHT11.h" 
int  main()
{
 	initSysTick();
	
	delay_ms(1000);
	OLED_Init();//oled初始化
	OLED_Clear();//oled清屏	
	delay_ms(1000);
	//让OLED显示下列一些字  在字库头文件里(字模软件生成)
	OLED_ShowChinese(0,2,6);		//当   都显示在第二页	
	OLED_ShowChinese(16,2,7);		//前
	OLED_ShowChinese(32,2,8);		//温
	OLED_ShowChinese(48,2,9);		//度
	OLED_ShowChinese(66,2,10);		//:
	OLED_ShowChinese(90,2,15);		//.
	OLED_ShowChinese(112,2,11);		//℃
	//这种都来自于字库文件 oledfont.h
	OLED_ShowChinese(0,5,6);		//当
	OLED_ShowChinese(16,5,7);		//前
	OLED_ShowChinese(32,5,12);		//湿
	OLED_ShowChinese(48,5,9);		//度
	OLED_ShowChinese(66,5,10);		//:
	OLED_ShowChinese(90,5,15);		//.
	OLED_ShowChinese(112,5,13);		//%
	while(1)
	{
		uint16_t i;
		uint8_t  Tempture ;
		uint8_t  Wet ;
		uint8_t  xiaoshu ;		
		DHT11_UpdataData();//DHT11模块更新数据
		
		i = Rxbuff[0] + Rxbuff[1] + Rxbuff[2] + Rxbuff[3];
			if(Rxbuff[4]==i)		//校验成功	
			{
				Tempture = Rxbuff[2];		//温度整数
				Wet = Rxbuff[0];			//湿度整数
				xiaoshu = Rxbuff[3];		//温度小数部分(教程湿度小数部分没处理)
				
				OLED_ShowNum(74,2,Tempture/10,3,3);
				OLED_ShowNum(82,2,Tempture%10,3,3);
			    OLED_ShowNum(98,2,xiaoshu,3,3);
				OLED_ShowNum(88,5,Wet/10,3,3);
				OLED_ShowNum(98,5,xiaoshu%10,3,3);
			}
		delay_ms(2000);
	}

显示效果

STM32使用IIC总线通讯协议在OLED屏幕上显示字符串、汉字、单总线获取DHT11模块温湿度并通过IIC显示到屏幕(软件IIC)_第4张图片

你可能感兴趣的:(STM32,stm32,单片机,arm)