湿度和温度传感器SHT40驱动,C51
SHT40_READ_ID();
SHT40_Read_T_H();
sht40.c
- #define SHT40_C
- #include "includes.h"
- #include <stdarg.h>
- xdata u8 SHTC3_Buf[6]={0,0,0,0,0,0};
- xdata float Temp_Value=0;
- xdata float Humi_Value=0;
- xdata u16 Temp_x10=0;
- xdata u16 Humi_x10=0;
- // ******
- void SHTC3_I2C_Ack(void)
- {
- SDA_L();
- SCL_L();
- SCL_PIN_out();
- SDA_PIN_out();
- IIC_delay();
- SCL_H();
- IIC_delay();
- SCL_L();
- }
- /*
- // ******
- void SHTC3_Read(void)
- {
- Delay_nms(50); // 等待SHTC3测量完成,SCL恢复高
- IIC_start();
- IIC_W_B(SHTC3_READ_CMD);
- IIC_respons();
- Delay_nms(50); // 等待SHTC3测量完成,SCL恢复高
- SHTC3_Buf[0]=IIC_R_B();
- SHTC3_I2C_Ack();
- SHTC3_Buf[1]=IIC_R_B();
- SHTC3_I2C_Ack();
- SHTC3_Buf[2]=IIC_R_B();
- SHTC3_I2C_Ack();
- SHTC3_Buf[3]=IIC_R_B();
- SHTC3_I2C_Ack();
- SHTC3_Buf[4]=IIC_R_B();
- SHTC3_I2C_Ack();
- SHTC3_Buf[5]=IIC_R_B();
- SHTC3_I2C_Ack();
- IIC_stop();
- IIC_release();
- }
- // ******
- void SHTC3_W_CMD(u16 CMD)
- {
- u8 CMD_H,CMD_L;
- CMD_H=(u8)(CMD>>8);
- CMD_L=(u8)(CMD&0x00FF);
- IIC_start();
- IIC_W_B(SHTC3_WIRTE_CMD);
- IIC_respons();
- IIC_W_B(CMD_H);
- IIC_respons();
- IIC_W_B(CMD_L);
- IIC_respons();
- IIC_stop();
- IIC_release();
- }
- // ******
- void SHTC3_WAKE_UP(void)
- {
- SHTC3_W_CMD(SHTC3_WAKE_UP_CMD);
- Delay_nms(5);
- }
- // ******
- void SHTC3_Sleep(void)
- {
- SHTC3_W_CMD(SHTC3_SLEEP_CMD);
- Delay_nms(5);
- }
- // ******
- u16 SHTC3_READ_ID(void)
- {
- SHTC3_W_CMD(SHTC3_READ_ID_CMD);
- SHTC3_Read();
- Delay_nms(5);
- }
- // ******
- void SHTC3_Init(void)
- {
- SHTC3_WAKE_UP();
- SHTC3_W_CMD(SHTC3_SOFT_RESET_CMD);
- Delay_nms(50);
- // SHTC3_WAKE_UP();
- // SHTC3_READ_ID();
- SHTC3_Read_T_H();
- }
- */
- // *******************************************************************************
- // * 名称: crc8_compute
- // * 功能: 计算8位CRC校验和
- // * 形参: check_data -> 需要校验的数据,num_of_data -> 需要校验的字节数
- // * 返回: 检验和
- // * 说明: 无
- // ******************************************************************************
- int crc8_compute(u8 *check_data,u8 num_of_data)
- {
- u8 i; //位掩码
- u8 crc=0xFF; //0xFF是根据手册设置的初始值
- u8 byteCtr; //字节计数
- //用给定的多项式计算8位检验和
- for(byteCtr=0;byteCtr<num_of_data;byteCtr++)
- {
- crc ^= (check_data[byteCtr]);
- //crc校验,如果最高位是1,则^0x31
- for(i=8;i>0;--i)
- {
- if(crc&0x80)
- {
- crc=(crc<<1)^0x31;
- }
- else
- {
- crc=(crc<<1);
- }
- }
- }
- return crc;
- }
- // *******************************************************************************
- // * 名称: shtc3_crc8_check
- // * 功能: 检查校验和
- // * 形参: check_data -> 校验的数据,num_of_data -> 校验字节数,CrcData -> 对比的校验和
- // * 返回: 0:校验和正确,1:校验和错误
- // * 说明: 无
- // ******************************************************************************
- int shtc3_crc8_check(u8 *p,u8 num_of_data,u8 CrcData)
- {
- u8 crc;
- crc=crc8_compute(p,num_of_data); //计算8位校验和
- if(crc!=CrcData)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- /*
- // ******
- void SHTC3_Read_T_H(void)
- {
- u8 crc_result;
- u16 Humi_RdData;
- u16 Temp_RdData;
-
- SHTC3_WAKE_UP();
- // SHTC3_W_CMD(SHTC3_NOR_READ_TEMP_FIR_EN_CMD);
- SHTC3_W_CMD(SHTC3_NOR_READ_HUM_FIR_EN_CMD);
- // SHTC3_W_CMD(SHTC3_NOR_READ_HUM_FIR_DIS_CMD);
- // SHTC3_W_CMD(SHTC3_NOR_READ_TEMP_FIR_DIS_CMD);
- Delay_nms(5);
- SHTC3_Read();
- Delay_nms(5);
- SHTC3_Sleep();
- // ***
- b_Sensor_Error=0;
- crc_result=shtc3_crc8_check(SHTC3_Buf,2,SHTC3_Buf[2]);
- if(crc_result==0)
- {
- Humi_RdData=(SHTC3_Buf[0]<<8)+SHTC3_Buf[1];
- Humi_Value=(float)((100.0*Humi_RdData)/65535.0);
- // printf("Humi_RdData: %d ",Humi_RdData);
- // printf("Humi:%.1f ",Humi_Value);
- }
- else
- {
- b_Sensor_Error=1;
- // printf("Humi CRC Error\r\n");
- }
-
- crc_result=shtc3_crc8_check(&SHTC3_Buf[3],2,SHTC3_Buf[5]);
- if(crc_result==0)
- {
- Temp_RdData=(SHTC3_Buf[3]<<8)+SHTC3_Buf[4];
- Temp_Value=(float)(((175.0*Temp_RdData)/65535.0)-45.0);
- // Temp_Value=(float)(((175.0*Temp_RdData)/65535.0)-50.0); // Test
- // printf("Temp_RdData: %d ",Temp_RdData);
- // printf("Temp:%.1f\r\n",Temp_Value);
- }
- else
- {
- b_Sensor_Error=1;
- // printf("Temp CRC Error\r\n");
- }
- }
- */
- // ******
- void SHT40_Soft_Reset(void)
- {
- IIC_start();
- IIC_W_B(SHT40_WIRTE_CMD);
- IIC_respons();
- IIC_W_B(0x94);
- IIC_respons();
- IIC_stop();
- IIC_release();
- Delay_nms(2);
- }
- // ******
- void SHT40_CMD(u8 CMD)
- {
- IIC_start();
- IIC_W_B(SHT40_WIRTE_CMD);
- IIC_respons();
- IIC_W_B(CMD);
- IIC_respons();
- IIC_stop();
- IIC_release();
- Delay_nms(2);
- }
- // ******
- void SHT40_Read(void)
- {
- Delay_nms(100); // 等待SHTC3测量完成,SCL恢复高
- IIC_start();
- IIC_W_B(SHT40_READ_CMD);
- IIC_respons();
- Delay_nms(100); // 等待SHTC3测量完成,SCL恢复高
- SHTC3_Buf[0]=IIC_R_B();
- SHTC3_I2C_Ack();
- SHTC3_Buf[1]=IIC_R_B();
- SHTC3_I2C_Ack();
- SHTC3_Buf[2]=IIC_R_B();
- SHTC3_I2C_Ack();
- SHTC3_Buf[3]=IIC_R_B();
- SHTC3_I2C_Ack();
- SHTC3_Buf[4]=IIC_R_B();
- SHTC3_I2C_Ack();
- SHTC3_Buf[5]=IIC_R_B();
- SHTC3_I2C_Ack();
- IIC_stop();
- IIC_release();
- }
- void SHT40_READ_ID(void)
- {
- SHT40_Soft_Reset();
- SHT40_CMD(0x89);
- SHT40_Read();
- }
- // ******
- void SHT40_Read_T_H(void)
- {
- u8 crc_result;
- idata u16 Humi_RdData;
- idata u16 Temp_RdData;
-
- SHT40_Soft_Reset();
- SHT40_CMD(0xFD);
- SHT40_Read();
- // ***
- b_Sensor_T_Error=0;
- crc_result=shtc3_crc8_check(&SHTC3_Buf[0],2,SHTC3_Buf[2]);
- if(crc_result==0)
- {
- Temp_RdData=(SHTC3_Buf[0]<<8)+SHTC3_Buf[1];
- Temp_Value=(float)(((175.0*Temp_RdData)/65535)-45);
- // printf("Temp_RdData: %d ",Temp_RdData);
- // printf("Temp:%.1f\r\n",Temp_Value);
-
- Humi_RdData=(SHTC3_Buf[3]<<8)+SHTC3_Buf[4];
- Humi_Value=(float)(((125.0*Humi_RdData)/65535)-6);
- if(Humi_Value>100)
- {Humi_Value=100;}
- if(Humi_Value<0)
- {Humi_Value=0;}
- }
- else
- {
- b_Sensor_T_Error=1;
- // printf("Temp CRC Error\r\n");
- }
- /*
- b_Sensor_H_Error=0;
- crc_result=shtc3_crc8_check(SHTC3_Buf[3],2,SHTC3_Buf[5]);
- if(crc_result==0)
- {
- Humi_RdData=(SHTC3_Buf[3]<<8)+SHTC3_Buf[4];
- Humi_Value=(float)(((125.0*Humi_RdData)/65535)-6);
- // printf("Humi_RdData: %d ",Humi_RdData);
- // printf("Humi:%.1f ",Humi_Value);
- }
- else
- {
- b_Sensor_H_Error=1;
- // printf("Humi CRC Error\r\n");
- }
- */
- }
- /*
- IIC_IO_Init();
- // ******
- SHTC3_Init();
- SHTC3_Read_T_H();
- */
复制代码
sht40.h
- #ifndef SHT40_H
- #define SHT40_H
- #include "includes.h"
- /*
- // SHTC3 地址
- #define SHTC3_ADDRESS 0x70
-
- //SHTC3 命令
- #define SHTC3_WIRTE_CMD ((SHTC3_ADDRESS<<1)&0xFE)
- #define SHTC3_READ_CMD ((SHTC3_ADDRESS<<1)|0x01)
-
- #define SHTC3_SOFT_RESET_CMD 0x805D //软件复位命令
- #define SHTC3_READ_ID_CMD 0xEFC8 //读取SHTC3 ID命令
- #define SHTC3_WAKE_UP_CMD 0x3517 //将芯片从睡眠模式唤醒命令
- #define SHTC3_SLEEP_CMD 0xB098 //使芯片进入休眠命令
-
- //Normal Mode Clock Stretching Enable Measurement Command
- #define SHTC3_NOR_READ_TEMP_FIR_EN_CMD 0x7CA2//
- #define SHTC3_NOR_READ_HUM_FIR_EN_CMD 0x5C24//
-
- //Normal Mode Clock Stretching Disable Measurement Command
- #define SHTC3_NOR_READ_TEMP_FIR_DIS_CMD 0x7866//
- #define SHTC3_NOR_READ_HUM_FIR_DIS_CMD 0x58E0//
-
- //Low Power Mode Clock Stretching Enable Measurement Command
- #define SHTC3_LOW_READ_TEMP_FIR_EN_CMD 0x6458//
- #define SHTC3_LOW_READ_HUM_FIR_EN_CMD 0x44DE//
-
- //Low Power Mode Clock Stretching Disable Measurement Command
- #define SHTC3_LOW_READ_TEMP_FIR_DIS_CMD 0x609C//
- #define SHTC3_LOW_READ_HUM_FIR_DIS_CMD 0x401A//
- */
- // SHT40_A 地址
- #define SHT40_A_ADDRESS 0x44
- #define SHT40_B_ADDRESS 0x45
- // SHT40 命令
- #define SHT40_WIRTE_CMD ((SHT40_A_ADDRESS<<1)&0xFE)
- #define SHT40_READ_CMD ((SHT40_A_ADDRESS<<1)|0x01)
- // ******
- extern xdata u8 SHTC3_Buf[];
- extern xdata float Temp_Value;
- extern xdata float Humi_Value;
- extern xdata u16 Temp_x10;
- extern xdata u16 Humi_x10;
- #ifdef SHT40_C
- /*
- void SHTC3_I2C_Ack(void);
- void SHTC3_Read(void);
- void SHTC3_ReadW_CMD(u16 CMD);
- void SHTC3_WAKE_UP(void);
- void SHTC3_Sleep(void);
- u16 SHTC3_READ_ID(void);
- void SHTC3_Init(void);
- void SHTC3_Read_T_H(void);
- */
- void SHT40_Read(void);
- void SHT40_READ_ID(void);
- void SHT40_Read_T_H(void);
- #else
- /*
- extern void SHTC3_I2C_Ack(void);
- extern void SHTC3_Read(void);
- extern void SHTC3_W_CMD(u16 CMD);
- extern void SHTC3_Sleep(void);
- extern void SHTC3_WAKE_UP(void);
- extern u16 SHTC3_READ_ID(void);
- extern void SHTC3_Init(void);
- extern void SHTC3_Read_T_H(void);
- */
- extern void SHT40_Read(void);
- extern void SHT40_READ_ID(void);
- extern void SHT40_Read_T_H(void);
- #endif
- #endif
复制代码
|