首页 » 技术SOS » 嵌入式系统 » MCU和嵌入式微处理器 » 求一份以51单片机为主控芯片的SHT温湿度传感器的程序?

求一份以51单片机为主控芯片的SHT温湿度传感器的程序?

菜鸟
2014-11-04 00:14:46     悬赏5分
求一份以51单片机为主控芯片的SHT温湿度传感器的程序?是SHT11温湿度传感器的控制程序,另加上传感器数据在液晶上可显示的更好。
关键词: sht11温湿度传感器, 液晶显示  

最佳回复

院士
2014-11-05 14:26:15
1楼

#include #include /******************************************************** 宏定义 ********************************************************/ #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 0x1e enum {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;x0;x--) for(y=125;y0;y--); } /******************************************************** 50us延时函数 ********************************************************/ void delay_50us(uint t) { uint j; for(;t0;t--) for(j=19;j0;j--); } /******************************************************** 50ms延时函数 ********************************************************/ void delay_50ms(uint t) { uint j; for(;t0;t--) for(j=6245;j0;j--); } /******************************************************** 12864液晶写指令 ********************************************************/ void write_12864com(uchar com) { lcdrs=0; lcdrw=0; delay_50us(1); P0=com; 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;i0;i=1) //高位为1,循环右移 { if (i&value) DATA=1; //和要发送的数相与,结果为发送的位 else DATA=0; SCK=1; _nop_();_nop_();_nop_(); //延时3us SCK=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;i0;i=1) //高位为1,循环右移 { SCK=1; if(DATA) val=(val|i); //读一位数据线的值 SCK=0; } DATA=!ack; //如果是校验,读取完后结束通讯 www.it165.net ; SCK=1; _nop_();_nop_();_nop_(); //延时3us SCK=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;i100)rh_true=100; //湿度最大修正 if(rh_true

工程师
2014-11-05 14:19:47
2楼

百度下应该就有哇