SHT30是盛世瑞出品的低端低價溫濕度傳感器,利用I2C進行數據傳輸,具有兩個可選地址,寬電源電壓從2.4V到5.5V。
SHT30芯片有八個引腳:
Pin | Name | Comments |
1 | SDA | 數據引腳,輸入/輸出 |
2 | ADDR | 地址引腳,輸入 |
3 | ALENT | 報警引腳,輸出;不使用時懸空 |
4 | SCL | 時鍾引腳,輸入/輸出 |
5 | VDD | 電源引腳,輸入 |
6 | nRESET | 復位引腳,低電平有效,輸入 |
7 | R | 無用引腳,接地 |
8 | VSS | 接地引腳 |
重復性影響SHT30的測量時間,因此就是影響傳感器的能量消耗。
通過改變選擇ADDR引腳的電壓,實現傳感器的地址改變。默認地址是ADDR連接VSS時( 0X44),可以修改成0X45。(關於引腳,詳情請看數據手冊)。
SHT30有兩中工作模式
1.單次數據采集模式
通過不同命令,在重復性(高,中,低)和時鍾延伸(禁止,使能)方面不同,每個數據包含一個16位溫度和一個16位濕度值(按此順序)組成。
傳輸如下所示(具體傳輸過程以數據手冊為准):
(1)STM32發出START,並發出I2C的地址(7位)+讀(1)/寫(0)標志位,然后等待SHT30的ACK
(2)發出命令高8位數據,等待SHT30的ACK
(3)發出命令低8位數據,等待SHT30的ACK
(4)STM32發送STOP停止通信。
(5)STM32發出START,並發出I2C的地址(7位)+讀(1)/寫(0)標志位,然后等待SHT30的ACK
(6)傳感器將接收讀頭的接收,並發送兩個字節的數據(溫度),接着是一個字節的CRC校驗另外
(7)傳感器發送兩個字節的數據(相對濕度),然后是一個字節的CRC校驗和。
每發送8位必須接受到STM32回應的ACK條件傳感器才會繼續發送數據。如果傳感器在任何字節的數據之后沒有接收到ACK,它就不會繼續發送數據。I2C主機可以中止讀取傳輸任何數據字節后,發送NACK狀態,如果不能在隨后的數據感興趣,例如CRC字節或第二測量結果,可以了節省時間。
有時鍾延伸:
如果沒有數據發送,傳感器會發出一個ACK,並隨后拉下SCL線。SCL線被拖到測量完成為止。一旦測量完成,傳感器釋放SCL線並發送測量結果。
無時鍾延伸:
沒有數據存在,傳感器響發出一個NACK。
2.周期性數據采集模式
通過不同命令,在重復性(高,中,低)和數據采集頻率((0.5, 1, 2, 4 & 10 mps )方面不同,每個數據包含一個16位溫度和一個16位濕度值(按此順序)組成。
測量數據的傳輸可以通過獲取數據命令來啟動。如果沒有測量數據是I2C讀取頭報以一個NACK。在讀出命令獲取數據之后,數據存儲器被清除,即沒有測量數據存在。
代碼如下(是我根據SHT30提供的代碼修改):
//功能:開始I2C通信,並發送I2C地址和寫標志 static etError SHT3X_StartWriteAccess(void) { etError error; // error code // write a start condition //I2c_StartCondition(); IIC_Start();//I2C開始通信需要的條件 // write the sensor I2C address with the write flag IIC_Send_Byte(_i2cAddress << 1); error = (etError)IIC_Wait_Ack(); return error; } //----------------------------------------------------------------------------- static etError SHT3X_StartReadAccess(void) { etError error; // error code // write a start condition //I2c_StartCondition(); IIC_Start(); // write the sensor I2C address with the read flag IIC_Send_Byte(_i2cAddress << 1 | 0x01); error =(etError) IIC_Wait_Ack(); //error = I2c_WriteByte(_i2cAddress << 1 | 0x01); return error; } //----------------------------------------------------------------------------- static void SHT3X_StopAccess(void) { // write a stop condition //I2c_StopCondition(); IIC_Stop(); } //----------------------------------------------------------------------------- static etError SHT3X_WriteCommand(etCommands command) { etError error; // error code // write the upper 8 bits of the command to the sensor IIC_Send_Byte(command >> 8); error = (etError) IIC_Wait_Ack(); // write the lower 8 bits of the command to the sensor IIC_Send_Byte(command & 0xFF); error |= IIC_Wait_Ack(); //error |= IIC_Send_Byte(command & 0xFF); return error; } //----------------------------------------------------------------------------- static etError SHT3X_Read2BytesAndCrc(u16t* data, etI2cAck finaleAckNack,u8t timeout) { etError error; // error code u8t bytes[2]; // read data array u8t checksum; // checksum byte // read two data bytes and one checksum byte bytes[0] = IIC_Read_Byte(ACK); bytes[1] = IIC_Read_Byte(ACK); checksum = IIC_Read_Byte(finaleAckNack); // verify checksum error = SHT3X_CheckCrc(bytes, 2, checksum); // combine the two bytes to a 16-bit value *data = (bytes[0] << 8) | bytes[1]; return error; } //----------------------------------------------------------------------------- static etError SHT3X_Write2BytesAndCrc(u16t data) { etError error; // error code u8t bytes[2]; // read data array u8t checksum; // checksum byte bytes[0] = data >> 8; bytes[1] = data & 0xFF; checksum = SHT3X_CalcCrc(bytes, 2); // write two data bytes and one checksum byte IIC_Send_Byte(bytes[0]); error = (etError)IIC_Wait_Ack();// write data MSB if(error == NO_ERROR) { IIC_Send_Byte(bytes[1]); // write data LSB error = (etError)IIC_Wait_Ack(); } if(error == NO_ERROR) { IIC_Send_Byte(checksum); // write data LSB error =(etError) IIC_Wait_Ack(); // write checksum } return error; }