Bootstrap

7.单总线通信

DHT11介绍分析

HT11数字温湿度传感器

数据格式(在HDT11没有小数):

    8bit湿度整数数据 + 8bit湿度小数数据 + 8bit温度整数数据 + 8bit温度小数数据 + 8bit校验和

校验方式将温湿度4个字节数据加起来 = 校验和,传送完成后重新加起来与校验和数据比较是否相同

开始信号时序

线空闲状态为高电平,单片机把总线拉低必须大于18毫秒,保证DHT11能检测到起始信号,单片机发送开始信号结束后,延时等待20-40us,DHT11接收到单片机开始信号后,等待单片机开始信号结束,然后发送80us低电平响应信号

开始信号时序

HT11发送响应信号后,再把总线拉高80us,准备发送数据

MCU接收数据时序

每一bit数据都以50us低电平时隙开始,高电平长短决定数据位是 0 还是 1

数字0信号时序

数字0信号时序图

数字1信号时序

数字1信号时序图

代码编写

利用上一个IIC协议和OLED模块,使用单总线通信和温湿度模块,在OLED屏上显示温湿度

dht11.c

#include "dht11.h"
#include "delay.h"
char DHT11_Start(void)
{
	uchar delay_count = 0;	//用于计数
	DHT11_IO = 1;	//一开始单片机时序高电平
	Delay30us();
	DHT11_IO = 0;	//拉低电平至少18us
	Delay25ms();
	DHT11_IO = 1;	//再拉高20-40us
	Delay30us();
	//当DHT11响应信号开始时为低电平(80us),等到结束时高电平,取反结束或大于80us时结束
	while(!DHT11_IO && delay_count < 80)
	{
		delay_count++;
	}
	if(delay_count >= 80)	//大于80us结束
		return -1;
	delay_count = 0;	//重置计数值
	//响应信号结束拉高80us后拉低,代表数据开始传输,等拉低电平时或大于80us结束
	while(DHT11_IO && delay_count < 80)
	{
		delay_count++;
	}
	if(delay_count >= 80)	//大于80us结束
	{
		return -1;
	}
	return 0;
}

char DHT11_Write(int *humi,int *temp)
{
	uchar delay_count = 0,data_byte,tmp,x,y;
	uchar DHT11_Data[5];	//定义数组保存数据
	DHT11_Start();	//调用开始函数
	//发送40bit数据
	for(x = 0;x < 5;x++)
	{
		for(y = 0;y < 8;y++)
		{
			//数据发送前低电平,判断为高电平时则开始若有问题跳出(>50)
			while(!DHT11_IO && delay_count < 50)
				delay_count++;
			if(delay_count >= 50)	//大于80us结束
				return -1;
			delay_count = 0;
			tmp = 0;
			Delay30us();
			if(DHT11_IO == 1)	//若条件不成立发送数据0
			{
				tmp = 1;	//发送数据1
				while(DHT11_IO && delay_count < 50)
					delay_count++;
				if(delay_count >= 50)	//大于80us结束
					return -1;
			}
			delay_count = 0;
			data_byte <<= 1;	//左移一位
			data_byte |= tmp;	//将每一位数据传给data_byte
		}
		DHT11_Data[x]=data_byte;
	}
	//若前四个数值加起来不等于校验位的数,则全部清零
	if(DHT11_Data[0]+DHT11_Data[1]+DHT11_Data[2]+DHT11_Data[3] != DHT11_Data[4])
	{
		DHT11_Data[0]=0;
		DHT11_Data[1]=0;
		DHT11_Data[2]=0;
		DHT11_Data[3]=0;
	}
	//因为DHT11只有整数,所以是0,2
	*humi = (int)DHT11_Data[0];	//相同地址,值改变则另一个相同地址的变量也改变
	*temp = (int)DHT11_Data[2];
	return 0;
}

dat11.h

#ifndef __DHT11_H__
#define __DHT11_H__

#include "main.h"

sbit DHT11_IO = P1^2;

char DHT11_Start(void);
char DHT11_Write(int *humi,int *temp);

#endif

main.c

#include "iic.h"
#include "oled.h"
#include "bmp.h"
#include "main.h"
#include "dht11.h"
#include <stdio.h>
int H,T;
char DHT11_Buf[20];

void main()
{
	OLED_Init();	//初始化OLED
	OLED_Clear();
	OLED_ShowCHinese(0,0,0);	//星(x坐标,y坐标,字模第几个)
	OLED_ShowCHinese(18,0,1);	//仔
	OLED_ShowCHinese(36,0,2);	//爱
	OLED_ShowCHinese(54,0,3);	//分
	OLED_ShowCHinese(72,0,4);	//享
	OLED_ShowCHinese(90,0,5);	//抖
	OLED_ShowCHinese(108,0,6);	//音
	OLED_ShowCHinese(0,2,5);	//抖
	

	OLED_Clear();	
	OLED_ShowString(6,3,"0.96' OLED TEST",16);	//显示字符串(x,y,字符串,字号)
	OLED_Clear();
	OLED_ShowNum(103,6,224,3,16);	//显示数字(x,y,数字,数字个数,字号)
	OLED_Clear();
	OLED_DrawBMP(0,0,128,8,BMP1);	//(1,2:左上角坐标,3,4:右下角坐标,图片字模)
	OLED_Clear();
	while(1)
	{
		DHT11_Write(&H, &T);
		sprintf(DHT11_Buf,"H2:%d T2:%d",H,T);	//将形式字符赋值给DHT11_Buf
		OLED_ShowString(0,4,DHT11_Buf,16);	//发送字符串
		Delay1000ms();
	}
}

main.h

#ifndef __MAIN_H__
#define __MAIN_H__

#include "reg52.h"
typedef unsigned char uchar;

#endif

iic.c

#include "iic.h"
#include "delay.h"
uchar i = 0;
//开始标志时序
void IIC_start()
{
	//一开始SCL和SDA都为高电平
	IIC_SCL = 1;
	IIC_SDA = 1;
	Delay5us();	//10us一个周期
	IIC_SDA = 0;	//大概一半周期SDA变低电平
	Delay5us();	//再过半个周期
	IIC_SCL = 0;	//SCL变低电平
}
//结束标志时序
void IIC_stop()
{
	//一开始SCL、SDA都为低电平
	IIC_SCL = 0;
	IIC_SDA = 0;
	Delay5us();
	IIC_SCL = 1;	//过半个周期SCL变高电平
	Delay5us();
	IIC_SDA = 1;	//过半个周期SDA变高电平
}
//写数据
void IIC_write(uchar a1)
{
	for(i = 0;i < 8;i++)
	{
		a1 <<= 1;
		IIC_SDA = CY;	//进位标志位,左移溢出的保存在CY里
		IIC_SCL = 0;
		Delay5us();
		IIC_SCL = 1;
		Delay5us();
		IIC_SCL = 0;
		
	}
	//应答
	IIC_SDA = 0;
	IIC_SCL = 0;
	Delay5us();
	IIC_SCL = 1;
	Delay5us();
	IIC_SCL = 0;
}

iic.h

#ifndef __IIC_H__
#define __IIC_H__

#include "main.h"

void IIC_start();
void IIC_stop();
void IIC_write(uchar a1);
//定义时间线和数据线
sbit IIC_SCL = P1^5;
sbit IIC_SDA = P1^4;

#endif

oled.c

#include "oled.h"
#include "oledfont.h"
#include "iic.h"
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
   IIC_start();
   IIC_write(0x78);            //Slave address,SA0=0	
   IIC_write(0x00);			//write command
   IIC_write(IIC_Command); 
   IIC_stop();
}
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
   IIC_start();
   IIC_write(0x78);			//D/C#=0; R/W#=0
   IIC_write(0x40);			//write data
   IIC_write(IIC_Data);
   IIC_stop();
}
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
	if(cmd)
			{

   Write_IIC_Data(dat);
   
		}
	else {
   Write_IIC_Command(dat);
		
	}


}

/********************************************
// fill_Picture
********************************************/
void fill_picture(unsigned char fill_Data)
{
	unsigned char m,n;
	for(m=0;m<8;m++)
	{
		OLED_WR_Byte(0xb0+m,0);		//page0-page1
		OLED_WR_Byte(0x00,0);		//low column start address
		OLED_WR_Byte(0x10,0);		//high column start address
		for(n=0;n<128;n++)
			{
				OLED_WR_Byte(fill_Data,1);
			}
	}
}


/***********************Delay****************************************/
void Delay_50ms(unsigned int Del_50ms)
{
	unsigned int m;
	for(;Del_50ms>0;Del_50ms--)
		for(m=6245;m>0;m--);
}

void Delay_1ms(unsigned int Del_1ms)
{
	unsigned char j;
	while(Del_1ms--)
	{	
		for(j=0;j<123;j++);
	}
}

//坐标设置

	void OLED_Set_Pos(unsigned char x, unsigned char y) 
{ 	OLED_WR_Byte(0xb0+y,OLED_CMD);
	OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
	OLED_WR_Byte((x&0x0f),OLED_CMD); 
}   	  
//开启OLED显示    
void OLED_Display_On(void)
{
	OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
	OLED_WR_Byte(0X14,OLED_CMD);  //DCDC ON
	OLED_WR_Byte(0XAF,OLED_CMD);  //DISPLAY ON
}
//关闭OLED显示     
void OLED_Display_Off(void)
{
	OLED_WR_Byte(0X8D,OLED_CMD);  //SET DCDC命令
	OLED_WR_Byte(0X10,OLED_CMD);  //DCDC OFF
	OLED_WR_Byte(0XAE,OLED_CMD);  //DISPLAY OFF
}		   			 
//清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!	  
void OLED_Clear(void)  
{  
	u8 i,n;		    
	for(i=0;i<8;i++)  
	{  
		OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址(0~7)
		OLED_WR_Byte (0x00,OLED_CMD);      //设置显示位置—列低地址
		OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址   
		for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA); 
	} //更新显示
}
void OLED_On(void)  
{  
	u8 i,n;		    
	for(i=0;i<8;i++)  
	{  
		OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址(0~7)
		OLED_WR_Byte (0x00,OLED_CMD);      //设置显示位置—列低地址
		OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址   
		for(n=0;n<128;n++)OLED_WR_Byte(1,OLED_DATA); 
	} //更新显示
}
//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//mode:0,反白显示;1,正常显示				 
//size:选择字体 16/12 
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size)
{      	
	unsigned char c=0,i=0;	
		c=chr-' ';//得到偏移后的值			
		if(x>Max_Column-1){x=0;y=y+2;}
		if(Char_Size ==16)
			{
			OLED_Set_Pos(x,y);	
			for(i=0;i<8;i++)
			OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
			OLED_Set_Pos(x,y+1);
			for(i=0;i<8;i++)
			OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
			}
			else {	
				OLED_Set_Pos(x,y);
				for(i=0;i<6;i++)
				OLED_WR_Byte(F6x8[c][i],OLED_DATA);
				
			}
}
//m^n函数
u32 oled_pow(u8 m,u8 n)
{
	u32 result=1;	 
	while(n--)result*=m;    
	return result;
}				  
//显示2个数字
//x,y :起点坐标	 
//len :数字的位数
//size:字体大小
//mode:模式	0,填充模式;1,叠加模式
//num:数值(0~4294967295);	 		  
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2)
{         	
	u8 t,temp;
	u8 enshow=0;						   
	for(t=0;t<len;t++)
	{
		temp=(num/oled_pow(10,len-t-1))%10;
		if(enshow==0&&t<(len-1))
		{
			if(temp==0)
			{
				OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
				continue;
			}else enshow=1; 
		 	 
		}
	 	OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2); 
	}
} 
//显示一个字符号串
void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size)
{
	unsigned char j=0;
	while (chr[j]!='\0')
	{		OLED_ShowChar(x,y,chr[j],Char_Size);
			x+=8;
		if(x>120){x=0;y+=2;}
			j++;
	}
}
//显示汉字
void OLED_ShowCHinese(u8 x,u8 y,u8 no)
{      			    
	u8 t,adder=0;
	OLED_Set_Pos(x,y);	
    for(t=0;t<16;t++)
		{
				OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
				adder+=1;
     }	
		OLED_Set_Pos(x,y+1);	
    for(t=0;t<16;t++)
			{	
				OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
				adder+=1;
      }					
}
/***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
{ 	
 unsigned int j=0;
 unsigned char x,y;
  
  if(y1%8==0) y=y1/8;      
  else y=y1/8+1;
	for(y=y0;y<y1;y++)
	{
		OLED_Set_Pos(x0,y);
    for(x=x0;x<x1;x++)
	    {      
	    	OLED_WR_Byte(BMP[j++],OLED_DATA);	    	
	    }
	}
} 

//初始化SSD1306					    
void OLED_Init(void)
{ 	
 
	OLED_WR_Byte(0xAE,OLED_CMD);//--display off
	OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
	OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
	OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  
	OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
	OLED_WR_Byte(0x81,OLED_CMD); // contract control
	OLED_WR_Byte(0xFF,OLED_CMD);//--128   
	OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap 
	OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
	OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
	OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty
	OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction
	OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset
	OLED_WR_Byte(0x00,OLED_CMD);//
	
	OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
	OLED_WR_Byte(0x80,OLED_CMD);//
	
	OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
	OLED_WR_Byte(0x05,OLED_CMD);//
	
	OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
	OLED_WR_Byte(0xF1,OLED_CMD);//
	
	OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
	OLED_WR_Byte(0x12,OLED_CMD);//
	
	OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
	OLED_WR_Byte(0x30,OLED_CMD);//
	
	OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
	OLED_WR_Byte(0x14,OLED_CMD);//
	
	OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
}  

oled.h

#ifndef __OLED_H__
#define __OLED_H__

#define  u8 unsigned char 
#define  u32 unsigned int 

#define OLED_CMD  0	//写命令
#define OLED_DATA 1	//写数据
#define OLED_MODE 0

//OLED模式设置
//0:4线串行模式
//1:并行8080模式

#define SIZE 16
#define XLevelL		0x02
#define XLevelH		0x10
#define Max_Column	128
#define Max_Row		64
#define	Brightness	0xFF 
#define X_WIDTH 	128
#define Y_WIDTH 	64	    						  
//-----------------OLED端口定义----------------  					   

void delay_ms(unsigned int ms);

//OLED控制用函数
void OLED_WR_Byte(unsigned dat,unsigned cmd);  
void OLED_Display_On(void);
void OLED_Display_Off(void);	   							   		    
void OLED_Init(void);
void OLED_Clear(void);
void OLED_DrawPoint(u8 x,u8 y,u8 t);
void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot);
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size);
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size);
void OLED_ShowString(u8 x,u8 y, u8 *p,u8 Char_Size);	 
void OLED_Set_Pos(unsigned char x, unsigned char y);
void OLED_ShowCHinese(u8 x,u8 y,u8 no);
void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[]);
void Delay_50ms(unsigned int Del_50ms);
void Delay_1ms(unsigned int Del_1ms);
void fill_picture(unsigned char fill_Data);
void Picture();

void Write_IIC_Command(unsigned char IIC_Command);
void Write_IIC_Data(unsigned char IIC_Data);

#endif

delay.c

#include "delay.h"
#include <intrins.h>
void Delay5us()		//@11.0592MHz
{
	
}

void Delay30us()		//@11.0592MHz
{
	unsigned char data i;

	i = 11;
	while (--i);
}

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

	i = 45;
	j = 208;
	do
	{
		while (--j);
	} while (--i);
}

void Delay1000ms()    //@11.0592MHz
{
  unsigned char data i, j, k;
  _nop_();
  i = 8;
  j = 1;
  k = 243;
  do
  {
     do
     {
       while (--k);
     } while (--j);
  } while (--i);
}

delay.h

#ifndef __DELAY_H__
#define __DELAY_H__

void Delay5us();
void Delay30us();
void Delay25ms();
void Delay1000ms();

#endif
;