Skip to content

Commit bc2cb62

Browse files
committed
data was packed into structure, measuring fun was improved
1 parent b40bcaf commit bc2cb62

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

Core/Inc/AHT20.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ typedef enum {
3131
AHT20_STATUS_NOT_MEASURED,
3232
} aht20_status_t;
3333

34+
typedef struct {
35+
uint8_t measured_data[7];
36+
float humidity;
37+
float temperature_c;
38+
float temperature_f;
39+
} aht20_data_t;
3440
/*
3541
* sends reads status_word for further calibration verification
3642
*

Core/Src/AHT20.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ static uint8_t INIT_CMD[3] = {0xbe, 0x08, 0x00};
6262
*/
6363
static uint8_t MEASURE_CMD[3] = {0xac, 0x33, 0x00};
6464

65+
/*
66+
* acknowledge signal
67+
*/
68+
static uint8_t ACK_CMD = 0x06;
69+
70+
/*
71+
* not acknowledge signal
72+
*/
73+
static uint8_t NACK_CMD = 0x15;
74+
6575
/*
6676
* calculates crc8 for given data
6777
*/
@@ -133,15 +143,13 @@ aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data, ui
133143
return AHT20_STATUS_NOT_MEASURED;
134144
}
135145

136-
uint8_t calculated_crc = calculate_crc(measured_data + 1);
137-
if (calculated_crc == measured_data[0]) {
138-
uint8_t ack = 0x06;
139-
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, &ack, (uint16_t)sizeof(ack), HAL_MAX_DELAY)) {
146+
uint8_t calculated_crc = calculate_crc(measured_data);
147+
if (calculated_crc == measured_data[6]) {
148+
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, &ACK_CMD, (uint16_t)sizeof(ACK_CMD), HAL_MAX_DELAY)) {
140149
return AHT20_STATUS_NOT_TRANSMITTED;
141150
}
142151
} else {
143-
uint8_t nack = 0x15;
144-
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, &nack, (uint16_t)sizeof(nack), HAL_MAX_DELAY)) {
152+
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, &NACK_CMD, (uint16_t)sizeof(NACK_CMD), HAL_MAX_DELAY)) {
145153
return AHT20_STATUS_NOT_TRANSMITTED;
146154
}
147155

Core/Src/main.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ int main(void)
145145

146146
/* getting info about sensor calibration */
147147
status = aht20_get_calibration_status(&hi2c1, &huart2, &status_word, (uint16_t)sizeof(status_word));
148-
149148
if (status != AHT20_STATUS_OK) {
150149
print_error(status);
151150
return 1;
@@ -155,7 +154,6 @@ int main(void)
155154
if (status != AHT20_STATUS_OK) {
156155
/* calibrates if not calibrated*/
157156
status = aht20_calibrate(&hi2c1, status_word);
158-
159157
if (status != AHT20_STATUS_OK) {
160158
print_error(status);
161159
return 2;
@@ -167,23 +165,20 @@ int main(void)
167165

168166
/* Infinite loop */
169167
/* USER CODE BEGIN WHILE */
170-
uint8_t measured_data[7] = {0};
171-
float humidity = 0.0;
172-
float temperature_c = 0.0;
173-
float temperature_f = 0.0;
168+
aht20_data_t sensor_data = {0};
174169

175170
while (1)
176171
{
177172
/* triggering measuring */
178-
status = aht20_measure(&hi2c1, measured_data, (uint16_t)sizeof(measured_data));
173+
status = aht20_measure(&hi2c1, sensor_data.measured_data, (uint16_t)sizeof(sensor_data.measured_data));
179174
if (status != AHT20_STATUS_OK) {
180175
print_error(status);
181176
aht20_soft_reset(&hi2c1);
182177
continue;
183178
}
184179

185-
aht20_calculate_measurments(measured_data, &humidity, &temperature_c, &temperature_f);
186-
transmit_data(humidity, temperature_c, temperature_f);
180+
aht20_calculate_measurments(sensor_data.measured_data, &sensor_data.humidity, &sensor_data.temperature_c, &sensor_data.temperature_f);
181+
transmit_data(sensor_data.humidity, sensor_data.temperature_c, sensor_data.temperature_f);
187182

188183
HAL_Delay(1000);
189184
/* USER CODE END WHILE */

0 commit comments

Comments
 (0)