基于单片机SHT11温湿度传感器电路图于程序文件

合集下载

基于51单片机SHT11温湿度传感器检测程序

基于51单片机SHT11温湿度传感器检测程序

基于51单片机SHT11温湿度传感器检测程序(含电路图)下面是原理图:下面是SHT11与MCU连接的典型电路:下面是源代码:#include <reg52.h>#include <intrins.h>/******************************************************** 宏定义********************************************************/ #define uint unsigned int#define uchar unsigned char#define noACK 0#define ACK 1#define STATUS_REG_W 0x06#define STATUS_REG_R 0x07#define MEASURE_TEMP 0x03#define MEASURE_HUMI 0x05#define RESET 0x1eenum {TEMP,HUMI};typedef union //定义共用同类型{unsigned int i;float f;} value;/******************************************************** 位定义********************************************************/ sbit lcdrs=P2^0;sbit lcdrw=P2^1;sbit lcden=P2^2;sbit SCK = P1^0;sbit DATA = P1^1;/******************************************************** 变量定义********************************************************/ uchar table2[]="SHT11 温湿度检测";uchar table3[]="温度为:℃";uchar table4[]="湿度为:";uchar table5[]=".";uchar wendu[6];uchar shidu[6];/******************************************************** 1ms延时函数********************************************************/ void delay(int z){int x,y;for(x=z;x>0;x--)for(y=125;y>0;y--);}/******************************************************** 50us延时函数********************************************************/ void delay_50us(uint t){uint j;for(;t>0;t--)for(j=19;j>0;j--);}/******************************************************** 50ms延时函数********************************************************/ void delay_50ms(uint t){uint j;for(;t>0;t--)for(j=6245;j>0;j--);}/******************************************************** 12864液晶写指令********************************************************/ void write_12864com(uchar ){lcdrs=0;delay_50us(1);P0=;lcden=1;delay_50us(10);lcden=0;delay_50us(2);}/******************************************************** 12864液晶写数据********************************************************/ void write_dat(uchar dat){lcdrs=1;lcdrw=0;delay_50us(1);P0=dat;lcden=1;delay_50us(10);lcden=0;delay_50us(2);}/******************************************************** 12864液晶初始化********************************************************/ void init12864lcd(void){delay_50ms(2);write_12864com(0x30);delay_50us(4);write_12864com(0x30);delay_50us(4);write_12864com(0x0f);delay_50us(4);write_12864com(0x01);delay_50us(240);write_12864com(0x06);delay_50us(10);write_12864com(0x0c);delay_50us(10);}/********************************************************12864液晶显示函数********************************************************/ void display1(void){uchar i;write_12864com(0x80);for(i=0;i<18;i++){write_dat(table2[i]);delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/ void display2(void){uchar i;write_12864com(0x90);for(i=0;i<18;i++){write_dat(table3[i]);delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/ void display3(void){uchar i;write_12864com(0x88);for(i=0;i<8;i++){write_dat(table4[i]);delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/void displaywendu(void) {uchar i;write_12864com(0x94); for(i=0;i<3;i++){write_dat(wendu[i]); delay_50us(1);}for(i=0;i<1;i++){write_dat(table5[i]); delay_50us(1);}for(i=4;i<5;i++){write_dat(wendu[i]); delay_50us(1);}}/******************************************************** 12864液晶显示函数********************************************************/ void displayshidu(void){uchar i;write_12864com(0x8C);for(i=0;i<3;i++){write_dat(shidu[i]);delay_50us(1);}for(i=0;i<1;i++){write_dat(table5[i]);delay_50us(1);}for(i=4;i<5;i++){write_dat(shidu[i]);delay_50us(1);}}/******************************************************** SHT11写字节程序********************************************************/ char s_write_byte(unsigned char value){unsigned char i,error=0;for (i=0x80;i>0;i>>=1) //高位为1,循环右移{if (i&value) DATA=1; //和要发送的数相与,结果为发送的位else DATA=0;SCK=1;_nop_();_nop_();_nop_(); //延时3usSCK=0;}DATA=1; //释放数据线SCK=1;error=DATA; //检查应答信号,确认通讯正常_nop_();_nop_();_nop_();SCK=0;DATA=1;return error; //error=1 通讯错误}/******************************************************** SHT11读字节程序********************************************************/ char s_read_byte(unsigned char ack){unsigned char i,val=0;DATA=1; //释放数据线for(i=0x80;i>0;i>>=1) //高位为1,循环右移{SCK=1;if(DATA) val=(val|i); //读一位数据线的值SCK=0;}DATA=!ack; //如果是校验,读取完后结束通讯 ;SCK=1;_nop_();_nop_();_nop_(); //延时3usSCK=0;_nop_();_nop_();_nop_();DATA=1; //释放数据线return val;}/******************************************************** SHT11启动传输********************************************************/ void s_transstart(void){DATA=1; SCK=0; //准备_nop_();SCK=1;_nop_();DATA=0;_nop_();SCK=0;_nop_();_nop_();_nop_();SCK=1;_nop_();DATA=1;_nop_();SCK=0;}/********************************************************SHT11连接复位********************************************************/void s_connectionreset(void){unsigned char i;DATA=1; SCK=0; //准备for(i=0;i<9;i++) //DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位{SCK=1;SCK=0;}s_transstart(); //启动传输}/********************************************************SHT11温湿度检测********************************************************/char s_measure(unsigned char *p_value, unsigned char *p_checksum,unsigned charmode){unsigned error=0;unsigned int i;s_transstart(); //启动传输switch(mode) //选择发送命令{case TEMP : error+=s_write_byte(MEASURE_TEMP); break; //测量温度case HUMI : error+=s_write_byte(MEASURE_HUMI); break; //测量湿度default : break;}for (i=0;i<65535;i++) if(DATA==0) break; //等待测量结束if(DATA) error+=1; // 如果长时间数据线没有拉低,说明测量错误*(p_value) =s_read_byte(ACK); //读第一个字节,高字节(MSB)*(p_value+1)=s_read_byte(ACK); //读第二个字节,低字节(LSB)*p_checksum =s_read_byte(noACK); //read CRC校验码return error; // error=1 通讯错误}/********************************************************SHT11温湿度值标度变换及温度补偿********************************************************/void calc_sth10(float *p_humidity ,float *p_temperature){const float C1=-4.0; // 12位湿度精度修正公式const float C2=+0.0405; // 12位湿度精度修正公式const float C3=-0.0000028; // 12位湿度精度修正公式const float T1=+0.01; // 14位温度精度5V条件修正公式const float T2=+0.00008; // 14位温度精度5V条件修正公式float rh=*p_humidity; // rh: 12位湿度float t=*p_temperature; // t: 14位温度float rh_lin; // rh_lin: 湿度linear值float rh_true; // rh_true: 湿度ture值float t_C; // t_C : 温度℃t_C=t*0.01 - 40; //补偿温度rh_lin=C3*rh*rh + C2*rh + C1; //相对湿度非线性补偿rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //相对湿度对于温度依赖性补偿if(rh_true>100)rh_true=100; //湿度最大修正if(rh_true<0.1)rh_true=0.1; //湿度最小修正*p_temperature=t_C; //返回温度结果*p_humidity=rh_true; //返回湿度结果}/********************************************************主函数********************************************************/void main(void){unsigned int temp,humi;value humi_val,temp_val; //定义两个共同体,一个用于湿度,一个用于温度unsigned char error; //用于检验是否出现错误unsigned char checksum; //CRCinit12864lcd();display1();display2();display3();s_connectionreset(); //启动连接复位while(1){error=0; //初始化error=0,即没有错误error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP); //温度测量error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI); //湿度测量if(error!=0) s_connectionreset(); ////如果发生错误,系统复位else{humi_val.f=(float)humi_val.i; //转换为浮点数temp_val.f=(float)temp_val.i; //转换为浮点数calc_sth10(&humi_val.f,&temp_val.f); //修正相对湿度及温度temp=temp_val.f*10;humi=humi_val.f*10;wendu[0]=temp/1000+'0'; //温度百位wendu[1]=temp%1000/100+'0'; //温度十位wendu[2]=temp%100/10+'0'; //温度个位wendu[3]=0x2E; //小数点wendu[4]=temp%10+'0'; //温度小数点后第一位displaywendu();shidu[0]=humi/1000+'0'; //湿度百位shidu[1]=humi%1000/100+'0'; //湿度十位shidu[2]=humi%100/10+'0'; //湿度个位shidu[3]=0x2E; //小数点shidu[4]=humi%10+'0'; //湿度小数点后第一位displayshidu();}delay(800); //等待足够长的时间,以现行下一次转换}}相关手册资料及源码下载地址:基于51单片机SHT11温湿度传感器检测程序相关资料。

STM32单片机DHT11数字温湿度监测

STM32单片机DHT11数字温湿度监测

STM32单片机DHT11数字温湿度监测OLED液晶接口电路图DHT11传感器接口电路图STM32单片机DHT11数字温湿度监测程序源代码#include "sys.h"#define LED_RED PBout(12) //红色发光二极管控制管脚PB12 #define LED_GREEN PBout(13) //绿色发光二极管控制管脚PB13 #define LED_YELLOW PBout(14) //黄色发光二极管控制管脚PB14 #define LED_BLUE PBout(15) //蓝色发光二极管控制管脚PB15 #define DHT11_IO_IN() {GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=8<<0;}#define DHT11_IO_OUT() {GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=3<<0;}#define DHT11_DQ_OUT PAout(0) //数据端口PA0#define DHT11_DQ_IN PAin(0) //数据端口PA0#define OLED_RST_Clr() PCout(13)=0 //RST#define OLED_RST_Set() PCout(13)=1 //RST#define OLED_RS_Clr() PBout(4)=0 //DC#define OLED_RS_Set() PBout(4)=1 //DC#define OLED_SCLK_Clr() PCout(15)=0 //SCL#define OLED_SCLK_Set() PCout(15)=1 //SCL#define OLED_SDIN_Clr() PCout(14)=0 //SDA#define OLED_SDIN_Set() PCout(14)=1 //SDA#define OLED_CMD 0 //写命令#define OLED_DATA 1 //写数据void delay_init(u8 SYSCLK);void delay_ms(u16 nms);void delay_us(u32 nus);void Led_Init(void); //发光二极管控制管脚初始化void Red_Led_Light(void); //点亮红色发光二极管void Green_Led_Light(void); //点亮绿色发光二极管void Yellow_Led_Light(void); //点亮黄色发光二极管void Blue_Led_Light(void); //点亮蓝色发光二极管void Red_Led_Goout(void); //熄灭红色发光二极管void Green_Led_Goout(void); //熄灭绿色发光二极管void Yellow_Led_Goout(void); //熄灭黄色发光二极管void Blue_Led_Goout(void); //熄灭蓝色发光二极管u8 DHT11_Init(void); //初始化DHT11u8 DHT11_Read_Data(u8 *temp,u8 *humi); //读取温湿度u8 DHT11_Read_Byte(void); //读出一个字节u8 DHT11_Read_Bit(void); //读出一个位u8 DHT11_Check(void); //检测是否存在DHT11void DHT11_Rst(void); //复位DHT11void OLED_WR_Byte(u8 dat,u8 cmd);void OLED_Display_On(void);void OLED_Display_Off(void);void OLED_Refresh_Gram(void);void Oled_Init(void);void OLED_Clear(void);void OLED_DrawPoint(u8 x,u8 y,u8 t);void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size,u8 mode);void OLED_ShowNumber(u8 x,u8 y,u32 num,u8 len,u8 size);void OLED_ShowString(u8 x,u8 y,const u8 *p);void Oled_Show(void);static u8 fac_us=0; //us延时倍乘数static u16 fac_ms=0; //ms延时倍乘数u8 OLED_GRAM[128][8];const unsigned char oled_asc2_1206[95][12]={{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/ {0x00,0x00,0x00,0x00,0x3F,0x40,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/ {0x00,0x00,0x30,0x00,0x40,0x00,0x30,0x00,0x40,0x00,0x00,0x00},/*""",2*/ {0x09,0x00,0x0B,0xC0,0x3D,0x00,0x0B,0xC0,0x3D,0x00,0x09,0x00},/*"#",3*/ {0x18,0xC0,0x24,0x40,0x7F,0xE0,0x22,0x40,0x31,0x80,0x00,0x00},/*"$",4*/{0x18,0x00,0x24,0xC0,0x1B,0x00,0x0D,0x80,0x32,0x40,0x01,0x80},/*"%",5*/ {0x03,0x80,0x1C,0x40,0x27,0x40,0x1C,0x80,0x07,0x40,0x00,0x40},/*"&",6*/ {0x10,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",7*/ {0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x80,0x20,0x40,0x40,0x20},/*"(",8*/ {0x00,0x00,0x40,0x20,0x20,0x40,0x1F,0x80,0x00,0x00,0x00,0x00},/*")",9*/ {0x09,0x00,0x06,0x00,0x1F,0x80,0x06,0x00,0x09,0x00,0x00,0x00},/*"*",10*/ {0x04,0x00,0x04,0x00,0x3F,0x80,0x04,0x00,0x04,0x00,0x00,0x00},/*"+",11*/ {0x00,0x10,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",12*/ {0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x00,0x00},/*"-",13*/ {0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",14*/ {0x00,0x20,0x01,0xC0,0x06,0x00,0x38,0x00,0x40,0x00,0x00,0x00},/*"/",15*/ {0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"0",16*/ {0x00,0x00,0x10,0x40,0x3F,0xC0,0x00,0x40,0x00,0x00,0x00,0x00},/*"1",17*/ {0x18,0xC0,0x21,0x40,0x22,0x40,0x24,0x40,0x18,0x40,0x00,0x00},/*"2",18*/ {0x10,0x80,0x20,0x40,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"3",19*/ {0x02,0x00,0x0D,0x00,0x11,0x00,0x3F,0xC0,0x01,0x40,0x00,0x00},/*"4",20*/ {0x3C,0x80,0x24,0x40,0x24,0x40,0x24,0x40,0x23,0x80,0x00,0x00},/*"5",21*/ {0x1F,0x80,0x24,0x40,0x24,0x40,0x34,0x40,0x03,0x80,0x00,0x00},/*"6",22*/ {0x30,0x00,0x20,0x00,0x27,0xC0,0x38,0x00,0x20,0x00,0x00,0x00},/*"7",23*/ {0x1B,0x80,0x24,0x40,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"8",24*/ {0x1C,0x00,0x22,0xC0,0x22,0x40,0x22,0x40,0x1F,0x80,0x00,0x00},/*"9",25*/ {0x00,0x00,0x00,0x00,0x08,0x40,0x00,0x00,0x00,0x00,0x00,0x00},/*":",26*/ {0x00,0x00,0x00,0x00,0x04,0x60,0x00,0x00,0x00,0x00,0x00,0x00},/*";",27*/ {0x00,0x00,0x04,0x00,0x0A,0x00,0x11,0x00,0x20,0x80,0x40,0x40},/*"<",28*/ {0x09,0x00,0x09,0x00,0x09,0x00,0x09,0x00,0x09,0x00,0x00,0x00},/*"=",29*/ {0x00,0x00,0x40,0x40,0x20,0x80,0x11,0x00,0x0A,0x00,0x04,0x00},/*">",30*/ {0x18,0x00,0x20,0x00,0x23,0x40,0x24,0x00,0x18,0x00,0x00,0x00},/*"?",31*/ {0x1F,0x80,0x20,0x40,0x27,0x40,0x29,0x40,0x1F,0x40,0x00,0x00},/*"@",32*/ {0x00,0x40,0x07,0xC0,0x39,0x00,0x0F,0x00,0x01,0xC0,0x00,0x40},/*"A",33*/ {0x20,0x40,0x3F,0xC0,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"B",34*/ {0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x30,0x80,0x00,0x00},/*"C",35*/ {0x20,0x40,0x3F,0xC0,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"D",36*/ {0x20,0x40,0x3F,0xC0,0x24,0x40,0x2E,0x40,0x30,0xC0,0x00,0x00},/*"E",37*/ {0x20,0x40,0x3F,0xC0,0x24,0x40,0x2E,0x00,0x30,0x00,0x00,0x00},/*"F",38*/ {0x0F,0x00,0x10,0x80,0x20,0x40,0x22,0x40,0x33,0x80,0x02,0x00},/*"G",39*/ {0x20,0x40,0x3F,0xC0,0x04,0x00,0x04,0x00,0x3F,0xC0,0x20,0x40},/*"H",40*/ {0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,0x20,0x40,0x00,0x00},/*"I",41*/ {0x00,0x60,0x20,0x20,0x20,0x20,0x3F,0xC0,0x20,0x00,0x20,0x00},/*"J",42*/ {0x20,0x40,0x3F,0xC0,0x24,0x40,0x0B,0x00,0x30,0xC0,0x20,0x40},/*"K",43*/ {0x20,0x40,0x3F,0xC0,0x20,0x40,0x00,0x40,0x00,0x40,0x00,0xC0},/*"L",44*/ {0x3F,0xC0,0x3C,0x00,0x03,0xC0,0x3C,0x00,0x3F,0xC0,0x00,0x00},/*"M",45*/ {0x20,0x40,0x3F,0xC0,0x0C,0x40,0x23,0x00,0x3F,0xC0,0x20,0x00},/*"N",46*/ {0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"O",47*/ {0x20,0x40,0x3F,0xC0,0x24,0x40,0x24,0x00,0x18,0x00,0x00,0x00},/*"P",48*/{0x1F,0x80,0x21,0x40,0x21,0x40,0x20,0xE0,0x1F,0xA0,0x00,0x00},/*"Q",49*/ {0x20,0x40,0x3F,0xC0,0x24,0x40,0x26,0x00,0x19,0xC0,0x00,0x40},/*"R",50*/ {0x18,0xC0,0x24,0x40,0x24,0x40,0x22,0x40,0x31,0x80,0x00,0x00},/*"S",51*/ {0x30,0x00,0x20,0x40,0x3F,0xC0,0x20,0x40,0x30,0x00,0x00,0x00},/*"T",52*/ {0x20,0x00,0x3F,0x80,0x00,0x40,0x00,0x40,0x3F,0x80,0x20,0x00},/*"U",53*/ {0x20,0x00,0x3E,0x00,0x01,0xC0,0x07,0x00,0x38,0x00,0x20,0x00},/*"V",54*/ {0x38,0x00,0x07,0xC0,0x3C,0x00,0x07,0xC0,0x38,0x00,0x00,0x00},/*"W",55*/ {0x20,0x40,0x39,0xC0,0x06,0x00,0x39,0xC0,0x20,0x40,0x00,0x00},/*"X",56*/ {0x20,0x00,0x38,0x40,0x07,0xC0,0x38,0x40,0x20,0x00,0x00,0x00},/*"Y",57*/ {0x30,0x40,0x21,0xC0,0x26,0x40,0x38,0x40,0x20,0xC0,0x00,0x00},/*"Z",58*/ {0x00,0x00,0x00,0x00,0x7F,0xE0,0x40,0x20,0x40,0x20,0x00,0x00},/*"[",59*/ {0x00,0x00,0x70,0x00,0x0C,0x00,0x03,0x80,0x00,0x40,0x00,0x00},/*"\",60*/ {0x00,0x00,0x40,0x20,0x40,0x20,0x7F,0xE0,0x00,0x00,0x00,0x00},/*"]",61*/ {0x00,0x00,0x20,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00},/*"^",62*/ {0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10},/*"_",63*/ {0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"`",64*/ {0x00,0x00,0x02,0x80,0x05,0x40,0x05,0x40,0x03,0xC0,0x00,0x40},/*"a",65*/ {0x20,0x00,0x3F,0xC0,0x04,0x40,0x04,0x40,0x03,0x80,0x00,0x00},/*"b",66*/ {0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x40,0x06,0x40,0x00,0x00},/*"c",67*/ {0x00,0x00,0x03,0x80,0x04,0x40,0x24,0x40,0x3F,0xC0,0x00,0x40},/*"d",68*/ {0x00,0x00,0x03,0x80,0x05,0x40,0x05,0x40,0x03,0x40,0x00,0x00},/*"e",69*/ {0x00,0x00,0x04,0x40,0x1F,0xC0,0x24,0x40,0x24,0x40,0x20,0x00},/*"f",70*/ {0x00,0x00,0x02,0xE0,0x05,0x50,0x05,0x50,0x06,0x50,0x04,0x20},/*"g",71*/ {0x20,0x40,0x3F,0xC0,0x04,0x40,0x04,0x00,0x03,0xC0,0x00,0x40},/*"h",72*/ {0x00,0x00,0x04,0x40,0x27,0xC0,0x00,0x40,0x00,0x00,0x00,0x00},/*"i",73*/ {0x00,0x10,0x00,0x10,0x04,0x10,0x27,0xE0,0x00,0x00,0x00,0x00},/*"j",74*/ {0x20,0x40,0x3F,0xC0,0x01,0x40,0x07,0x00,0x04,0xC0,0x04,0x40},/*"k",75*/ {0x20,0x40,0x20,0x40,0x3F,0xC0,0x00,0x40,0x00,0x40,0x00,0x00},/*"l",76*/ {0x07,0xC0,0x04,0x00,0x07,0xC0,0x04,0x00,0x03,0xC0,0x00,0x00},/*"m",77*/ {0x04,0x40,0x07,0xC0,0x04,0x40,0x04,0x00,0x03,0xC0,0x00,0x40},/*"n",78*/ {0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x40,0x03,0x80,0x00,0x00},/*"o",79*/ {0x04,0x10,0x07,0xF0,0x04,0x50,0x04,0x40,0x03,0x80,0x00,0x00},/*"p",80*/ {0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x50,0x07,0xF0,0x00,0x10},/*"q",81*/ {0x04,0x40,0x07,0xC0,0x02,0x40,0x04,0x00,0x04,0x00,0x00,0x00},/*"r",82*/ {0x00,0x00,0x06,0x40,0x05,0x40,0x05,0x40,0x04,0xC0,0x00,0x00},/*"s",83*/ {0x00,0x00,0x04,0x00,0x1F,0x80,0x04,0x40,0x00,0x40,0x00,0x00},/*"t",84*/ {0x04,0x00,0x07,0x80,0x00,0x40,0x04,0x40,0x07,0xC0,0x00,0x40},/*"u",85*/ {0x04,0x00,0x07,0x00,0x04,0xC0,0x01,0x80,0x06,0x00,0x04,0x00},/*"v",86*/ {0x06,0x00,0x01,0xC0,0x07,0x00,0x01,0xC0,0x06,0x00,0x00,0x00},/*"w",87*/ {0x04,0x40,0x06,0xC0,0x01,0x00,0x06,0xC0,0x04,0x40,0x00,0x00},/*"x",88*/ {0x04,0x10,0x07,0x10,0x04,0xE0,0x01,0x80,0x06,0x00,0x04,0x00},/*"y",89*/ {0x00,0x00,0x04,0x40,0x05,0xC0,0x06,0x40,0x04,0x40,0x00,0x00},/*"z",90*/ {0x00,0x00,0x00,0x00,0x04,0x00,0x7B,0xE0,0x40,0x20,0x00,0x00},/*"{",91*/ {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xF0,0x00,0x00,0x00,0x00},/*"|",92*/{0x00,0x00,0x40,0x20,0x7B,0xE0,0x04,0x00,0x00,0x00,0x00,0x00},/*"}",93*/ {0x40,0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x20,0x00,0x40,0x00},/*"~",94*/ };const unsigned char oled_asc2_1608[95][16]={{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xCC,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/{0x00,0x00,0x08,0x00,0x30,0x00,0x60,0x00,0x08,0x00,0x30,0x00,0x60,0x00,0x00,0x00},/*""",2*/{0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x00,0x00},/*"#",3*/{0x00,0x00,0x0E,0x18,0x11,0x04,0x3F,0xFF,0x10,0x84,0x0C,0x78,0x00,0x00,0x00,0x00},/*"$",4*/{0x0F,0x00,0x10,0x84,0x0F,0x38,0x00,0xC0,0x07,0x78,0x18,0x84,0x00,0x78,0x00,0x00},/*"%",5*/{0x00,0x78,0x0F,0x84,0x10,0xC4,0x11,0x24,0x0E,0x98,0x00,0xE4,0x00,0x84,0x00,0x08},/*"&",6*/{0x08,0x00,0x68,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",7*/{0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x18,0x18,0x20,0x04,0x40,0x02,0x00,0x00},/*"(",8*/{0x00,0x00,0x40,0x02,0x20,0x04,0x18,0x18,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00},/*")",9*/{0x02,0x40,0x02,0x40,0x01,0x80,0x0F,0xF0,0x01,0x80,0x02,0x40,0x02,0x40,0x00,0x00},/*"*",10*/{0x00,0x80,0x00,0x80,0x00,0x80,0x0F,0xF8,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x00},/*"+",11*/{0x00,0x01,0x00,0x0D,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",12*/{0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80},/*"-",13*/{0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",14*/{0x00,0x00,0x00,0x06,0x00,0x18,0x00,0x60,0x01,0x80,0x06,0x00,0x18,0x00,0x20,0x00},/*"/",15*/{0x00,0x00,0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"0",16*/{0x00,0x00,0x08,0x04,0x08,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"1",17*/{0x00,0x00,0x0E,0x0C,0x10,0x14,0x10,0x24,0x10,0x44,0x11,0x84,0x0E,0x0C,0x00,0x00},/*"2",18*/{0x00,0x00,0x0C,0x18,0x10,0x04,0x11,0x04,0x11,0x04,0x12,0x88,0x0C,0x70,0x00,0x00},/*"3",19*/0x00,0x24,0x00,0x00},/*"4",20*/{0x00,0x00,0x1F,0x98,0x10,0x84,0x11,0x04,0x11,0x04,0x10,0x88, 0x10,0x70,0x00,0x00},/*"5",21*/{0x00,0x00,0x07,0xF0,0x08,0x88,0x11,0x04,0x11,0x04,0x18,0x88, 0x00,0x70,0x00,0x00},/*"6",22*/{0x00,0x00,0x1C,0x00,0x10,0x00,0x10,0xFC,0x13,0x00,0x1C,0x00, 0x10,0x00,0x00,0x00},/*"7",23*/{0x00,0x00,0x0E,0x38,0x11,0x44,0x10,0x84,0x10,0x84,0x11,0x44, 0x0E,0x38,0x00,0x00},/*"8",24*/{0x00,0x00,0x07,0x00,0x08,0x8C,0x10,0x44,0x10,0x44,0x08,0x88, 0x07,0xF0,0x00,0x00},/*"9",25*/{0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x03,0x0C,0x00,0x00, 0x00,0x00,0x00,0x00},/*":",26*/{0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x06,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00},/*";",27*/{0x00,0x00,0x00,0x80,0x01,0x40,0x02,0x20,0x04,0x10,0x08,0x08, 0x10,0x04,0x00,0x00},/*"<",28*/{0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20, 0x02,0x20,0x00,0x00},/*"=",29*/{0x00,0x00,0x10,0x04,0x08,0x08,0x04,0x10,0x02,0x20,0x01,0x40, 0x00,0x80,0x00,0x00},/*">",30*/{0x00,0x00,0x0E,0x00,0x12,0x00,0x10,0x0C,0x10,0x6C,0x10,0x80, 0x0F,0x00,0x00,0x00},/*"?",31*/{0x03,0xE0,0x0C,0x18,0x13,0xE4,0x14,0x24,0x17,0xC4,0x08,0x28, 0x07,0xD0,0x00,0x00},/*"@",32*/{0x00,0x04,0x00,0x3C,0x03,0xC4,0x1C,0x40,0x07,0x40,0x00,0xE4, 0x00,0x1C,0x00,0x04},/*"A",33*/{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x11,0x04,0x0E,0x88, 0x00,0x70,0x00,0x00},/*"B",34*/{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x04,0x10,0x08, 0x1C,0x10,0x00,0x00},/*"C",35*/{0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08, 0x07,0xF0,0x00,0x00},/*"D",36*/{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x17,0xC4,0x10,0x04, 0x08,0x18,0x00,0x00},/*"E",37*/{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x17,0xC0,0x10,0x00, 0x08,0x00,0x00,0x00},/*"F",38*/{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x44,0x1C,0x78, 0x00,0x40,0x00,0x00},/*"G",39*/{0x10,0x04,0x1F,0xFC,0x10,0x84,0x00,0x80,0x00,0x80,0x10,0x84, 0x1F,0xFC,0x10,0x04},/*"H",40*/{0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04, 0x00,0x00,0x00,0x00},/*"I",41*/0x10,0x00,0x00,0x00},/*"J",42*/{0x10,0x04,0x1F,0xFC,0x11,0x04,0x03,0x80,0x14,0x64,0x18,0x1C, 0x10,0x04,0x00,0x00},/*"K",43*/{0x10,0x04,0x1F,0xFC,0x10,0x04,0x00,0x04,0x00,0x04,0x00,0x04, 0x00,0x0C,0x00,0x00},/*"L",44*/{0x10,0x04,0x1F,0xFC,0x1F,0x00,0x00,0xFC,0x1F,0x00,0x1F,0xFC, 0x10,0x04,0x00,0x00},/*"M",45*/{0x10,0x04,0x1F,0xFC,0x0C,0x04,0x03,0x00,0x00,0xE0,0x10,0x18, 0x1F,0xFC,0x10,0x00},/*"N",46*/{0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08, 0x07,0xF0,0x00,0x00},/*"O",47*/{0x10,0x04,0x1F,0xFC,0x10,0x84,0x10,0x80,0x10,0x80,0x10,0x80, 0x0F,0x00,0x00,0x00},/*"P",48*/{0x07,0xF0,0x08,0x18,0x10,0x24,0x10,0x24,0x10,0x1C,0x08,0x0A, 0x07,0xF2,0x00,0x00},/*"Q",49*/{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x11,0xC0,0x11,0x30, 0x0E,0x0C,0x00,0x04},/*"R",50*/{0x00,0x00,0x0E,0x1C,0x11,0x04,0x10,0x84,0x10,0x84,0x10,0x44, 0x1C,0x38,0x00,0x00},/*"S",51*/{0x18,0x00,0x10,0x00,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x00, 0x18,0x00,0x00,0x00},/*"T",52*/{0x10,0x00,0x1F,0xF8,0x10,0x04,0x00,0x04,0x00,0x04,0x10,0x04, 0x1F,0xF8,0x10,0x00},/*"U",53*/{0x10,0x00,0x1E,0x00,0x11,0xE0,0x00,0x1C,0x00,0x70,0x13,0x80, 0x1C,0x00,0x10,0x00},/*"V",54*/{0x1F,0xC0,0x10,0x3C,0x00,0xE0,0x1F,0x00,0x00,0xE0,0x10,0x3C, 0x1F,0xC0,0x00,0x00},/*"W",55*/{0x10,0x04,0x18,0x0C,0x16,0x34,0x01,0xC0,0x01,0xC0,0x16,0x34, 0x18,0x0C,0x10,0x04},/*"X",56*/{0x10,0x00,0x1C,0x00,0x13,0x04,0x00,0xFC,0x13,0x04,0x1C,0x00, 0x10,0x00,0x00,0x00},/*"Y",57*/{0x08,0x04,0x10,0x1C,0x10,0x64,0x10,0x84,0x13,0x04,0x1C,0x04, 0x10,0x18,0x00,0x00},/*"Z",58*/{0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFE,0x40,0x02,0x40,0x02, 0x40,0x02,0x00,0x00},/*"[",59*/{0x00,0x00,0x30,0x00,0x0C,0x00,0x03,0x80,0x00,0x60,0x00,0x1C, 0x00,0x03,0x00,0x00},/*"\",60*/{0x00,0x00,0x40,0x02,0x40,0x02,0x40,0x02,0x7F,0xFE,0x00,0x00, 0x00,0x00,0x00,0x00},/*"]",61*/{0x00,0x00,0x00,0x00,0x20,0x00,0x40,0x00,0x40,0x00,0x40,0x00, 0x20,0x00,0x00,0x00},/*"^",62*/{0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01, 0x00,0x01,0x00,0x01},/*"_",63*/0x00,0x00,0x00,0x00},/*"`",64*/{0x00,0x00,0x00,0x98,0x01,0x24,0x01,0x44,0x01,0x44,0x01,0x44, 0x00,0xFC,0x00,0x04},/*"a",65*/{0x10,0x00,0x1F,0xFC,0x00,0x88,0x01,0x04,0x01,0x04,0x00,0x88, 0x00,0x70,0x00,0x00},/*"b",66*/{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x04, 0x00,0x88,0x00,0x00},/*"c",67*/{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x11,0x08, 0x1F,0xFC,0x00,0x04},/*"d",68*/{0x00,0x00,0x00,0xF8,0x01,0x44,0x01,0x44,0x01,0x44,0x01,0x44, 0x00,0xC8,0x00,0x00},/*"e",69*/{0x00,0x00,0x01,0x04,0x01,0x04,0x0F,0xFC,0x11,0x04,0x11,0x04, 0x11,0x00,0x18,0x00},/*"f",70*/{0x00,0x00,0x00,0xD6,0x01,0x29,0x01,0x29,0x01,0x29,0x01,0xC9, 0x01,0x06,0x00,0x00},/*"g",71*/{0x10,0x04,0x1F,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04, 0x00,0xFC,0x00,0x04},/*"h",72*/{0x00,0x00,0x01,0x04,0x19,0x04,0x19,0xFC,0x00,0x04,0x00,0x04, 0x00,0x00,0x00,0x00},/*"i",73*/{0x00,0x00,0x00,0x03,0x00,0x01,0x01,0x01,0x19,0x01,0x19,0xFE, 0x00,0x00,0x00,0x00},/*"j",74*/{0x10,0x04,0x1F,0xFC,0x00,0x24,0x00,0x40,0x01,0xB4,0x01,0x0C, 0x01,0x04,0x00,0x00},/*"k",75*/{0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04, 0x00,0x00,0x00,0x00},/*"l",76*/{0x01,0x04,0x01,0xFC,0x01,0x04,0x01,0x00,0x01,0xFC,0x01,0x04, 0x01,0x00,0x00,0xFC},/*"m",77*/{0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04, 0x00,0xFC,0x00,0x04},/*"n",78*/{0x00,0x00,0x00,0xF8,0x01,0x04,0x01,0x04,0x01,0x04,0x01,0x04, 0x00,0xF8,0x00,0x00},/*"o",79*/{0x01,0x01,0x01,0xFF,0x00,0x85,0x01,0x04,0x01,0x04,0x00,0x88, 0x00,0x70,0x00,0x00},/*"p",80*/{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x05, 0x01,0xFF,0x00,0x01},/*"q",81*/{0x01,0x04,0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x04,0x01,0x00, 0x01,0x80,0x00,0x00},/*"r",82*/{0x00,0x00,0x00,0xCC,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x24, 0x01,0x98,0x00,0x00},/*"s",83*/{0x00,0x00,0x01,0x00,0x01,0x00,0x07,0xF8,0x01,0x04,0x01,0x04, 0x00,0x00,0x00,0x00},/*"t",84*/{0x01,0x00,0x01,0xF8,0x00,0x04,0x00,0x04,0x00,0x04,0x01,0x08, 0x01,0xFC,0x00,0x04},/*"u",85*/0x01,0x80,0x01,0x00},/*"v",86*/{0x01,0xF0,0x01,0x0C,0x00,0x30,0x01,0xC0,0x00,0x30,0x01,0x0C,0x01,0xF0,0x01,0x00},/*"w",87*/{0x00,0x00,0x01,0x04,0x01,0x8C,0x00,0x74,0x01,0x70,0x01,0x8C,0x01,0x04,0x00,0x00},/*"x",88*/{0x01,0x01,0x01,0x81,0x01,0x71,0x00,0x0E,0x00,0x18,0x01,0x60,0x01,0x80,0x01,0x00},/*"y",89*/{0x00,0x00,0x01,0x84,0x01,0x0C,0x01,0x34,0x01,0x44,0x01,0x84,0x01,0x0C,0x00,0x00},/*"z",90*/{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x3E,0xFC,0x40,0x02,0x40,0x02},/*"{",91*/{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},/*"|",92*/{0x00,0x00,0x40,0x02,0x40,0x02,0x3E,0xFC,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"}",93*/{0x00,0x00,0x60,0x00,0x80,0x00,0x80,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x20,0x00},/*"~",94*/};void delay_init(u8 SYSCLK){SysTick->CTRL&=0xfffffffb; //bit2清空,选择外部时钟 HCLK/8 fac_us=SYSCLK/8;fac_ms=(u16)fac_us*1000;}void delay_ms(u16 nms){u32 temp;SysTick->LOAD=(u32)nms*fac_ms; //时间加载(SysTick->LOAD为24bit) SysTick->VAL =0x00; //清空计数器SysTick->CTRL=0x01 ; //开始倒数do{temp=SysTick->CTRL;}while(temp&0x01&&!(temp&(1<<16))); //等待时间到达SysTick->CTRL=0x00; //关闭计数器SysTick->VAL =0X00; //清空计数器}void delay_us(u32 nus){u32 temp;SysTick->LOAD=nus*fac_us; //时间加载SysTick->VAL=0x00; //清空计数器SysTick->CTRL=0x01 ; //开始倒数do{temp=SysTick->CTRL;}while(temp&0x01&&!(temp&(1<<16))); //等待时间到达SysTick->CTRL=0x00; //关闭计数器SysTick->VAL =0X00; //清空计数器}void Led_Init(void){RCC->APB2ENR|=1<<3; //使能PORTB接口时钟GPIOB->CRH&=0XFFF0FFFF;GPIOB->CRH|=0X00030000; //PB12推挽输出GPIOB->ODR|=1<<12; //PB12输出高电平GPIOB->CRH&=0XFF0FFFFF;GPIOB->CRH|=0X00300000; //PB13推挽输出GPIOB->ODR|=1<<13; //PB13输出高电平GPIOB->CRH&=0XF0FFFFFF;GPIOB->CRH|=0X03000000; //PB14推挽输出GPIOB->ODR|=1<<14; //PB14输出高电平GPIOB->CRH&=0X0FFFFFFF;GPIOB->CRH|=0X30000000; //PB15推挽输出GPIOB->ODR|=1<<15; //PB15输出高电平}void Red_Led_Light(void){LED_RED=0;}void Red_Led_Goout(void){LED_RED=1;}void Green_Led_Light(void){LED_GREEN=0;}void Green_Led_Goout(void){LED_GREEN=1;}void Yellow_Led_Light(void){LED_YELLOW=0;}void Yellow_Led_Goout(void){LED_YELLOW=1;}void Blue_Led_Light(void){LED_BLUE=0;}void Blue_Led_Goout(void){LED_BLUE=1;}void DHT11_Rst(void){DHT11_IO_OUT(); //SET OUTPUTDHT11_DQ_OUT=0; //拉低DQdelay_ms(20); //拉低至少18msDHT11_DQ_OUT=1; //DQ=1delay_us(30); //主机拉高20~40us}u8 DHT11_Check(void){u8 retry=0;DHT11_IO_IN();while(DHT11_DQ_IN&&retry<100) //DHT11会拉低40~80us {retry++;delay_us(1);};if(retry>=100)return 1;else retry=0;while (!DHT11_DQ_IN&&retry<100) //DHT11拉低后会再次拉高40~80us {retry++;delay_us(1);};if(retry>=100)return 1;return 0;}u8 DHT11_Read_Bit(void){u8 retry=0;while(DHT11_DQ_IN&&retry<100) //等待变为低电平{retry++;delay_us(1);}retry=0;while(!DHT11_DQ_IN&&retry<100) //等待变高电平{retry++;delay_us(1);}delay_us(40); //等待40usif(DHT11_DQ_IN)return 1;else return 0;}u8 DHT11_Read_Byte(void){u8 i,dat;dat=0;for(i=0;i<8;i++){dat<<=1;dat|=DHT11_Read_Bit();}return dat;}u8 DHT11_Read_Data(u8 *temp,u8 *humi){u8 buf[5];u8 i;DHT11_Rst();if(DHT11_Check()==0){for(i=0;i<5;i++){buf[i]=DHT11_Read_Byte();} //读取40位数据if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4]){*humi=buf[0];*temp=buf[2];} }else return 1;return 0;}u8 DHT11_Init(void){RCC->APB2ENR|=1<<2; //使能PORTA口时钟GPIOA->CRL&=0XFFFFFFF0; //PORTA.11 推挽输出GPIOA->CRL|=0X00000003;GPIOA->ODR|=1<<0; //输出1DHT11_Rst();return DHT11_Check();void OLED_Refresh_Gram(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(OLED_GRAM[n][i],OLED_DATA);} }}void OLED_WR_Byte(u8 dat,u8 cmd){u8 i;if(cmd){OLED_RS_Set();}else{OLED_RS_Clr();}for(i=0;i<8;i++){OLED_SCLK_Clr();if(dat&0x80){OLED_SDIN_Set();}else{OLED_SDIN_Clr();}OLED_SCLK_Set();dat<<=1;}OLED_RS_Set();}void OLED_Display_On(void){OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令OLED_WR_Byte(0X14,OLED_CMD); //DCDC ONOLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON}void OLED_Display_Off(void){OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFFOLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF}void OLED_Clear(void){u8 i,n;for(i=0;i<8;i++)for(n=0;n<128;n++)OLED_GRAM[n][i]=0X00;OLED_Refresh_Gram(); //更新显示void OLED_DrawPoint(u8 x,u8 y,u8 t){u8 pos,bx,temp=0;if(x>127||y>63)return; //超出范围了.pos=7-y/8;bx=y%8;temp=1<<(7-bx);if(t)OLED_GRAM[x][pos]|=temp;else OLED_GRAM[x][pos]&=~temp;}void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size,u8 mode){u8 temp,t,t1;u8 y0=y;chr=chr-' ';for(t=0;t<size;t++){if(size==12){temp=oled_asc2_1206[chr][t];} //调用1206字体 else{temp=oled_asc2_1608[chr][t];} //调用1608字体 for(t1=0;t1<8;t1++){if(temp&0x80){OLED_DrawPoint(x,y,mode);}else{OLED_DrawPoint(x,y,!mode);}temp<<=1;y++;if((y-y0)==size){y=y0;x++;break;}}}}u32 oled_pow(u8 m,u8 n){u32 result=1;while(n--)result*=m;return result;}void OLED_ShowNumber(u8 x,u8 y,u32 num,u8 len,u8 size){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+(size/2)*t,y,' ',size,1);continue;}else{enshow=1;}}OLED_ShowChar(x+(size/2)*t,y,temp+'0',size,1);}}void OLED_ShowString(u8 x,u8 y,const u8 *p){#define MAX_CHAR_POSX 122#define MAX_CHAR_POSY 58while(*p!='\0'){if(x>MAX_CHAR_POSX){x=0;y+=16;}if(y>MAX_CHAR_POSY){y=x=0;OLED_Clear();}OLED_ShowChar(x,y,*p,12,1);x+=8;p++;}}void Oled_Init(void){RCC->APB2ENR|=1<<3; //使能PORTB时钟GPIOB->CRL&=0XFFF0FFFF;GPIOB->CRL|=0X00020000; //PB4 推挽输出RCC->APB2ENR|=1<<4; //使能PORTC时钟RCC->APB2ENR|=1<<0; //使能AFIO时钟GPIOC->CRH&=0X000FFFFF; //PC13,14,15设置成输出 2MHz 推挽输出GPIOC->CRH|=0X22200000;PWR->CR|=1<<8; //取消备份区写保护RCC->BDCR&=0xFFFFFFFE; //外部低俗振荡器关闭 PC14,PC15成为普通IO BKP->CR&=0xFFFFFFFE; //侵入检测TAMPER引脚作为通用IO口使用PWR->CR&=0xFFFFFEFF; //备份区写保护OLED_RST_Clr();delay_ms(100);OLED_RST_Set();OLED_WR_Byte(0xAE,OLED_CMD); //关闭显示OLED_WR_Byte(0xD5,OLED_CMD); //设置时钟分频因子,震荡频率OLED_WR_Byte(80,OLED_CMD); //[3:0],分频因子;[7:4],震荡频率OLED_WR_Byte(0xA8,OLED_CMD); //设置驱动路数OLED_WR_Byte(0X3F,OLED_CMD); //默认0X3F(1/64)OLED_WR_Byte(0xD3,OLED_CMD); //设置显示偏移OLED_WR_Byte(0X00,OLED_CMD); //默认为0OLED_WR_Byte(0x40,OLED_CMD); //设置显示开始行 [5:0],行数OLED_WR_Byte(0x8D,OLED_CMD); //电荷泵设置OLED_WR_Byte(0x14,OLED_CMD); //bit2,开启/关闭OLED_WR_Byte(0x20,OLED_CMD); //设置内存地址模式OLED_WR_Byte(0x02,OLED_CMD);//[1:0],00,列地址模式;01,行地址模式;10,页地址模式;默认10;OLED_WR_Byte(0xA1,OLED_CMD); //段重定义设置,bit0:0,0->0;1,0->127;OLED_WR_Byte(0xC0,OLED_CMD);//设置COM扫描方向;bit3:0,普通模式;1,重定义模式 COM[N-1]->COM0;N:驱动路数OLED_WR_Byte(0xDA,OLED_CMD); //设置COM硬件引脚配置OLED_WR_Byte(0x12,OLED_CMD); //[5:4]配置OLED_WR_Byte(0x81,OLED_CMD); //对比度设置OLED_WR_Byte(0xEF,OLED_CMD); //1~255;默认0X7F (亮度设置,越大越亮)OLED_WR_Byte(0xD9,OLED_CMD); //设置预充电周期OLED_WR_Byte(0xf1,OLED_CMD); //[3:0],PHASE 1;[7:4],PHASE 2;OLED_WR_Byte(0xDB,OLED_CMD); //设置VCOMH 电压倍率OLED_WR_Byte(0x30,OLED_CMD);//[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;OLED_WR_Byte(0xA4,OLED_CMD);//全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏)OLED_WR_Byte(0xA6,OLED_CMD); //设置显示方式;bit0:1,反相显示;0,正常显示 OLED_WR_Byte(0xAF,OLED_CMD); //开启显示OLED_Clear();}void Oled_Show(void){OLED_ShowString(0,0,"****************");OLED_ShowString(22,20,"DHT11--TEST");OLED_ShowString(30,30,"T: 'C");OLED_ShowString(30,40,"H: %");OLED_ShowString(0,50,"****************");OLED_Refresh_Gram();}int main(void){u8 t;u8 temperature;u8 humidity;Stm32_Clock_Init(9); //系统时钟设置delay_init(72); //延时初始化JTAG_Set(JTAG_SWD_DISABLE); //关闭JTAG接口JTAG_Set(SWD_ENABLE); //打开SWD接口Oled_Init();Led_Init(); //LED初始化while(DHT11_Init()) //初始化DHT11{delay_ms(1000);LED_RED = !LED_RED;} //DS0闪烁Oled_Show();while(1){if(t%10==0){DHT11_Read_Data(&temperature,&humidity);。

温湿度传感器DHT11程序编写

温湿度传感器DHT11程序编写

/******************DHT11******************************** FileName:dht11.cAuthor:沧海麒麟Date:2011.05.29Version :1.0Description:适用于ATmega16(L)Function: 利用串口发送温度和湿度到电脑,同时用液晶显示*/#include <iom16v.h>#include <macros.h>#include <avrdef.h>#include "delay.c"#include "LCM1602.c"#define uchar unsigned char#define uint unsigned int#define DDR_1 DDRA|=BIT(PA0)#define DDR_0 DDRA&=~BIT(PA0)#define PORT_1 PORTA|=BIT(PA0)#define PORT_0 PORTA&=~BIT(PA0)#define MOSIN (PINA&0X01)uchar U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata; uchar DHT11_5DATA[5];uchar U8temp;uchar flag = 0;uchar humidity[] = "humidity :";uchar temperature[] = "temperature:";void COM(void){uchar i,j,m;for(i=0;i<5;i++){DHT11_5DATA[i]=0X00;for(j=0;j<8;j++){while(!MOSIN);//等待高电平的到来,当高电平到来后便开始计时Delay(60);//for(m=0;m<50;m++); //延时50us后看还是不是高电平,如果是高那么这位就为1,否则为0if(MOSIN){DHT11_5DATA[i] |= BIT(7-j);while(MOSIN);}//end of if}//end of for}//end of for}void DHT11(void){uchar i;DDR_1;PORT_0;Delay_Nms(20);//主机拉低18msPORT_1;//总线由上拉电阻拉高主机延时20usDelay(30);//for(i=0;i<23;i++); //此操作为微秒级的延时DDR_0;//主机设为输入判断从机响应信号Delay(15);//for(i=0;i<10;i++);//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行if(!MOSIN) //{while(!MOSIN);//判断从机是否发出 80us 的低电平响应信号是否结束while(MOSIN);//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态COM();//数据接收状态DDR_1 ;PORT_1 ;U8temp=(DHT11_5DATA[0]+DHT11_5DATA[1]+DHT11_5DATA[2]+DHT11_5DATA[3]);//数据校验if(DHT11_5DATA[4]==U8temp){U8RH_data_H = DHT11_5DATA[0];U8RH_data_L = DHT11_5DATA[1];U8T_data_H = DHT11_5DATA[2];U8T_data_L = DHT11_5DATA[3];U8checkdata = DHT11_5DATA[4];}}}/***********端口初始化函数**********************/void init_port(void){PORTD|= 0x02; //注意这两个管脚要配置为输出DDRD |= 0x02;}/************串口初始化函数*********************/void init_uart0(void){UCSRB = 0x00;UBRRH = 0x00;UBRRL = 0x67;UCSRA = (1 << U2X);//双倍率工作模式UCSRB = (1 << RXEN)|(1 << TXEN);//|(1 << UCSZ2);使能接收器和发射器 UCSRC = 0x80|(1 << UCSZ1)|(1 << UCSZ0);}/************发送一个字节函数********************/void Send_Uart0(uchar data){while(!(UCSRA&(1<<UDRE)));UDR=data;}/************T/C1初始化函数**********************/void Timer1_InterruptMode(void)//T/C1中断方式函数{//stop errant interrupts until set upCLI(); //disable all interruptsTCCR1B = 0x00; //stopTCNT1H = 0xCF; //setupTCNT1L = 0x2C;TCCR1B = 0x03; //start TimerMCUCR = 0x00;GICR = 0x00;TIMSK = 0x04; //timer interrupt sourcesSEI(); //re-enable interrupts//all peripherals are now initialized}/************总初始化函数**********************/void Init_Devices(void){Init_Port();Init_1602(0x18);init_port();init_uart0();Timer1_InterruptMode();}/************中断服务函数**********************/#pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF //9 void timer1_ovf_isr(void){//TIMER1 has overflowedTCNT1H = 0xCF; //reload counter high valueTCNT1L = 0x2C; //reload counter low valueflag++;}void main(void){Init_Devices();DisplayString(0,0,humidity);DisplayString(0,1,temperature);while(1){if(flag==10)//1s采集一次{ //调用温湿度读取子程序flag=0;CLI();DHT11();DisplayNumber(12,0,U8RH_data_H);//1602显示湿度Delay_Nms(10);DisplayNumber(12,1,U8T_data_H);//1602显示温度 Send_Uart0(U8RH_data_H);Send_Uart0(U8T_data_H);Send_Uart0(U8checkdata);Send_Uart0(U8temp);SEI();}}}。

单片机温度湿度控制程序(dht11)

单片机温度湿度控制程序(dht11)

1、温度湿度控制主程序(单片机)#include<reg52.h>#include "dht11.h"#define uchar unsigned char#define uint unsigned intsbit beep=P2^3;sbit k1=P3^2;sbit k2=P3^3;sbit k3=P3^6;sbit k4=P3^7;sbit rs=P3^5;sbit lcden=P3^4;uchar table0[]="T:00.00 TM:00.0 ";//初始化液晶温度实测值以及门限值缓存uchar table1[]="H:00.00 HM:00.0 ";//初始化液晶湿度实测值以及门限值缓存uchar table3[]="This is T/H test";//系统开机显示内容初始化缓存uchar table4[]="Result is coming";//系统开机显示内容初始化缓存static uchar flag;extern int temp_value,humi_value;void Delay_1ms(uint ms);uint TM,HM;void delay(uint x)//{uint a,b;for(a=x;a>0;a--)for(b=10;b>0;b--);}void delay1(uint x)//延时程序{uint a,b;for(a=x;a>0;a--)for(b=100;b>0;b--);}void write_com(uchar com)//液晶写指令操作{P0=com;rs=0;lcden=0;delay(10);lcden=1;delay(10);lcden=0;}void write_date(uchar date)//液晶写数据操作{P0=date;rs=1;lcden=0;delay(10);lcden=1;delay(10);lcden=0;}void init()//液晶初始化{write_com(0x38);delay(1);write_com(0x0c);delay(1);write_com(0x06);delay(1);write_com(0x01);delay(1);}void LCD_Display(uchar buff1[],uchar buff2[])//液晶显示程序{uchar aa;init();//液晶初始化write_com(0x80+16);//写入第一行数据指令delay(1);for(aa=0;aa<16;aa++)//写入第一行待显示数据{write_date(buff1[aa]);}write_com(0xc0+16); //写入第二行数据指令for(aa=0;aa<16;aa++)//写入第一行待显示数据{write_date(buff2[aa]);}for(aa=0;aa<16;aa++){write_com(0x18);delay(1);}}void Tem_Ch(uchar AA,uchar nn)//T门限数值替换{switch(AA){case 0:table0[nn]='0';break;case 1:table0[nn]='1';break;case 2:table0[nn]='2';break;case 3:table0[nn]='3';break;case 4:table0[nn]='4';break;case 5:table0[nn]='5';break;case 6:table0[nn]='6';break;case 7:table0[nn]='7';break;case 8:table0[nn]='8';break;case 9:table0[nn]='9';break;}}void Hum_Ch(uchar BB,uchar mm)//H门限数值替换{switch(BB){case 0:table1[mm]='0';break;case 1:table1[mm]='1';break;case 2:table1[mm]='2';break;case 3:table1[mm]='3';break;case 4:table1[mm]='4';break;case 5:table1[mm]='5';break;case 6:table1[mm]='6';break;case 7:table1[mm]='7';break;case 8:table1[mm]='8';break;case 9:table1[mm]='9';break;}}///////////void YZ_Resh()//门限值更新{uchar A1,A2,A3;A1=TM/10;//取十位A2=TM%10;//取个位A3=0;Tem_Ch(A1,11);//写入对应的液晶显示缓存Tem_Ch(A2,12);//写入对应的液晶显示缓存Tem_Ch(A3,14);//写入对应的液晶显示缓存A1=HM/10;A2=HM%10;A3=0;Hum_Ch(A1,11);Hum_Ch(A2,12);Hum_Ch(A3,14);}void key_test()//按键检测/*根据按键的检测结果进行温度湿度门限值的设定*/{bit kk1;bit kk2;bit kk3;bit kk4;uchar temp;k1=0;k2=1;k3=1;k4=1;kk1=k1;kk2=k2;kk3=k3;kk4=k4;if(kk3!=1){delay1(100);if(kk3!=1)//若可开=0且持续一段时间后任然为0,则执行有关操作{temp=P3;//按键一操作TM=TM+1;//提高温度门限值YZ_Resh();////更新液晶显示缓存的门限值LCD_Display(table0,table1);//液晶显示数值}}if(kk4!=1){delay1(100);if(kk4!=1){temp=P3;//按键二操作TM=TM-1;//降低温度门限值YZ_Resh();////更新液晶显示缓存的门限值LCD_Display(table0,table1);//液晶显示数值}}k1=1;k2=0;k3=1;k4=1;kk1=k1;kk2=k2;kk3=k3;kk4=k4;if(kk3!=1){delay1(100);if(kk3!=1){temp=P3;//按键三操作HM=HM+1;//提高湿度门限YZ_Resh();//更新液晶显示缓存的门限值LCD_Display(table0,table1);//液晶显示数值}}if(kk4!=1){delay1(100);if(kk4!=1){temp=P3;//按键四操作HM=HM-1;//降低湿度门限YZ_Resh();//更新液晶显示缓存的门限值LCD_Display(table0,table1);//液晶显示数值}}}void TempAndHumi_Resh()//更新液晶显示缓存中的温度湿度数值/*根据测得的结果将液晶显示缓存中的数据进行替换*/{uchar ge,shi,bai;ge=temp_value%10;shi=temp_value%100/10;bai=temp_value%1000/100;Tem_Ch(bai,2);Tem_Ch(shi,3);Tem_Ch(ge,5);ge=humi_value%10;shi=humi_value%100/10;bai=humi_value%1000/100;Hum_Ch(bai,2);Hum_Ch(shi,3);Hum_Ch(ge,5);}void beep_control()//警报状态控制函数/*如果实测温度超过门限值则报警*/{if(temp_value>(TM*10)){beep=0;}else{if(humi_value>(HM*10)){beep=0;}else{beep=1;}}}void main(){uint m=0;TM=25;//设定温度初始门限值:25°CHM=50;//设定湿度初始门限值:50%YZ_Resh();//更新液晶显示缓存的门限值beep=1;//关闭蜂鸣器LCD_Display(table3,table4);//显示系统本系统信息while(1){key_test();//监测按键状态,并根据对应按键改变温度湿度门限值,并显示if (m<100)//循环计时,3秒钟{m++;Delay_1ms(30);}else//当m=100时即为3秒时间到,则进行温度湿度数据采集{m=0;ReadTempAndHumi();//采集温度湿度数据TempAndHumi_Resh();//更新液晶显示缓存中的温度湿度数值LCD_Display(table0,table1);//显示当前温度湿度实测值以及门限值}beep_control();//根据门限值以及实测值进行警报状态设置}}#include <reg52.h>#include "dht11.h"#define uchar unsigned char#define uint unsigned int#define NUMBER 20#define SIZE 5sbit dht11=P2^2;static uchar status;static uchar value_array[SIZE];//定义存放DHT11采集的5个字节的数组int temp_value,humi_value;static uchar ReadValue(void);extern void Delay_1ms(uint ms)//毫秒延时,延时时间为ms毫秒{uint x,y;for(x=ms;x>0;x--){for(y=124;y>0;y--);}}static void Delay_10us(void)//10微秒延时{uchar i;i--;i--;i--;i--;i--;i--;}static uchar ReadValue(void)//读一个字节数据的程序{uchar count,value=0,i;status=OK;//设定标志位正常状态for(i=8;i>0;i--){value<<=1;//高位在前进行接收数据count=0;while(dht11==0&&count++<NUMBER);if(count>=NUMBER)//若大于最大数值则出错{status=ERROR;//设定错误标志return 0;//函数执行过程发生错误就退出函数}Delay_10us();//进行30us的延时Delay_10us();Delay_10us();if(dht11!=0)//再次检测是否为高电平{value++;//进入本函数说明信号为1,则进行自加while(dht11!=0&&count++<NUMBER){dht11=1;}if(count>=NUMBER){status=ERROR;return 0;}}}return(value);//返回读取的结果}extern uchar ReadTempAndHumi(void)//读取一次温度湿度数据,共5个字节{uchar i=0,check_value=0,count=0;EA=0;dht11=0;//拉低数据线20msDelay_1ms(20);dht11=1;//释放数据线,以用于检测低电平应答信号Delay_10us();//延时40usDelay_10us();Delay_10us();Delay_10us();if(dht11!=0)//检测应答信号,应答信号为低电平{EA=1;//无低电平。

基于单片机的温湿度传感器程序设计

基于单片机的温湿度传感器程序设计
2
1.性能参数
DHT11相关参数如表13-3所示。湿度测量范围中的RH表示相对湿度。
表13-3 DHT11参数表
外文名
DHT11
测量精度 湿度±5%RH(误差为5%),温度±2℃
供电电压
3.3~5.5V DC
分辨率
湿度1%RH,温度1℃输出Βιβλιοθήκη 单总线数字信号互换性
可完全互换
测量范围 湿度20~90%RH,温度0~50℃ 长期稳定性
9
【例13-2】 【参考代码】
//***********************************************************************//
//
DHT11使用范例
//
功能:串口发送温湿度数据
//***********************************************************************//
#include <reg51.h>
#include <intrins.h>
typedef unsigned char U8;
//无符号8位整型变量
typedef signed char S8;
//有符号8位整型变量
typedef unsigned int U16;
//无符号16位整型变量
typedef signed int S16;
<±1%RH/年
DHT11为4针单排引脚封装,引脚说明如表13-4所示。
引脚Pin 1 2 3 4
表13-4 DHT11引脚说明
名称
说明
VDD
电源3.3~5.5V DC

课程设计基于单片机的温湿度传感器

课程设计基于单片机的温湿度传感器

由于温度与湿度不管是从物理量本身还是在实际人们的生活中都有着密切的关系,所以温湿度一体的传感器就会相应产生。

DHT11与单片机之间能采用简单的单总线进行通信,仅仅需要一个I/O口。

操作简单,使用基亚5110二手屏幕显示DHT11传感器读出来的温度和湿度值。

关键字:AT89S52;5110液晶;DHT11传感器;单总线AbstractBecause of the temperature and the humidity from both the physical quantity itself or in the actual life of people are closely related, so the temperature and humidity sensor integrated will arise accordingly. Between DHT11 and SCM can adopt the simple single bus, only need a I\/O port. Simple operation, using the base of 5110 secondhand screen display DHT11 sensor Read out the temperature and humidity.Keywords: AT89S52; DHT11 5110 LCD; sensor; single bus引言 ------------------------------------------11.1 DHT11描述---------------------------------------------------21.2 管脚排列----------------------------------------------21.3 应用电路连接说明--------------------------------------21.4 DHT11数据结构-----------------------------------------31.5 DHT11的传输时序---------------------------------------31.5.1 DHT11开始发送数据流程--------------------------31.5.2 主机复位信号和DHT11响应信号 --------------------31.5.3 数字‘0’信号表示方法-----------------------------31.5.4 数字‘1’信号表示方法-----------------------------42、诺基亚5110液晶简介(PCD8544驱动)--------------------42.1 引脚---------------------------------------------------4 2.2 功能描述:---------------------------------------------52.2.1 地址计数器 (AC)--------------------------------52.2.2 初始化-------------------------------------------52.2.3复位的作用----------------------------------------62.2.4显示控制------------------------------------------62.2.5 串行接口时序--------------------------------------62.2.6 指令集 ------------------------------------------63、总结-------------------------------------------------74、谢辞-------------------------------------------------85、参考文献---------------------------------------------96、附录-------------------------------------------------105.1 实验总框架图---------------------------------------------10 5.2 硬件部分-------------------------------------------------105.2.1硬件原理图-------------------------------------------10 5.2.2 硬件PCB图-------------------------------------------11 5.2.3 所需元器件-------------------------------------------115.3 实验效果-------------------------------------------------11 5.4 实验软件程序---------------------------------------------12引言可靠性与卓越的长期稳定性。

温湿度DHT11 51程序

温湿度DHT11 51程序
U8FLAG=2;
while((!P2_0)&&U8FLAG++);
Delay_10us();
Delay_10us();
Delay_10us();
U8temp=0;
if(P2_0)U8temp=1;
U8FLAG=2;
//----------------------------------------------//
sbit P2_0 = P2^0 ;
//----------------------------------------------//
//----------------定义区--------------------//
//----校验 8位 == U8checkdata-----
//----调用相关子程序如下----------
//---- Delay();, Delay_10us();,COM();
//--------------------------------
void RH(void)
i--;
i--;
i--;
}
void COM(void)
{
U8 i;
for(i=0;i<8;i++)
{
U8 U8comdata;
U8 outdata[5]; //定义发送的字节数
U8 indata[5];
U8 count, count_r=0;
U8 str[5]={"RS232"};
U16 U16temp1,U16temp2;
SendData(U8 *a)

温湿度一体化传感器DHT11可用程序

温湿度一体化传感器DHT11可用程序

//****************************************************************** DHT11.h用于温湿度一体传感器DHT11的驱动程序。

单片机为STC89C52,晶振频率为11.0592MHz。

单线串口,记得不要接到P3口上,会出现未知问题。

时序要求很严格,建议详细参照DHT11的说明书来编程。

//******************************************************************* #include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit dht_dat=P1^0; //用哪个I/O口自选,注意不要用P3口uchar dht_t1=0,dht_t2=0; //依次为温度整数部分和温度小数部分uchar dht_d1=0,dht_d2=0; //依次为湿度整数部分和湿度小数部分uchar dht_chk=0; //和校验,可选择是否使用,具体参照数据手册uchar dht_num=0; //用于while循环中计数,超时则跳出循环#ifndef __INF_NEC__#define __INF_NEC__extern void dht_delay_10us();extern void dht_delay_10ms(uchar t);extern uchar dht_readat();extern void dht_getdat();extern void dht_init();#endifvoid dht_delay_10us() //自己调,一定要尽量精确到10us,很重要{uchar i=0;for(i=0;i<1;i++);}void dht_delay_10ms(uchar t) //大概10ms就行,粗略延时{uchar i=0,j=0,k=0;for(i=0;i<t;i++){for(j=0;j<40;j++)for(k=0;k<75;k++);}}uchar dht_readat() //接收一个8位数据,先高位后低位{uchar i=0,dat=0;for(i=0;i<8;i++){dht_num=2; // dht_num用于while循环中计数,超时则跳出循环while((dht_dat==0)&&(dht_num++)); //dht_dat 数据线由DH11拉低准备发送数据dht_delay_10us();dht_delay_10us();dht_delay_10us();dht_delay_10us(); //延时50usdat=dat<<1;if(dht_dat==1) //DH11拉高数据线发送数据(读一位数据){dht_num=2;dat=dat|0x01;while((dht_dat==1)&&(dht_num++)); //等待高电平}}return dat;}void dht_getdat() //给DHT11一个开始信号,然后读取一次数据,共五个8位字节{uchar i=0;dht_dat=0; //单片机拉低数据线dht_delay_10ms(4); //延时40msdht_dat=1; //单片机给起始脉冲信号dht_delay_10us();dht_delay_10us();dht_delay_10us();dht_delay_10us(); //延时40usdht_dat=1; //稍作延时,等待DHT11返回响应(响应为低电平)if(dht_dat==0) //有响应才接收数据,否则不作处理{dht_num=2;while((dht_dat==0)&&(dht_num++));dht_num=2;while((dht_dat==1)&&(dht_num++)); //dht_d1=dht_readat();dht_d2=dht_readat();dht_t1=dht_readat();dht_t2=dht_readat();dht_chk=dht_readat();//一次读出五个数据}dht_dat=1; //释放总线dht_delay_10ms(10); //稍作延时}void dht_init() //DHT11的初始化函数,别忘了写程序时先加上之{dht_delay_10ms(100); //DHT11上电前准备时间,大概1sdht_dat=1; //总线准备}说明:各位朋友请注意一点,如果用STC89C52系列单片机的话,用单线串口原件与之连接时注意不要接到P3的八个口上。

SHT11温湿度传感器在单片机中的应用

SHT11温湿度传感器在单片机中的应用

郑州航空工业管理学院《单片机原理与应用》课程设计说明书09 级电气工程及其自动化专业 72 班级题目I2C总线数字温湿度传感器SHT11及其在单片机系统的应用姓名李兴田学号090607215指导教师王义琴职称讲师二О一二年五月28 日一、SHT11温湿度传感器的基本原理SHT11的湿度检测运用电容式结构,并采用具有不同保护的“微型结构”检测电极系统与聚合物覆盖层来组成传感器芯片的电容,除保持电容式湿敏器件的原有特性外,还可抵御来自外界的影响。

由于它将温度传感器与湿度传感器结合在一起而构成了一个单一的个体,因而测量精度较高且可精确得出露点,同时不会产生由于温度与湿度传感器之间随温度梯度变化引起的误差。

CMOSensTM技术不仅将温湿度传感器结合在一起,而且还将信号放大器、模/数转换器、校准数据存储器、标准I2C总线等电路全部集成在一个芯片内。

SHT11传感器的内部结构框图如图2所示。

SHT11的每一个传感器都是在极为精确的湿度室中校准的。

SHT11传感器的校准系数预先存在OTP内存中。

经校准的相对湿度和温度传感器与一个14位的A/D转换器相连,可将转换后的数字温湿度值送给二线I2C总线器件,从而将数字信号转换为符合I2C总线协议的串行数字信号。

由于将传感器与电路部分结合在一起,因此,该传感器具有比其它类型的湿度传感器优越得多的性能。

首先是传感器信号强度的增加增强了传感器的抗干扰性能,保证了传感器的长期稳定性,而A/D转换的同时完成,则降低了传感器对干扰噪声的敏感程度。

其次在传感器芯片内装载的校准数据保证了每一只湿度传感器都具有相同的功能,即具有100%的互换性。

最后,传感器可直接通过I2C总线与任何类型的微处理器、微控制器系统连接,从而减少了接口电路的硬件成本,简化了接口方式。

二、设计方案3.4 系统设计流程图设计SHT11温湿度传感器在单片机中的应用的流程图如下:三、硬件设计图5 单片机与SHT11接口电路及温湿度显示电路(1)湿度值输出SHT11可通过I2C总线直接输出数字量湿度值,其相对湿度数字输出特性曲线如图3所示。

基于某某51单片机地温湿度DHT11采集

基于某某51单片机地温湿度DHT11采集

一、设计目的温湿度控制系统早已经成为当今社会研究的热门领域之一,同时温湿度也是现今工农业生产生活中必须要首先考虑的重要因素之一。

因此现今社会生产生活中就迫切需要一种价格便宜、容易操作而且精确度高的温湿度控制系统。

本系统设计就是利用AT89C52单片机对温湿度参量进行检测进而实现对温湿度参量的控制。

本系统设计通过使用AT89C52单片机、DHT11传感器模块、LCD1602液晶显示屏模块以及LED闪烁和蜂鸣器报警模块。

简单的系统设计就可以基本上满足本系统设计的系统设计要求。

DHT11数字温湿度传感器负责把采集到的温湿度信号传送给AT89C52单片机。

温湿度经过AT89C52单片机处理,然后AT89C52准确地把温湿度信号发送到LCD1602液晶显示屏模块,温湿度数据就可以准确地显示到LCD1602液晶屏上面,同时我们可以分别对温度和湿度设置上下限,越限LED闪烁和蜂鸣器报警模块。

二、设计要求和方案选择•LCD1602能够实时、准确的显示AT89C52采样温度值和湿度值,显示设定报警的温度和风扇的状态。

•DHT11采集温度及湿度值,AT89C52能够将DHT11输送过来的采集信息,准确的判断标准值与设定值之间的差异,及时的启动报警装置(LED闪烁和蜂鸣器)进行报警。

•根据采集温湿度的数据进行判断,控制继电器进行风扇的开闭传感器选择方案•方案一:利用两个传感器分别检测温度和湿度值,温度检测模块选用DS18B20温度传感器(+-0.5℃)。

湿度检测模块选用HS1101电容式湿度传感器(+-5%RH)。

这两款传感器都是我们大学实践电子设计中非常常见和常用的传感器。

•方案二:检测温湿度数值选用DHT11传感器作为系统设计的温湿度检测模块。

DHT11传感器能够同时对温度和湿度数值进行检测,而且其精确度能过完全满足本课程设计的精度要求。

湿度、温度测量误差:+-5%、+-2度•经过上述分析,方案一虽然精度更精确。

原创 C51 温湿度传感器DHT11驱动 LCD1602显示程序 硬件通过 含电路图和实际测试效果图

原创 C51 温湿度传感器DHT11驱动 LCD1602显示程序 硬件通过 含电路图和实际测试效果图

DHT11.c文件#include<reg52.h>#include<Time_Delay.h>//the main only needs to call getDHT11(),then the temperature and huminity was geted in F16T,F16RH as floatsbit bit11=P2^0;unsigned char U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;float F16T,F16RH; //用于最终读取的温湿度数据// read 8 bits onicechar COM(void){char i,U8temp,U8comdata;for(i=0;i<8;i++){while(!bit11); //表示读取的高电位延时大于20多us 则读取的是1 否则读取的是0Delay_us(35); //通过U8FLAG 可判断bit11 显示数据的脉长U8temp=0;if(bit11)U8temp=1;while(bit11);U8comdata<<=1;U8comdata|=U8temp; //0}//rofreturn U8comdata;}//--------------------------------//-----温湿度读取子程序------------//--------------------------------//----以下变量均为全局变量--------//----温度高8位== U8T_data_H------//----温度低8位== U8T_data_L------//----湿度高8位== U8RH_data_H-----//----湿度低8位== U8RH_data_L-----//----校验8位== U8checkdata-----//----调用相关子程序如下----------//---- Delay();, Delay_10us();,COM();//--------------------------------void getDHT11(void){//主机拉低18msGO1: bit11=0;Delay_ms(20);bit11=1;//总线由上拉电阻拉高主机延时20usDelay_us(60);//主机设为输入判断从机响应信号// bit11=1;//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行if(!bit11) //T !{while(!bit11); //wait DHT goto highwhile(bit11);//数据接收状态U8RH_data_H=COM();U8RH_data_L=COM();U8T_data_H=COM();U8T_data_L=COM();U8checkdata=COM();bit11=1;//数据校验if((U8T_data_H+U8T_data_L+U8RH_data_H+U8RH_data_L)!=U8checkdata) //if check wrong,read againgoto GO1;}//fiF16T=U8T_data_H+(float)U8T_data_L/256; //change integer to floatF16RH=U8RH_data_H+(float)U8RH_data_L/256;}LCD1602文件#include<reg52.h>#include <stdio.h>#include <INTRINS.H>#include <Lcd_1602.h>#include <Time_Delay.h>#define LCD_DATA P0 //LCD1602 data transfer define#define uint unsigned int#define uchar unsigned char/*只由主函数调用的有Init_Lcd()LCD_write_str(uchar X,uchar Y,uchar *s)LCD_value(unsigned char x,unsigned char y,float f)*/sbit LCD_RS = P2^5; //1602 control definesbit RW = P2^6;sbit LCD_E = P2^7;/*************************************************************************** #define LCD_SCREEN_ON 0x0C //显示开#define LCD_SCREEN_OFF 0x08 //显示关#define LCD_CURSOR_ON 0x0A //显示光标#define LCD_CURSOR_OFF 0x0c //无光标#define LCD_C_FLASH_ON 0x0f //有光标,光标闪动#define LCD_C_FLASH_OFF 0x0e //有光标,光标不闪动//进入模式设置指令#define LCD_AC_UP 0x06 //新数据后光标右移#define LCD_AC_DOWN 0x04 //新数据后光标左移#define LCD_S_MOVE_ON 0x05 // 画面可平移#define LCD_S_MOVE_OFF 0x04 //画面不可平移//设定显示屏或光标移动方向指令#define LCD_C_LEFT 0x10 //光标左移1格,且AC值减1#define LCD_C_RIGHT 0x11 //光标右移1格,且AC值加1#define LCD_CHAR_LEFT 0x18 //显示器上字符全部左移一格,但光标不动#define LCD_CHAR_RIGHT 0x1C //显示器上字符全部右移一格,但光标不动***************************************************************************/ //注有主函数调用的函数都已作说明其他函数一般不由主函数调用/**************************************************************************** * 名称:Init_Lcd() 主函数调用* 功能:Lcd初始化* 入口参数:无* 出口参数:无* 范例: 在主函数中直接调用****************************************************************************/void Init_Lcd() //LCD初始化{LCD_write_char(0x38,0);Delay_ms(1);LCD_write_char(0x38,0);Delay_ms(1);LCD_write_char(0x38,0);Delay_ms(1);LCD_write_char(0x0c,0);Delay_ms(1);LCD_write_char(0x06,0);Delay_ms(1);LCD_write_char(0x0c,0);Delay_ms(1);//}/**************************************************************************** * 名称:LCD_write_str(uchar X,uchar Y,uchar *s)主函数调用* 功能:在指定地址写一个字符串eg:Y=0,1,2,3,4,5,6,7,8,9,10...15。

DHT11温湿度测量电路图及程序

DHT11温湿度测量电路图及程序
wela=0;
PO=Oxff;
PO=table[j/1O];
dula=1;
dula=0;
delay(1);
P0=0xff;
P0=wei[i+1];
wela=1;
wela=0;
P0=0xff;
P0=table[j%10];
dula=1;
dula=0;
delay(1);
}
void mai n()
{
while(1)
}
retur n data_byte;
}
void receive()
uchar T_H,T_L,R_H,R_L,check, num _check;
uchar count;
start();〃开始信号
io=1;
if(!io)〃读取DHT11响应信号
{Байду номын сангаас
coun t=2;
while((!io)&&count++);//DHT11高电平80us是否结束
LLAU
五门
「1H
91啊
N工
frl91
口U
丁】初
廿]利
□□A
0
i
■<■1
II
G
H
ll
J
n
L

J
n
9
tod
0
fi
g
p
91
t

J
u
r
IfM
q
fir
c
IJ
6(
i
CtNU
9H—
j
(IND
UK
□L
□9
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

基于89C51单片机SHT11温湿度传感器电路图于程序作者:志杰SHT11.h文件:#ifndef __SHT11_H__#define __SHT11_H__/*************************SHT11相关命令**************************/#define uchar unsigned char#define uint unsigned int#define TEM_TEST 0x03//温度检测命令#define HUM_TEST 0x05//湿度检测命令#define REG_READ 0x07//读寄存器#define REG_WRITE 0x06//写寄存器#define FUNCTION_SET 0x01//设置SHT11的工作精度为8位/湿度12位温度/**************************SHT11端口定义***************************/sbit SHT11_DATA=P3^1;sbit SHT11_SCK=P3^0;sbit P33=P3^3;sbit P32=P3^2;sbit P36=P3^6;sbit P37=P3^7;uchar flag_tempeture=0; //显示温度位置的标志uchar flag_humidity=0; //显示湿度位置的标志uchar code str1[]={ 0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00};//温度图标uchar code str6_sht11[]="%RH ";uchar code str4_sht11[]="humi=";uchar code str2_sht11[]="temp=";uchar code str7_sht11[]=" ";//清除没不要的显示/***************************函数名称:Delay()函数功能:SHT11部延时****************************/void Delay(){;;}/***************************函数名称:Delay_Ms()函数功能:SHT11检测等待延时函数说明:11ms/55ms/210ms 分别对应8位/12位/14位测量结果对应的形参为N 则延时Nms****************************/void Delay_Ms(uint ms) // ms延时函数(AT89C51 11.0592MHz){uint i;uchar j;for(i=0;i<ms;i++){for(j=0;j<200;j++);for(j=0;j<102;j++);}}/**************************函数功能:SHT11启动时序***************************/void SHT11_Start(){SHT11_SCK=1;SHT11_DATA=1;Delay();SHT11_DATA=0;Delay();SHT11_SCK=0;Delay();SHT11_SCK=1;Delay();SHT11_DATA=1;}/******************************函数名称:SHT11_Sendbyte(uchar dat) 函数功能: 向SHT11发送8bite数据******************************/void SHT11_Sendbyte(uchar dat){uchar i;SHT11_SCK=0;Delay();for(i=0;i<8;i++){if(dat&0x80){SHT11_DATA=1;Delay();}else{SHT11_DATA=0;Delay();}dat=dat<<1;SHT11_SCK=1;Delay();SHT11_SCK=0;}}/*********************************函数名称SHT11_Answer():函数功能:检测SHT11的响应信号(在第九个时钟周期)***********************************/void SHT11_Answer(){SHT11_SCK=1;Delay();while(SHT11_DATA==1);SHT11_SCK=0;SHT11_DATA=1;}/************************************函数名称:SHT11_T est_Finish()函数功能:检测SHT11温湿度检测是否完毕*************************************/void SHT11_T est_Finish(){while(SHT11_DATA==1);}/************************************函数名称:SHT11_Receivebyte()函数功能:从SHT11接收8bite数据*************************************/uchar SHT11_Receivebyte(){uchar i;uchar dat;SHT11_SCK=0;Delay();for(i=0;i<8;i++){SHT11_SCK=1;Delay();dat=dat<<1;if(SHT11_DATA){dat=dat|0x01;Delay();}else{dat=dat&0xfe;Delay();}SHT11_SCK=0;Delay();}SHT11_DATA=1; //释放数据总线return(dat);}/***********************************函数名称:MCU_Answer()函数功能:单片机向SHT11发送应答信号*************************************/void MCU_Answer(){SHT11_SCK=0;Delay();SHT11_DATA=0;Delay();SHT11_SCK=1;Delay();SHT11_SCK=0;Delay();SHT11_DATA=1; //释放数据总线这条指令非常重要不加的话导致单片机不能读取低8位}/***********************************函数名称:SHT11_End()当接收两个8byte数据后部接收CRC校验码************************************/void SHT11_End(){SHT11_DATA=1;SHT11_SCK=1;Delay();SHT11_SCK=0;Delay();}/*************************************函数名称:void SHT11_Write_Register(uchar command ,uchar dat)函数说明:向SHT11的状态寄存器设置功能command为REG_WRITE 0x06写寄存器dat为设置SHT11的功能可以设置检测的数据位数*/void SHT11_Write_Register(uchar command ,uchar dat){SHT11_Start();SHT11_Sendbyte(command);SHT11_Answer();SHT11_Sendbyte(dat);SHT11_Answer();}/***************************************函数名称:uchar SHT11_Read_Register(uchar command)函数说明:command为REG_READ 0x07//读寄存器返回值为状态寄存器的值位6显示当前检测完一次数据后电源供电情况当位6为0时表明VDD>2.47V 当位6为1时表明VDD<2.47V即电量不足位0表明当前的测量分辨率当位0为1时表明测量精度:8位/湿度12位温度当位0为0时表明测量精度:12位湿度14位温度默认为0*******************************************/uchar SHT11_Read_Register(uchar command){uchar dat;SHT11_Start();SHT11_Sendbyte(command);SHT11_Answer();dat=SHT11_Receivebyte();SHT11_End();return(dat);}/***************************************函数名称:SHT11_Measure(uchar command,uint time);函数功能:设置SHT11检测功能,并返回相应的检测结果函数说明:command形参用于设定温度检测还是湿度检测,time形参用于设定检测过程中的等待时间,以确定检测结果的位数11ms/55ms/210ms 分别对应8位/12位/14位****************************************/uint SHT11_Measure(uchar command,uchar time){uint dat=0;uchar data_high,data_low;SHT11_Start();SHT11_Sendbyte(command);SHT11_Answer();Delay_Ms(time);SHT11_T est_Finish();data_high=SHT11_Receivebyte();MCU_Answer();data_low=SHT11_Receivebyte();SHT11_End();dat=(dat|data_high);dat=(dat<<8)|data_low;return(dat);}/****************************************函数名称:Convert_T empeture12bit(uint dat); 函数功能:将检测到的数据转化为相应的温度数据函数说明:温度转换公式--T=d1+d2*SOt公式中的参数d1=-40,d2=0.04适用于12位测量精度*/float SHT11_Convert_T empeture12bit(uint dat) {float tempeture1;tempeture1=-40+0.04*dat;if(tempeture1>23)tempeture1=tempeture1+1;if(tempeture1>55)tempeture1=tempeture1+1;if(P37==1){if(tempeture1>=16&&tempeture1<30){P33=1;P32=0;}else{P33=0;P32=1;}}return(tempeture1);}/*****************************************函数名称:SHT11_Convert_Humidity8bit(uint dat,float temp)函数功能:将检测到的数据转化为相应的湿度数据函数说明:相对湿度转换公式-----RHline=C1+C2*SOrh+C3*SOrh*SOrh(检测数据的线性化SOrh为单片机接收到的数据)-----RHtrue=(tempeture-25)*(t1+t2*SOrh)+RHline公式中的参数:C1=-4,C2=0,648,C3=-0.00072t1=0.01,t2=0.00128适用于8位测量精度*/uint SHT11_Convert_Humidity8bit(uint dat,float temp){float RHline,RHtrue;uint r;RHline=-4+0.648*dat-0.00072*dat*dat;RHtrue=(temp-25)*(0.01+0.00128*dat)+RHline;r=(RHtrue-3)*10+0.5;if(P37==0){if(r>=400&&r<600){P33=1;}else{P33=0;}if(r>=600){P32=1;}else{P32=0;}}return(r);}#endifDisplay.c文件:#include <reg52.h>#include "SHT11.h"#define uchar unsigned char#define uint unsigned int#define TEM_TEST 0x03//温度检测命令#define HUM_TEST 0x05//湿度检测命令#define REG_READ 0x07//读寄存器#define REG_WRITE 0x06//写寄存器#define FUNCTION_SET 0x01//设置SHT11的工作精度为8位/湿度12位温度uchar DispData[4] = {0, 1, 2, 3};code uchar DispSegmentP0[10]={0x3f,0x06,0x1b,0x0f,0x26,0x2d,0x3d,0x07,0x3f,0x2f}; code uchar DispSegmentP2[10]={0x00,0x00,0x22,0x22,0x22,0x22,0x22,0x00,0x22,0x22};// 0 1 2 3 4 5 6 7 8 9 % code uchar DispCtrl[4] = {0xef, 0xdF, 0xbf ,0x7F};sbit P16=P1^6;sbit P26=P2^6;sbit P34=P3^4;void T emp_delay(unsigned int j){uchar i;for(i=100;i>0;i--){for(j;j>0;j--);}}void Show(uchar *Buffer){uchar i;for (i=0; i<4; i++){P1 = DispCtrl[i];P0 = DispSegmentP0[*Buffer];P2 = DispSegmentP2[*Buffer];if(P16==0)P26=1;T emp_delay(2);Buffer++;}}void DispConvert(uchar *DispAddr, uint T emp2Con){DispAddr[0] = T emp2Con/1000;DispAddr[1] = (T emp2Con - DispAddr[0]*1000)/100;DispAddr[2] = (T emp2Con - DispAddr[0]*1000 - DispAddr[1]*100)/10;DispAddr[3] = T emp2Con - DispAddr[0]*1000 - DispAddr[1]*100 - DispAddr[2]*10;}void main(){uint temp;uint dat;uint Hum;//float f;while(1){Show(DispData);SHT11_Write_Register(REG_WRITE,FUNCTION_SET);temp=SHT11_Measure(TEM_TEST,0x37);temp=SHT11_Convert_T empeture12bit(temp);dat=SHT11_Measure(HUM_TEST,0x0b);Hum=SHT11_Convert_Humidity8bit(dat,temp);if(P34==0){P36=1;P37=0;DispConvert(DispData,Hum);}else{P36=0;P37=1;DispConvert(DispData,temp*10);}}}还有些地方不是很完美。

相关文档
最新文档