From c994c3323ee1b0d19faa86ebe26a01d566b86eaa Mon Sep 17 00:00:00 2001 From: root Date: Wed, 17 Feb 2021 18:33:49 +0000 Subject: stm8 humidity sensor --- humidity_sensors/app/awu.c | 58 ++++++++++++++++++++------------------------ humidity_sensors/app/main.c | 8 +++--- humidity_sensors/app/sht20.c | 31 +++++++++++++++++++++++ 3 files changed, 62 insertions(+), 35 deletions(-) diff --git a/humidity_sensors/app/awu.c b/humidity_sensors/app/awu.c index 18cf1f8..dc7ea9f 100644 --- a/humidity_sensors/app/awu.c +++ b/humidity_sensors/app/awu.c @@ -1,21 +1,19 @@ #include "project.h" /** Contains the different values to write in the APR register (used by AWU_Init function) */ -CONST uint8_t APR_Array[17] = - { - 0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 61, 23, 23, 62 - }; +CONST uint8_t APR_Array[17] = { + 0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 61, 23, 23, 62 +}; /** Contains the different values to write in the TBR register (used by AWU_Init function) */ -CONST uint8_t TBR_Array[17] = - { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 14, 15, 15 - }; +CONST uint8_t TBR_Array[17] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 14, 15, 15 +}; -FlagStatus AWU_GetFlagStatus(void) +FlagStatus AWU_GetFlagStatus (void) { - return((FlagStatus)(((uint8_t)(AWU->CSR & AWU_CSR_AWUF) == (uint8_t)0x00) ? RESET : SET)); + return ((FlagStatus) (((uint8_t) (AWU->CSR & AWU_CSR_AWUF) == (uint8_t)0x00) ? RESET : SET)); } @@ -25,46 +23,42 @@ INTERRUPT_HANDLER (AWU_IRQHandler, 1) } -void AWU_Init(AWU_Timebase_TypeDef AWU_TimeBase) +void AWU_Init (AWU_Timebase_TypeDef AWU_TimeBase) { /* Enable the AWU peripheral */ AWU->CSR |= AWU_CSR_AWUEN; - + /* Set the TimeBase */ - AWU->TBR &= (uint8_t)(~AWU_TBR_AWUTB); - AWU->TBR |= TBR_Array[(uint8_t)AWU_TimeBase]; - + AWU->TBR &= (uint8_t) (~AWU_TBR_AWUTB); + AWU->TBR |= TBR_Array[ (uint8_t)AWU_TimeBase]; + /* Set the APR divider */ - AWU->APR &= (uint8_t)(~AWU_APR_APR); - AWU->APR |= APR_Array[(uint8_t)AWU_TimeBase]; + AWU->APR &= (uint8_t) (~AWU_APR_APR); + AWU->APR |= APR_Array[ (uint8_t)AWU_TimeBase]; } -void AWU_LSICalibrationConfig(uint16_t lsifreqkhz) +void AWU_LSICalibrationConfig (uint16_t lsifreqkhz) { uint16_t A = 0x0; - - + + /* Calculation of AWU calibration value */ - - A = (uint16_t)(lsifreqkhz >> 2U); /* Division by 4, keep integer part only */ - + + A = (uint16_t) (lsifreqkhz >> 2U); /* Division by 4, keep integer part only */ + if ((4U * A) >= ((lsifreqkhz - (4U * A)) * (1U + (2U * A)))) - { - AWU->APR = (uint8_t)(A - 2U); - } + AWU->APR = (uint8_t) (A - 2U); else - { - AWU->APR = (uint8_t)(A - 1U); - } + AWU->APR = (uint8_t) (A - 1U); } -void awu_init(void) +void awu_init (void) { - AWU_LSICalibrationConfig(128); - AWU_Init(AWU_TIMEBASE_1S); + AWU_LSICalibrationConfig (128); + AWU_Init (AWU_TIMEBASE_1S); } diff --git a/humidity_sensors/app/main.c b/humidity_sensors/app/main.c index 6f15cdf..6c9e602 100644 --- a/humidity_sensors/app/main.c +++ b/humidity_sensors/app/main.c @@ -15,9 +15,11 @@ main (void) sht20_reset(); - enableInterrupts (); + enableInterrupts(); for (;;) { + sht20_setup(); + printf ("$SNTHD,%s,%s\n", sht20_temp_s(), sht20_humid_s()); while (!uart_rx (&c)) { @@ -25,8 +27,8 @@ main (void) sht20_reset(); } - delay_ms(10); + delay_ms (10); halt(); - delay_ms(10); + delay_ms (10); } } diff --git a/humidity_sensors/app/sht20.c b/humidity_sensors/app/sht20.c index d6d648b..5d6acb7 100644 --- a/humidity_sensors/app/sht20.c +++ b/humidity_sensors/app/sht20.c @@ -11,6 +11,13 @@ #define SOFT_RESET 0xFE +#define MAX_RESOLUTION ((u8) 0x00) +#define BATTERY_LOW ((u8) 0x40) +#define RESERVED_MASK ((u8) 0x38) +#define ENABLE_HEATER ((u8) 0x04) +#define DISABLE_OTP_RELOAD ((u8) 0x02) + + void sht20_reset (void) { @@ -119,3 +126,27 @@ sht20_humid_s (void) return ret; } + +void sht20_setup (void) +{ + uint8_t u; + + i2cb_start_transaction (SHT20_ADDRESS, I2C_WRITE); + i2cb_send_data (READ_USER_REG); + i2cb_start_transaction (SHT20_ADDRESS, I2C_READ); + u = i2cb_read (0); + i2cb_stop(); + + + u &= RESERVED_MASK; + u |= MAX_RESOLUTION | DISABLE_OTP_RELOAD; + + // FWIW - it appears to be impossible to turn off the heater + // with either a reset or write, perhaps a fake chip? + u &= ~ ENABLE_HEATER; + + i2cb_start_transaction (SHT20_ADDRESS, I2C_WRITE); + i2cb_send_data (WRITE_USER_REG); + i2cb_send_data (u); + i2cb_stop(); +} -- cgit v1.2.3