登录
|
立即注册
|
使用QQ帐号登录
论坛
>
51单片机
发帖
|
SHT31温湿度传感器的51单片机程序(SH79F1615中颖芯片)
看8004
|
回11
|
收藏
楼主
ID:273876
只看他
2018-1-9 16:57
SHT31温湿度传感器+SH79F1615中颖的芯片
主程序:
代码:
/*所有包含的头文件*/
#include <SH79F1615.H>
#include <intrins.h>
#include "myiic.h"
#include "sht30.h"
//==========================================================================//
/**************************************************************************
* 函数名: main
* 函数功能:主函数
* 输入参数: 无
* 返回参数: 无
* 备注:
***************************************************************************/
void main(void)
{
IIC_Init();
while(1)
{
Read_sht30();
Convert_sht30();
delay_ms(5000);
}
}
sht30.c单片机源程序如下:
代码:
#include "sht30.h"
#include "myiic.h"
unsigned char BUFt[2]={0};
unsigned char BUFh[2]={0};
unsigned int hum_value=0;
unsigned int hum_sum=0;
unsigned char count=0;
/*unsigned char crc8(char *buf)
{
unsigned char i,j,crc;
crc=0;
for(j=1;j<7;j++)
{
crc=crc^(*buf);
for(i = 8; i > 0; i--)
{
if(crc & 0x80)
{
crc = (crc<< 1)^0x31;
}
else
{
crc = crc<< 1;
}
}
buf++;
}
return crc;
}*/
unsigned char Get_Crc8(unsigned char *ptr,unsigned int len)
{
unsigned char crc;
unsigned char i;
crc=0;
while(len--)
{
crc^=*ptr++;
for(i=0;i<8;i++)
{
if(crc&0x01)crc=(crc>>1)^0x8C;
else crc >>= 1;
}
}
return crc;
}
void Cmd_Write_sht30(unsigned char msb,unsigned char lsb)
{
IIC_Start(); //起始信号
IIC_Send_Byte(0x88+0); //发送设备地址+写信号
while(IIC_Wait_Ack()); //从机应答
IIC_Send_Byte(msb); //发送命令
while(IIC_Wait_Ack());
IIC_Send_Byte(lsb); //发送命令
while(IIC_Wait_Ack());
IIC_Stop(); //发送停止信号
delay_ms(5);
}
unsigned char crc1=0;
unsigned char crc2=0;
unsigned char tem ;
unsigned char hum ;
void Read_sht30(void)
{
unsigned char a=0;
Cmd_Write_sht30(0x20,0x32); //数据采集频率1 m/s
delay_ms(5000);
IIC_Start(); //起始信号
IIC_Send_Byte(0x88+1); //发送设备地址+读信号
while(IIC_Wait_Ack());
BUFt[0]=IIC_Read_Byte(1); //发送ACK
BUFt[1]=IIC_Read_Byte(1); //发送ACK
a=IIC_Read_Byte(1); //
BUFh[0]=IIC_Read_Byte(1); //发送ACK
BUFh[1]=IIC_Read_Byte(1); //发送ACK
IIC_Stop(); //发送停止信号
delay_ms(5);
}
//数据转换
void Convert_sht30(void)
{
tem=((unsigned long)175*(BUFt[0]*256+BUFt[1]))/65535-45; //摄氏度
hum=((unsigned long)100*(BUFh[0]*256+BUFh[1]))/65535;
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
全部资料下载地址:
SHT30(51代码).zip
(112.91 KB)
(下载次数: 280, 2018-1-9 16:56 上传)
下载积分: 黑币 -5
沙发
ID:273619
只看他
2018-1-9 18:14
厉害了6666水一波
板凳
ID:185362
只看他
2018-4-27 19:20
感谢楼主分享
地板
ID:306946
只看他
2018-4-28 08:43
谢谢,回去试试
5
#
ID:394151
只看他
2018-9-5 17:12
非常好,非常好
6
#
ID:435455
只看他
2018-11-29 16:38
您好,谢谢楼主分享,请教一下,SHT30的地址我看资料里写的是0X44,0X45,资料我也看的很懵,明明低位是读写地址,官方资料里说成0x44和0X45两个地址,但是看您的程序里地址是0x88,不知道这个地址到底是多少呢?
7
#
ID:393699
只看他
2018-12-11 21:33
刚接触这模块,不知道为啥总是没数据
8
#
ID:151749
只看他
2019-1-30 00:40
谢谢提供
9
#
ID:405257
只看他
2019-10-7 16:17
谢谢楼主分享,下载来看看
10
#
ID:378085
只看他
2020-1-19 09:39
工作正好接触到 SHT31 学习一下,谢谢楼主分享
11
#
ID:706721
只看他
2020-3-11 22:52
这是sht30的代码,sht31通用嘛?
51黑电子论坛
Powered by
Discuz!
X3.1
首页
|
标准版
|
触屏版
|
电脑版