基于STC15F104W下的温湿度显示(DHT11)(OLED)

设计名称:基于DHT11的温湿度计

显示:采用OLED12864显示温度和湿度
测量,采用DHT11模块
Hygrothermograph.c

#include
#include "intrins.h"
#include "OLED_Contrul_Hygrothermograph.c"//OLED控制函数

sbit Y_LED=P3^1;
sbit R_LED=P3^0;
sbit DHT=P3^5;//DHT11模块

unsigned int Temperature[3]={1,1,1};
unsigned int Humidity[2]={1,1};

unsigned char Data_Receive[5];

void Delay30us()//@12.000MHz
{
	unsigned char i;
	i=87;
	while(--i);
}

void Delay20ms()//@12.000MHz
{
	unsigned char i,j,k;
	i=1;
	j=234;
	k=113;
	do
	{
		do
		{
			while(--k);
		}while(--j);
	}while(--i);
}

void Delay1s()//@12.000MHz
{
	unsigned char i,j,k;
	i=46;
	j=153;
	k=245;
	do
	{
		do
		{
			while(--k);
		}while(--j);
	}while(--i);
}

void OLED_Display()//OLED显示驱动函数
{
	OLED_DataDisplay_T(56,4,Temperature);//显示温度
	OLED_DataDisplay_H(56,6,Humidity);//显示湿度
}

void DHT_Collect()//温度采集
{
	unsigned char i,j;
	unsigned char t;
	unsigned char dat8=0;//一次读取的8位数据,需要读5次
	unsigned int CheckValue=0;//校验和
	
	DHT=0;//主机发起始信号
	Delay20ms();//主机拉低总线至少18ms
	DHT=1;//主机拉高总线20~40us
	Delay30us();
	
	t=80;//设置超时等待时间
	while(DHT&&t--);//等待DHT11拉低总线
	if(t==0)//超时
	{
		DHT=1;
	}
	t=250;//设置超时等待时间
	while(!DHT&&t--);//等待DHT11拉高总线
	if(t==0)//超时
	{
		DHT=1;
	}
	t=250;//设置超时等待时间
	while(DHT&&t--);//等待DHT11拉低总线
	if(t==0)//超时
	{
		DHT=1;
	}

	for(j=0;j<5;j++)//5次读取
	{
		for(i=0;i<8;i++)//1次8个位
		{
	//等待50us开始时隙
			t=150;//设置超时等待时间
			while(!DHT&&t--);//等待DHT11拉高总线
			if(t==0)//超时
			{
				DHT=1;
			}
			t=0;//记录时间清零
			while(DHT&&++t);//等待并记录高电平持续时间
			dat8<<=1;
			if(t>30)//高电平持续时间较长(70us)
			dat8+=1;//传输值为1
		}
		Data_Receive[j]=dat8;
	}
	
	Delay30us();//等待DHT11拉低50us
	Delay30us();
	DHT=1;

	for(i=0;i<4;i++)
	{
		CheckValue+=Data_Receive[i];//计算校验值
	}
	if((unsigned char)CheckValue==Data_Receive[4])//比对
	{
		Temperature[0]=Data_Receive[2]/10;
		Temperature[1]=Data_Receive[2]%10;
		Temperature[2]=Data_Receive[3]%10;
		Humidity[0]=Data_Receive[0]/10;
		Humidity[1]=Data_Receive[0]%10;
	}
}

void sys_init()		//系统初始化函数
{
	Y_LED=1;
	R_LED=1;
	OLED_Init(); 	//OLED初始化
	OLED_CLS();		//清屏
	
	OLED_FormTitle(0,0);				//显示标题文字Linyar Lab.
	OLED_FormName(0,2);					//显示Hygrothermograph名称
	OLED_FormLine(0,3);					//显示一条横线
	OLED_FormTemp(8,4);					//显示温度文字
	OLED_FormHumi(16,6);				//显示湿度文字
	OLED_AnyChar_16X16(96,4,TempChar);	//显示摄氏度℃字符
	OLED_AnyChar_8X16(88,6,HumiChar);	//显示百分号%字符
	OLED_AnyChar_8X16(72,4,PointChar);	//显示冒号:字符
}

void main()
{
	sys_init();
	
	while(1)
	{
		DHT_Collect();		//DHT11数据采集
		Y_LED=1;R_LED=0;	//灯状态转换
		OLED_Display();		//OLED显示函数
		R_LED=1;Y_LED=0;	//灯状态转换
		Delay1s();
		Delay1s();
	}
}

OLED_Contrul_Hygrothermograph.c

#include "STC15.H"
#include "codetab_Hygrothermograph.h"//OLED字库
#include "OLED_Hygrothermograph.h"//OLED基本驱动函数

void OLED_FormName(unsigned char x, y)//在指定位置显示HFont字符组
{
	unsigned char i=0,j=0;
	for(j=0;j<17;j++)
	{
		OLED_Set_Pos(x,y);
		for(i=0;i<6;i++)
		OLED_WrDat(HFont[i+j*6]);
		x+=6;
	}
}

void OLED_FormLine(unsigned char x, y)//在指定位置显示HFont字符组
{
	unsigned char i=0,j=0;
	for(j=0;j<18;j++)
	{
		OLED_Set_Pos(x,y);
		for(i=0;i<6;i++)
		OLED_WrDat(LineFont[i]);
		x+=6;
	}
}

void OLED_FormTitle(unsigned char x,y)//主标题
{
	unsigned char i=0,j=0;
	for(j=0;j<13;j++)
	{
		OLED_Set_Pos(x,y);

		for(i=0;i<8;i++)
		{
			OLED_WrDat(TitleFont[j*16+i]);
		}
		OLED_Set_Pos(x,y+1);
		
		for(i=0;i<8;i++)
		{
			OLED_WrDat(TitleFont[j*16+i+8]);
		}
		x+=8;
	}
}

void OLED_FormTemp(unsigned char x,y)//温度标识
{
	unsigned char i=0,j=0;
	for(j=0;j<5;j++)
	{
		OLED_Set_Pos(x,y);

		for(i=0;i<8;i++)
		{
			OLED_WrDat(TempFont[j*16+i]);
		}
		OLED_Set_Pos(x,y+1);
		
		for(i=0;i<8;i++)
		{
			OLED_WrDat(TempFont[j*16+i+8]);
		}
		x+=8;
	}
}

void OLED_FormHumi(unsigned char x,y)//湿度标识
{
	unsigned char i=0,j=0;
	for(j=0;j<4;j++)
	{
		OLED_Set_Pos(x,y);

		for(i=0;i<8;i++)
		{
			OLED_WrDat(HumiFont[j*16+i]);
		}
		OLED_Set_Pos(x,y+1);
		
		for(i=0;i<8;i++)
		{
			OLED_WrDat(HumiFont[j*16+i+8]);
		}
		x+=8;
	}
}

void OLED_AnyChar_16X16(unsigned char x, y,unsigned char anystr_temp[])//在指定位置显示一个指定的16X16字符
//x:选定行第几个像素点;y:选定的行数(以16个列像素为单位);待显示的字符串
{
	unsigned char i=0;
	if(x>112)//如果指定的行超界,那么转到下一行第一个位置
	{
		x=0;y++;
	}
	
	OLED_Set_Pos(x,y);//置写入位
	
	for(i=0;i<16;i++)
	{
	OLED_WrDat(anystr_temp[i]);//在字模表中查找相应位置的字模,并写入到指定位置
	}
	
	OLED_Set_Pos(x,y+1);//写入位下移半个单位
		
	for(i=0;i<16;i++)
	{
		OLED_WrDat(anystr_temp[i+16]);
	}
}

void OLED_AnyChar_8X16(unsigned char x, y,unsigned char anystr_temp[])//在指定位置显示一个指定的8X16字符
//x:选定行第几个像素点;y:选定的行数(以16个列像素为单位);待显示的字符串
{
	unsigned char i=0;
	if(x>112)//如果指定的行超界,那么转到下一行第一个位置
	{
		x=0;y++;
	}
	
	OLED_Set_Pos(x,y);//置写入位
	
	for(i=0;i<8;i++)
	{
	OLED_WrDat(anystr_temp[i]);//在字模表中查找相应位置的字模,并写入到指定位置
	}
	
	OLED_Set_Pos(x,y+1);//写入位下移半个单位
		
	for(i=0;i<8;i++)
	{
		OLED_WrDat(anystr_temp[i+8]);
	}
}

void OLED_DataDisplay_H(unsigned char x, y,unsigned int display_temp[])//定制的数据显示函数
//x:选定行第几个像素点;y:选定的行数(以16个列像素为单位);待显示的数字字符串
{
	unsigned char i=0,j=0;
	for(j=0;j<2;j++)//写整数数据
	{
		OLED_Set_Pos(x,y);//置写入位
		for(i=0;i<8;i++)
		{
			OLED_WrDat(NumberFont[display_temp[j]*16+i]);//在字模表中查找相应位置的字模,并写入到指定位置
		}
		OLED_Set_Pos(x,y+1);//写入位下移半个单位
		for(i=0;i<8;i++)
		{
			OLED_WrDat(NumberFont[display_temp[j]*16+i+8]);
		}
		x+=8;
	}
}

void OLED_DataDisplay_T(unsigned char x, y,unsigned int display_temp[])//定制的数据显示函数
//x:选定行第几个像素点;y:选定的行数(以16个列像素为单位);待显示的数字字符串
{
	unsigned char i=0,j=0;
	for(j=0;j<2;j++)//写整数数据
	{
		OLED_Set_Pos(x,y);//置写入位
		for(i=0;i<8;i++)
		{
			OLED_WrDat(NumberFont[display_temp[j]*16+i]);//在字模表中查找相应位置的字模,并写入到指定位置
		}
		OLED_Set_Pos(x,y+1);//写入位下移半个单位
		for(i=0;i<8;i++)
		{
			OLED_WrDat(NumberFont[display_temp[j]*16+i+8]);
		}
		x+=8;
	}
	x+=8;
		OLED_Set_Pos(x,y);//置写入位
		for(i=0;i<8;i++)
		{
			OLED_WrDat(NumberFont[display_temp[j]*16+i]);//在字模表中查找相应位置的字模,并写入到指定位置
		}
		OLED_Set_Pos(x,y+1);//写入位下移半个单位
		for(i=0;i<8;i++)
		{
			OLED_WrDat(NumberFont[display_temp[j]*16+i+8]);
		}
}

OLED_Hygrothermograph.h

#include "STC15.H"

sbit SCL=P3^2; //串行时钟
sbit SDA=P3^3; //串行数据

#define high 1
#define low 0

#define	Brightness	0xCF 
#define X_WIDTH 	128
#define Y_WIDTH 	64

void delay(unsigned int z)
{
	unsigned int x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);
}

void IIC_Start()
{
   SCL = high;		
   SDA = high;
   SDA = low;
   SCL = low;
}

void IIC_Stop()
{
   SCL = low;
   SDA = low;
   SCL = high;
   SDA = high;
}

void Write_IIC_Byte(unsigned char IIC_Byte)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		if(IIC_Byte & 0x80)
			SDA=high;
		else
			SDA=low;
		SCL=high;
		SCL=low;
		IIC_Byte<<=1;
	}
	SDA=1;
	SCL=1;
	SCL=0;
}

void OLED_WrDat(unsigned char IIC_Data)
{
	IIC_Start();
	Write_IIC_Byte(0x78);
	Write_IIC_Byte(0x40);			//write data
	Write_IIC_Byte(IIC_Data);
	IIC_Stop();
}

void OLED_WrCmd(unsigned char IIC_Command)
{
	IIC_Start();
	Write_IIC_Byte(0x78);            //Slave address,SA0=0
	Write_IIC_Byte(0x00);			//write command
	Write_IIC_Byte(IIC_Command);
	IIC_Stop();
}

void OLED_Set_Pos(unsigned char x, unsigned char y) 
{ 
	OLED_WrCmd(0xb0+y);
	OLED_WrCmd(((x&0xf0)>>4)|0x10);
	OLED_WrCmd((x&0x0f)|0x01);
} 

void OLED_Fill(unsigned char bmp_dat) 
{
	unsigned char y,x;
	for(y=0;y<8;y++)
	{
		OLED_WrCmd(0xb0+y);
		OLED_WrCmd(0x01);
		OLED_WrCmd(0x10);
		for(x=0;x

codetab_Hygrothermograph.h

unsigned char code TempChar[32]={0x06,0x09,0x09,0xE6,0xF8,0x0C,0x04,0x02,0x02,0x02,0x02,0x02,0x04,0x1E,0x00,0x00,
0x00,0x00,0x00,0x07,0x1F,0x30,0x20,0x40,0x40,0x40,0x40,0x40,0x20,0x10,0x00,0x00};//放置要写入字符的字模,℃
unsigned char code HumiChar[16]={0xF0,0x08,0xF0,0x80,0x60,0x18,0x00,0x00,0x00,0x31,0x0C,0x03,0x1E,0x21,0x1E,0x00};//放置要写入字符的字模,%
unsigned char code PointChar[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00};//一个小数点,.
unsigned char LineFont[]={0x00, 0x08, 0x08, 0x08, 0x08, 0x08};//一个短线,-
/************************************6*8的点阵************************************/
unsigned char code HFont[]=
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp
0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H
0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y
0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g
0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r
0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o
0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t
0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h
0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e
0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r
0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m
0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o
0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g
0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r
0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a
0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p
0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h
};
/****************************************8*16的点阵************************************/
unsigned char code NumberFont[]={
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0
0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,//1
0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2
0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00,//3
0x00,0x00,0x80,0x40,0x30,0xF8,0x00,0x00,0x00,0x06,0x05,0x24,0x24,0x3F,0x24,0x24,//4
0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00,//5
0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00,//6
0x00,0x18,0x08,0x08,0x88,0x68,0x18,0x00,0x00,0x00,0x00,0x3E,0x01,0x00,0x00,0x00,//7
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8
0x00,0xF0,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00//9
};

unsigned char code TitleFont[]={
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,
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
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,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
};

unsigned char code TempFont[]={
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
};

unsigned char code HumiFont[]={
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
};

主要的参考来源:http://www.51hei.com/bbs/dpj-127755-1.html

你可能感兴趣的:(基于STC15F104W下的温湿度显示(DHT11)(OLED))