Skip to content

Commit 28fb3bb

Browse files
authored
Merge pull request #3 from git-user-cpp/development
v1.1.1
2 parents 5445fa1 + bc2cb62 commit 28fb3bb

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

Core/Inc/AHT20.h

Lines changed: 7 additions & 1 deletion
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
*
@@ -61,7 +67,7 @@ aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word);
6167
* Datasheet: AHT20 Product manuals
6268
* 5.4 Sensor reading process, paragraph 2
6369
*/
64-
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data);
70+
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data, uint16_t measured_data_size);
6571

6672
/*
6773
* resets the sensor without turning off the power supply

Core/Src/AHT20.c

Lines changed: 18 additions & 20 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
*/
@@ -119,39 +129,27 @@ aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word) {
119129
* Datasheet: AHT20 Product manuals
120130
* 5.4 Sensor reading process, paragraph 2
121131
*/
122-
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data) {
123-
uint8_t received_crc = 0;
124-
132+
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data, uint16_t measured_data_size) {
125133
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, MEASURE_CMD, (uint16_t)sizeof(MEASURE_CMD), HAL_MAX_DELAY)) {
126134
return AHT20_STATUS_NOT_TRANSMITTED;
127135
}
128136
HAL_Delay(80);
129137

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);
138+
if (HAL_OK != HAL_I2C_Master_Receive(hi2c, DEVICE_ADDRESS, measured_data, measured_data_size, HAL_MAX_DELAY)) {
139+
return AHT20_STATUS_NOT_RECEIVED;
138140
}
139141

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

146146
uint8_t calculated_crc = calculate_crc(measured_data);
147-
if (calculated_crc == received_crc) {
148-
uint8_t ack = 0x06;
149-
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, &ack, (uint16_t)sizeof(ack), HAL_MAX_DELAY)) {
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)) {
150149
return AHT20_STATUS_NOT_TRANSMITTED;
151150
}
152151
} else {
153-
uint8_t nack = 0x15;
154-
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)) {
155153
return AHT20_STATUS_NOT_TRANSMITTED;
156154
}
157155

Core/Src/main.c

Lines changed: 7 additions & 11 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
}
@@ -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,22 +165,20 @@ int main(void)
167165

168166
/* Infinite loop */
169167
/* USER CODE BEGIN WHILE */
170-
uint8_t measured_data[6] = {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);
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);
181-
return 3;
176+
aht20_soft_reset(&hi2c1);
177+
continue;
182178
}
183179

184-
aht20_calculate_measurments(measured_data, &humidity, &temperature_c, &temperature_f);
185-
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);
186182

187183
HAL_Delay(1000);
188184
/* USER CODE END WHILE */

0 commit comments

Comments
 (0)