Skip to content

Commit 717f0f3

Browse files
committed
added soft reset
1 parent 7c6d4ad commit 717f0f3

File tree

3 files changed

+62
-23
lines changed

3 files changed

+62
-23
lines changed

Core/Inc/AHT20.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word);
6363
*/
6464
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data);
6565

66+
/*
67+
* resets the sensor without turning off the power supply
68+
*
69+
* Datasheet: AHT20 Product manuals
70+
* 5.5 Soft reset
71+
*/
72+
aht20_status_t aht20_soft_reset(I2C_HandleTypeDef *hi2c);
73+
6674
/*
6775
* calculates measured_data and writes the calculation in provided variables
6876
*

Core/Src/AHT20.c

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@
3030
*/
3131
static const uint16_t DEVICE_ADDRESS = (0x38 << 1);
3232

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+
3341
/*
3442
* get status command. is needed for sending to device after power on
3543
*
3644
* Datasheet: AHT20 Product manuals
3745
* 5.4 Sensor reading process, paragraph 1
3846
*/
39-
static uint8_t GET_STATUS = 0x71;
47+
static uint8_t GET_STATUS_CMD = 0x71;
4048

4149
/*
4250
* array for calibration initialization
@@ -54,6 +62,9 @@ static uint8_t INIT_CMD[3] = {0xbe, 0x08, 0x00};
5462
*/
5563
static uint8_t MEASURE_CMD[3] = {0xac, 0x33, 0x00};
5664

65+
/*
66+
* calculates crc8 for given data
67+
*/
5768
static uint8_t calculate_crc(uint8_t *data);
5869

5970
/*
@@ -63,7 +74,7 @@ static uint8_t calculate_crc(uint8_t *data);
6374
* 5.3 Send command
6475
*/
6576
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)) {
6778
return AHT20_STATUS_NOT_TRANSMITTED;
6879
}
6980

@@ -95,7 +106,7 @@ aht20_status_t aht20_check_calibration(uint8_t status_word) {
95106
* 5.4 Sensor reading process, paragraph 1
96107
*/
97108
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)) {
99110
return AHT20_STATUS_NOT_TRANSMITTED;
100111
}
101112

@@ -111,43 +122,76 @@ aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word) {
111122
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data) {
112123
uint8_t received_crc = 0;
113124

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)) {
115126
return AHT20_STATUS_NOT_TRANSMITTED;
116127
}
117128
HAL_Delay(80);
118129

119130
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);
121132

122133
uint8_t all_data[7];
123134
if (measuring_status & (1 << 7)) {
124135
return AHT20_STATUS_NOT_MEASURED;
125136
} 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);
127138
}
128139

129140
// 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) {
131142
measured_data[i] = all_data[i];
132143
}
133144
received_crc = all_data[6]; // CRC is the 7th byte
134145

135146
uint8_t calculated_crc = calculate_crc(measured_data);
136147
if (calculated_crc == received_crc) {
137148
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)) {
139150
return AHT20_STATUS_NOT_TRANSMITTED;
140151
}
141152
} else {
142153
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)) {
144155
return AHT20_STATUS_NOT_TRANSMITTED;
145156
}
157+
158+
aht20_soft_reset(hi2c);
146159
}
147160

148161
return AHT20_STATUS_OK;
149162
}
150163

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+
151195
/*
152196
* calculates crc8 for given data
153197
*/
@@ -169,18 +213,4 @@ static uint8_t calculate_crc(uint8_t *data) {
169213
return crc;
170214
}
171215

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]);
182216

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-
}

Core/Src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ int main(void)
183183

184184
aht20_calculate_measurments(measured_data, &humidity, &temperature_c, &temperature_f);
185185
transmit_data(humidity, temperature_c, temperature_f);
186+
186187
HAL_Delay(1000);
187188
/* USER CODE END WHILE */
188189

0 commit comments

Comments
 (0)