30
30
*/
31
31
static const uint16_t DEVICE_ADDRESS = (0x38 << 1 );
32
32
33
+ /*
34
+ * performs soft resetting of the sensor
35
+ *
36
+ * Datasheet: AHT20 Product manuals
37
+ * 5.5 Soft reset
38
+ */
39
+ static uint8_t SOFT_RESET_CMD = 0xba ;
40
+
33
41
/*
34
42
* get status command. is needed for sending to device after power on
35
43
*
36
44
* Datasheet: AHT20 Product manuals
37
45
* 5.4 Sensor reading process, paragraph 1
38
46
*/
39
- static uint8_t GET_STATUS = 0x71 ;
47
+ static uint8_t GET_STATUS_CMD = 0x71 ;
40
48
41
49
/*
42
50
* array for calibration initialization
@@ -54,6 +62,9 @@ static uint8_t INIT_CMD[3] = {0xbe, 0x08, 0x00};
54
62
*/
55
63
static uint8_t MEASURE_CMD [3 ] = {0xac , 0x33 , 0x00 };
56
64
65
+ /*
66
+ * calculates crc8 for given data
67
+ */
57
68
static uint8_t calculate_crc (uint8_t * data );
58
69
59
70
/*
@@ -63,7 +74,7 @@ static uint8_t calculate_crc(uint8_t *data);
63
74
* 5.3 Send command
64
75
*/
65
76
aht20_status_t aht20_get_calibration_status (I2C_HandleTypeDef * hi2c , UART_HandleTypeDef * huart , uint8_t * status_word , uint16_t status_word_size ) {
66
- if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , & GET_STATUS , (uint16_t )sizeof (GET_STATUS ), HAL_MAX_DELAY )) {
77
+ if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , & GET_STATUS_CMD , (uint16_t )sizeof (GET_STATUS_CMD ), HAL_MAX_DELAY )) {
67
78
return AHT20_STATUS_NOT_TRANSMITTED ;
68
79
}
69
80
@@ -95,7 +106,7 @@ aht20_status_t aht20_check_calibration(uint8_t status_word) {
95
106
* 5.4 Sensor reading process, paragraph 1
96
107
*/
97
108
aht20_status_t aht20_calibrate (I2C_HandleTypeDef * hi2c , uint8_t status_word ) {
98
- if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , INIT_CMD , 3 , HAL_MAX_DELAY )) {
109
+ if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , INIT_CMD , ( uint16_t ) sizeof ( INIT_CMD ) , HAL_MAX_DELAY )) {
99
110
return AHT20_STATUS_NOT_TRANSMITTED ;
100
111
}
101
112
@@ -111,43 +122,76 @@ aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word) {
111
122
aht20_status_t aht20_measure (I2C_HandleTypeDef * hi2c , uint8_t * measured_data ) {
112
123
uint8_t received_crc = 0 ;
113
124
114
- if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , MEASURE_CMD , 3 , HAL_MAX_DELAY )) {
125
+ if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , MEASURE_CMD , ( uint16_t ) sizeof ( MEASURE_CMD ) , HAL_MAX_DELAY )) {
115
126
return AHT20_STATUS_NOT_TRANSMITTED ;
116
127
}
117
128
HAL_Delay (80 );
118
129
119
130
uint8_t measuring_status = 0 ;
120
- HAL_I2C_Master_Receive (hi2c , DEVICE_ADDRESS , & measuring_status , 1 , HAL_MAX_DELAY );
131
+ HAL_I2C_Master_Receive (hi2c , DEVICE_ADDRESS , & measuring_status , ( uint16_t ) sizeof ( measuring_status ) , HAL_MAX_DELAY );
121
132
122
133
uint8_t all_data [7 ];
123
134
if (measuring_status & (1 << 7 )) {
124
135
return AHT20_STATUS_NOT_MEASURED ;
125
136
} else {
126
- HAL_I2C_Master_Receive (hi2c , DEVICE_ADDRESS , all_data , 7 , HAL_MAX_DELAY );
137
+ HAL_I2C_Master_Receive (hi2c , DEVICE_ADDRESS , all_data , ( uint16_t ) sizeof ( all_data ) , HAL_MAX_DELAY );
127
138
}
128
139
129
140
// Copy 6 data bytes to measured_data
130
- for (uint8_t i = 0 ; i < 6 ; i ++ ) {
141
+ for (uint8_t i = 0 ; i < 6 ; ++ i ) {
131
142
measured_data [i ] = all_data [i ];
132
143
}
133
144
received_crc = all_data [6 ]; // CRC is the 7th byte
134
145
135
146
uint8_t calculated_crc = calculate_crc (measured_data );
136
147
if (calculated_crc == received_crc ) {
137
148
uint8_t ack = 0x06 ;
138
- if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , & ack , 1 , HAL_MAX_DELAY )) {
149
+ if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , & ack , ( uint16_t ) sizeof ( ack ) , HAL_MAX_DELAY )) {
139
150
return AHT20_STATUS_NOT_TRANSMITTED ;
140
151
}
141
152
} else {
142
153
uint8_t nack = 0x15 ;
143
- if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , & nack , 1 , HAL_MAX_DELAY )) {
154
+ if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , & nack , ( uint16_t ) sizeof ( nack ) , HAL_MAX_DELAY )) {
144
155
return AHT20_STATUS_NOT_TRANSMITTED ;
145
156
}
157
+
158
+ aht20_soft_reset (hi2c );
146
159
}
147
160
148
161
return AHT20_STATUS_OK ;
149
162
}
150
163
164
+ /*
165
+ * calculates measured_data and writes the calculation in provided variables
166
+ *
167
+ * Datasheet: AHT20 Product manuals
168
+ * 6.1 Relative humidity transformation
169
+ * 6.2 Temperature transformation
170
+ */
171
+ void aht20_calculate_measurments (uint8_t * measured_data , float * humidity , float * temp_c , float * temp_f ) {
172
+ uint32_t raw_humidity = ((measured_data [1 ] << 12 ) | (measured_data [2 ] << 4 ) | (measured_data [3 ] >> 4 ));
173
+ uint32_t raw_temperature = (((measured_data [3 ] & 0x0F ) << 16 ) | (measured_data [4 ] << 8 ) | measured_data [5 ]);
174
+
175
+ * humidity = ((float )raw_humidity * 100.0 ) / 1048576.0 ; /* 2^20 */
176
+ * temp_c = (((float )raw_temperature * 200.0 ) / 1048576.0 ) - 50.0 ;
177
+ * temp_f = * temp_c * 9.0 / 5.0 + 32.0 ;
178
+ }
179
+
180
+ /*
181
+ * resets the sensor without turning off the power supply
182
+ *
183
+ * Datasheet: AHT20 Product manuals
184
+ * 5.5 Soft reset
185
+ */
186
+ aht20_status_t aht20_soft_reset (I2C_HandleTypeDef * hi2c ) {
187
+ if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , & SOFT_RESET_CMD , (uint16_t )sizeof (SOFT_RESET_CMD ), HAL_MAX_DELAY )) {
188
+ return AHT20_STATUS_NOT_TRANSMITTED ;
189
+ }
190
+
191
+ HAL_Delay (20 );
192
+ return AHT20_STATUS_OK ;
193
+ }
194
+
151
195
/*
152
196
* calculates crc8 for given data
153
197
*/
@@ -169,18 +213,4 @@ static uint8_t calculate_crc(uint8_t *data) {
169
213
return crc ;
170
214
}
171
215
172
- /*
173
- * calculates measured_data and writes the calculation in provided variables
174
- *
175
- * Datasheet: AHT20 Product manuals
176
- * 6.1 Relative humidity transformation
177
- * 6.2 Temperature transformation
178
- */
179
- void aht20_calculate_measurments (uint8_t * measured_data , float * humidity , float * temp_c , float * temp_f ) {
180
- uint32_t raw_humidity = ((measured_data [1 ] << 12 ) | (measured_data [2 ] << 4 ) | (measured_data [3 ] >> 4 ));
181
- uint32_t raw_temperature = (((measured_data [3 ] & 0x0F ) << 16 ) | (measured_data [4 ] << 8 ) | measured_data [5 ]);
182
216
183
- * humidity = ((float )raw_humidity * 100.0 ) / 1048576.0 ; /* 2^20 */
184
- * temp_c = (((float )raw_temperature * 200.0 ) / 1048576.0 ) - 50.0 ;
185
- * temp_f = * temp_c * 9.0 / 5.0 + 32.0 ;
186
- }
0 commit comments