!21 再次解决SHT30规范扫描问题
* 再次解决规范扫描问题 * 解决sht30 规范扫描问题 * 解决规范扫描问题 * 更新SHT30温湿度传感器例程
This commit is contained in:
parent
08b0a0096b
commit
2d12ef6985
|
@ -19,20 +19,155 @@
|
|||
#include "cmsis_os2.h"
|
||||
#include "iot_i2c.h"
|
||||
#include "iot_errno.h"
|
||||
#include "iot_gpio.h"
|
||||
#include "hi_i2c.h"
|
||||
|
||||
#define ADC_TASK_STACK_SIZE 4096
|
||||
#define ADC_TASK_PRIO 25
|
||||
#define SHT30_TASK_STACK_SIZE 4096
|
||||
#define SHT30_TASK_PRIO 25
|
||||
|
||||
static void i2cSht30Task(void)
|
||||
#define SHT30_ADDR 0x44 //0x44接地,0x45接电源
|
||||
#define WriteHeader (SHT30_ADDR<<1) //地址+写命令
|
||||
#define ReadHeader (WriteHeader|0X01) //地址+读命令
|
||||
|
||||
#define CMD_READ_SERIALNBR 0x3780 // read serial number
|
||||
#define CMD_MEAS_CLOCKSTR_H 0x2C06 // meas. clock stretching, high rep.
|
||||
|
||||
// CRC
|
||||
#define POLYNOMIAL 0x131 // P(x) = x^8 + x^5 + x^4 + 1 = 100110001
|
||||
static unsigned char SHT3XCalcCrc(unsigned char data[], unsigned char nbrOfBytes)
|
||||
{
|
||||
unsigned char bit = 0; // bit mask
|
||||
unsigned char crc = 0xFF; // calculated checksum
|
||||
unsigned char byteCtr = 0; // byte counter
|
||||
|
||||
// calculates 8-Bit checksum with given polynomial
|
||||
for(byteCtr = 0; byteCtr < nbrOfBytes; byteCtr++)
|
||||
{
|
||||
crc ^= (data[byteCtr]);
|
||||
for(bit = 8; bit > 0; --bit)
|
||||
{
|
||||
if(crc & 0x80)
|
||||
{
|
||||
crc = (crc << 1) ^ POLYNOMIAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
crc = (crc << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
//温度转换
|
||||
float SHT3XCalcTemperature(unsigned char *buf)
|
||||
{
|
||||
unsigned short rawValue = 0;
|
||||
|
||||
rawValue = (((unsigned int)buf[0]) << 8) | ((unsigned int)buf[1]);
|
||||
return 175 * (float)rawValue / 65535 - 45;
|
||||
}
|
||||
|
||||
//湿度转换
|
||||
float SHT3XCalcHumidity(unsigned char *buf)
|
||||
{
|
||||
unsigned short rawValue = 0;
|
||||
|
||||
rawValue = (((unsigned short)buf[0]) << 8) | ((unsigned short)buf[1]);
|
||||
return 100 * (float)rawValue / 65535;
|
||||
}
|
||||
|
||||
//SHT3X周期性模式读取数据函数,一次读取4byte
|
||||
static int SHT3xPeriodicReadData(unsigned short cmd, unsigned char *pbuf)
|
||||
{
|
||||
int ret = 0;
|
||||
unsigned char temp_crc = 0, hum_crc = 0;
|
||||
unsigned char buf[6] = {0};
|
||||
|
||||
buf[0] = (unsigned char)(cmd >> 8)&0XFF;
|
||||
buf[1] = (unsigned char)(cmd >> 0)&0XFF;
|
||||
ret = IoTI2cWrite(HI_I2C_IDX_0, WriteHeader, buf, 2);
|
||||
if(ret != IOT_SUCCESS)
|
||||
{
|
||||
printf("I2C write data fail, ret = %d\r\n",ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
usleep(20000);
|
||||
ret = IoTI2cRead(HI_I2C_IDX_0, ReadHeader, buf, 6);
|
||||
if(ret != IOT_SUCCESS)
|
||||
{
|
||||
printf("I2C read data fail, ret = %d\r\n",ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//crc校验读取到的数据
|
||||
temp_crc = buf[2];
|
||||
hum_crc = buf[5];
|
||||
if((temp_crc != SHT3XCalcCrc(buf,2)) || (hum_crc != SHT3XCalcCrc(&buf[3],2)))
|
||||
{
|
||||
printf("I2C read data crc check fail\r\n");
|
||||
return IOT_FAILURE;
|
||||
}
|
||||
|
||||
pbuf[0] = buf[0];
|
||||
pbuf[1] = buf[1];
|
||||
pbuf[2] = buf[3];
|
||||
pbuf[3] = buf[4];
|
||||
|
||||
return IOT_SUCCESS;
|
||||
}
|
||||
|
||||
//读取芯片序列号
|
||||
static void SHT3xReadSerialNumber(unsigned int* serialNbr)
|
||||
{
|
||||
unsigned char number_buf[4] = {0};
|
||||
|
||||
SHT3xPeriodicReadData(CMD_READ_SERIALNBR,number_buf);
|
||||
*serialNbr = (((unsigned int)number_buf[0]) << 24) + (((unsigned int)number_buf[1]) << 16)+\
|
||||
(((unsigned int)number_buf[2]) << 8) + ((unsigned int)number_buf[3]);
|
||||
}
|
||||
|
||||
static void I2cSht30Task(void)
|
||||
{
|
||||
int ret = 0;
|
||||
unsigned int serialNumber = 0;
|
||||
unsigned char read_buf[4] = {0};
|
||||
float temp = 0, hum = 0;
|
||||
|
||||
IoTGpioInit(IOT_GPIO_IO_GPIO_13);
|
||||
IoTGpioInit(IOT_GPIO_IO_GPIO_14);
|
||||
|
||||
IotIoSetPull(IOT_GPIO_IO_GPIO_13, IOT_IO_PULL_UP);
|
||||
IotIoSetPull(IOT_GPIO_IO_GPIO_14, IOT_IO_PULL_UP);
|
||||
|
||||
IotIoSetFunc(IOT_GPIO_IO_GPIO_13, HI_IO_FUNC_GPIO_13_I2C0_SDA);
|
||||
IotIoSetFunc(IOT_GPIO_IO_GPIO_14, HI_IO_FUNC_GPIO_14_I2C0_SCL);
|
||||
|
||||
ret = IoTI2cInit(HI_I2C_IDX_0, 400000);
|
||||
if(ret != IOT_SUCCESS)
|
||||
{
|
||||
printf("I2c Init failed, ret = %d\r\n",ret);
|
||||
}
|
||||
|
||||
SHT3xReadSerialNumber(&serialNumber);
|
||||
printf("sht30 serialNumber = %08X\r\n", serialNumber);
|
||||
|
||||
for(;;)
|
||||
{
|
||||
if(SHT3xPeriodicReadData(CMD_MEAS_CLOCKSTR_H,read_buf) == IOT_SUCCESS)
|
||||
{
|
||||
temp = SHT3XCalcTemperature(&read_buf[0]);
|
||||
hum = SHT3XCalcHumidity(&read_buf[2]);
|
||||
}
|
||||
|
||||
usleep(1000000);
|
||||
printf("sht30->> temp = %.02f, hum = %.01f\r\n",temp, hum);
|
||||
usleep(2000000);
|
||||
}
|
||||
}
|
||||
|
||||
static void i2cSht30Entry(void)
|
||||
static void I2cSht30Entry(void)
|
||||
{
|
||||
osThreadAttr_t attr;
|
||||
attr.name = "uartTask";
|
||||
|
@ -40,13 +175,14 @@ static void i2cSht30Entry(void)
|
|||
attr.cb_mem = NULL;
|
||||
attr.cb_size = 0U;
|
||||
attr.stack_mem = NULL;
|
||||
attr.stack_size = ADC_TASK_STACK_SIZE;
|
||||
attr.priority = ADC_TASK_PRIO;
|
||||
attr.stack_size = SHT30_TASK_STACK_SIZE;
|
||||
attr.priority = SHT30_TASK_PRIO;
|
||||
|
||||
if (osThreadNew((osThreadFunc_t)i2cSht30Task, NULL, &attr) == NULL) {
|
||||
printf("[i2cSht30Example] Falied to create i2cSht30Task!\n");
|
||||
if (osThreadNew((osThreadFunc_t)I2cSht30Task, NULL, &attr) == NULL)
|
||||
{
|
||||
printf("[i2cSht30Example] Falied to create I2cSht30Task!\n");
|
||||
}
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
APP_FEATURE_INIT(i2cSht30Entry);
|
||||
SYS_RUN(I2cSht30Entry);
|
||||
|
|
Loading…
Reference in New Issue