Skip to content

Commit b40bcaf

Browse files
committed
improved reading from sensor and fixed a few bugs
1 parent 5445fa1 commit b40bcaf

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

Core/Inc/AHT20.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word);
6161
* Datasheet: AHT20 Product manuals
6262
* 5.4 Sensor reading process, paragraph 2
6363
*/
64-
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data);
64+
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data, uint16_t measured_data_size);
6565

6666
/*
6767
* resets the sensor without turning off the power supply

Core/Src/AHT20.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,32 +119,22 @@ aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word) {
119119
* Datasheet: AHT20 Product manuals
120120
* 5.4 Sensor reading process, paragraph 2
121121
*/
122-
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data) {
123-
uint8_t received_crc = 0;
124-
122+
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data, uint16_t measured_data_size) {
125123
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, MEASURE_CMD, (uint16_t)sizeof(MEASURE_CMD), HAL_MAX_DELAY)) {
126124
return AHT20_STATUS_NOT_TRANSMITTED;
127125
}
128126
HAL_Delay(80);
129127

130-
uint8_t measuring_status = 0;
131-
HAL_I2C_Master_Receive(hi2c, DEVICE_ADDRESS, &measuring_status, (uint16_t)sizeof(measuring_status), HAL_MAX_DELAY);
132-
133-
uint8_t all_data[7];
134-
if (measuring_status & (1 << 7)) {
135-
return AHT20_STATUS_NOT_MEASURED;
136-
} else {
137-
HAL_I2C_Master_Receive(hi2c, DEVICE_ADDRESS, all_data, (uint16_t)sizeof(all_data), HAL_MAX_DELAY);
128+
if (HAL_OK != HAL_I2C_Master_Receive(hi2c, DEVICE_ADDRESS, measured_data, measured_data_size, HAL_MAX_DELAY)) {
129+
return AHT20_STATUS_NOT_RECEIVED;
138130
}
139131

140-
// Copy 6 data bytes to measured_data
141-
for (uint8_t i = 0; i < 6; ++i) {
142-
measured_data[i] = all_data[i];
132+
if (measured_data[0] & (1 << 7)) {
133+
return AHT20_STATUS_NOT_MEASURED;
143134
}
144-
received_crc = all_data[6]; // CRC is the 7th byte
145135

146-
uint8_t calculated_crc = calculate_crc(measured_data);
147-
if (calculated_crc == received_crc) {
136+
uint8_t calculated_crc = calculate_crc(measured_data + 1);
137+
if (calculated_crc == measured_data[0]) {
148138
uint8_t ack = 0x06;
149139
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, &ack, (uint16_t)sizeof(ack), HAL_MAX_DELAY)) {
150140
return AHT20_STATUS_NOT_TRANSMITTED;

Core/Src/main.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void print_error(aht20_status_t status) {
8989
/* doing nothing if ok */
9090
}
9191
else {
92-
printf(debug_msg, "Unknown error\r\n");
92+
sprintf(debug_msg, "Unknown error\r\n");
9393
UART_Send_String(debug_msg);
9494
}
9595
}
@@ -167,18 +167,19 @@ int main(void)
167167

168168
/* Infinite loop */
169169
/* USER CODE BEGIN WHILE */
170-
uint8_t measured_data[6] = {0};
170+
uint8_t measured_data[7] = {0};
171171
float humidity = 0.0;
172172
float temperature_c = 0.0;
173173
float temperature_f = 0.0;
174174

175175
while (1)
176176
{
177177
/* triggering measuring */
178-
status = aht20_measure(&hi2c1, measured_data);
178+
status = aht20_measure(&hi2c1, measured_data, (uint16_t)sizeof(measured_data));
179179
if (status != AHT20_STATUS_OK) {
180180
print_error(status);
181-
return 3;
181+
aht20_soft_reset(&hi2c1);
182+
continue;
182183
}
183184

184185
aht20_calculate_measurments(measured_data, &humidity, &temperature_c, &temperature_f);

0 commit comments

Comments
 (0)