-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsht21.c
66 lines (49 loc) · 1.8 KB
/
sht21.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Driver for SHT21.
* David Hladky
* 2016
* Version 1.0 Update 14-7-2016
*/
#include "sht21.h"
void sht21_soft_reset(void)
{
uint8_t commnad = SHT21_SOFT_RESET;
HAL_I2C_Master_Transmit(&I2C1_struct,I2C_dev_add,&commnad,1,1000); // Send command to the SHT21.
HAL_Delay(15);
}
uint8_t sht21_read_user_reg(void)
{
uint8_t user_reg, commnad = SHT21_READ_USER_REGISTER;
HAL_I2C_Master_Transmit(&I2C1_struct,I2C_dev_add,&commnad,1,1000); // Send command to the SHT21.
HAL_Delay(55);
HAL_I2C_Master_Receive(&I2C1_struct,I2C_dev_add,user_reg,1,3000);
return user_reg;
}
void sht21_write_user_reg(uint8_t data)
{
uint8_t data_to_sht21[2];
data_to_sht21[0] = SHT21_WRITE_USER_REGISTER;
data_to_sht21[1] = data;
HAL_I2C_Master_Transmit(&I2C1_struct,I2C_dev_add,data_to_sht21,2,1000);
}
float sht21_get_temperature(void)
{
uint8_t data_from_sht21[3], commnad = SHT21_T_MEASURE_NO_MASTER;
float temperature, temp_data;
HAL_I2C_Master_Transmit(&I2C1_struct,I2C_dev_add,&commnad,1,1000); // Send command to the SHT21.
HAL_Delay(50);
HAL_I2C_Master_Receive(&I2C1_struct,I2C_dev_add,data_from_sht21,3,3000); // Read data from SHT21.
temp_data = ((data_from_sht21[0]<<8)|data_from_sht21[1]) & 0xFFFC;
temperature = -46.85 + 175.12*((float)((temp_data/65535)));
return temperature;
}
float sht21_get_humidity(void)
{
uint8_t data_from_sht21[3], commnad = SHT21_H_MEASURE_NO_MASTER;
float humidity,temp_data;
HAL_I2C_Master_Transmit(&I2C1_struct,I2C_dev_add,&commnad,1,1000); // Send command to the SHT21.
HAL_Delay(20);
HAL_I2C_Master_Receive(&I2C1_struct,I2C_dev_add,data_from_sht21,3,1000); // Read data from SHT21.
temp_data = ((data_from_sht21[0]<<8)|data_from_sht21[1]) & 0xFFFC;
humidity = -6 + 125*((temp_data/65535));
return humidity;
}