stm32-sht2x
stm32-sht2x copied to clipboard
Problem with code
Hello and thanks for sharing code I use the code in Keil and I add the library. when I compile the code, I have 8 errors with these statements uint8_t cmd = (hold)?SHT2x_READ_RH_HOLD:SHT2x_READ_RH_NOHOLD; and the error is : ..\Inc\sht2x_for_stm32_hal.c(63): error: #65: expected a ";" What is the problem? Do I need to make any changes in code? Thank you
Hi. Thanks for letting me know.
I've checked the .c file and the original line seems to be
uint8_t cmd = (hold ? SHT2x_READ_RH_HOLD : SHT2x_READ_RH_NOHOLD);.
Please replace the line and let me know if the problem persists?
Thanks!
Hi I changed the line as you said but there is no change in errors. I have the same error where statements like this are used.
uint8_t cmd = (hold ? SHT2x_READ_TEMP_HOLD : SHT2x_READ_TEMP_NOHOLD);
..\Inc\sht2x_for_stm32_hal.c(64): error: #53: expected a ":" uint8_t cmd = (hold ? SHT2x_READ_RH_HOLD : SHT2x_READ_RH_NOHOLD); ..\Inc\sht2x_for_stm32_hal.c(64): error: #18: expected a ")" uint8_t cmd = (hold ? SHT2x_READ_RH_HOLD : SHT2x_READ_RH_NOHOLD);
Hello. I'm sorry that I'd only tested the code in STM32CubeIDE.
I've just tested the code in Keil and ran into the same error.
../Core/Src/main.c(124): error: #18: expected a ")"
It seems that the Keil compiler doesn't work well with the binary numbers I defined in the .h header file, i.e. SHT2x_READ_RH_HOLD and SHT2x_READ_RH_NOHOLD.
I've replaced all binary values with hexadecimal and the errors were cleared.
Please update your .h file and .c file with the new version and see if the errors persist? Thanks.
Thanks for your consideration. I change the .h file and now I do not have those errors. But there is an error in .c file I think it needs to change the parameter name. It occurs in this function in .c file : void SHT2x_SetResolution(Resolution res) { uint8_t val = SHT2x_ReadUserReg(); val = (val & 0b01111110) | res; uint8_t temp[2] = { SHT2x_WRITE_REG, val }; HAL_I2C_Master_Transmit(_sht2x_ui2c, SHT2x_I2C_ADDR << 1, temp, 2, SHT2x_TIMEOUT); }
The error is : ..\Inc\sht2x_for_stm32_hal.c(72): error: #20: identifier "Resolution" is undefined void SHT2x_SetResolution(Resolution res) { ..\Inc\sht2x_for_stm32_hal.c(74): error: #18: expected a ")" val = (val & 0b01111110) | res;
Sorry about the first error. I changed the identifier Resolution and forgot to update the .c file. Thanks for letting me know.
I think the second error has to do with the binary number problem.
Please update the .c file and see if they're still here?
Thank you very much. Now there is no error in compiling. I will test the library on hardware tomorrow and I will tell the results. Thanks for your replying
Sure thing! Thanks for your interest in my library as well.
Hi I add the library to my main code #include "sht2x_for_stm32_hal.h" and I have a new error sht20\sht20.axf: Error: L6200E: Symbol _sht2x_ui2c multiply defined (by sht2x_for_stm32_hal.o and main.o). I think some variable is defined twice
Hi. Sorry about the error.
This is because I accidentally defined _sht2x_ui2c in the .h header. So every time you call #include "sht2x_for_stm32_hal.h", the linker defines it again and leads to that error.
I've moved that variable to the .c file and made it extern in the header. Please update the .c and .h files and see if the error will go away?
Sorry again about the inconvenience.
You are welcome Thank you for replying and solving the problem. I really appreciate that I changed the file and there is no error I generate the project with cubemx and there is difference between my I2C function and yours Here is my I2C fucntion : static void MX_I2C1_Init(void) {
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 / hi2c1.Instance = I2C1; hi2c1.Init.ClockSpeed = 100000; hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init(&hi2c1) != HAL_OK) { Error_Handler(); } / USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
I can not see any thing on serial port
Sorry it was my fault but now I see negative values on serial monitor and they do not change Does it cause from difference ofo I2C functions?
Hi. I also use STM32CubeMX but I'm not sure about this error.
The first time I tried I also had some strange constant value and it turned out to be a soldering problem.
If it's not a hardware issue, could it be the I2C problem? I configured the I2C1 mode to "I2C" and used the default settings in STM32CubeMX, but I'm not sure why our I2C functions are different.
Are you having negative values like -46.85 or -6?
Yes exactly that values
Hello. I've replicated the error with my SHT20 sensor. I think it's a small glitch with the I2C connection in no hold master mode, causing it to miss the ACK bit.
Please update your .c file and see if this persists?
Sorry again about the inconvenience.
Hi I really appreciate your help, thank you I changed the .c file and it did not work. There is nothing on the serial monitor. But with the previous .c file, I reconnect the power, and everything went well and I can see the temperature and humidity on the serial monitor. I think it would be better you change the .c file. Thank you very much for sharing the code and help me to solve the problem
Hi. So now you can see the proper temperature and humidity values in the serial monitor or just negative values?
Now I can see the proper values
That's good news! I'll revert the .c file.
Thank you for your useful feedback. It helps me a lot with my future projects. Hopefully everything goes well for your project as well.
Thank you very much, you help me a lot
Glad I was able to help!
Hi again I faced a new problem. I am using a tft lcd with FSMC and after I config them together, I mean lcd and SHT20, I always ger negative numbers and by reconnecting power, it does not change. I use your DS1307 library too with lcd. I do not have a problem with that it works great, but this one shows negative values. Is there any conflict?
Very sorry that I was away for the weekend. Has the problem been resolved?
I used SHT20 and DS1307 in another project and there weren't conflict. I'm not sure about the LCD, but if you get negative values from the SHT20 it's likely an I2C communication problem.
Hello. What STM32 core are you using?
I think the there may be a conflict between the I2C and FSMC, as described in this post?
When the FSMC is being used, the NADV signal is set to 1 by default when the alternate function output is selected for this pin. TIM4_CH2 and the I2C1 SDA signal are in conflict with the NADV signal.
If your STM32 core has more than one I2C bus, maybe you can set up another I2C bus and connect your I2C devices to it, or else you can try remapping the I2C pins in STM32CubeMX?
Hi. No problem you are welcome. I test them together with serial port and there is no problem but when using lcd, they do not work. I tested PB8 and PB9 as I2C but it did not work
Hello. I believe the conflict between I2C1 and FSMC is documented in ST’s official errata. As a workaround, they suggested:
Concerning I2C1, it is possible to use the remap functionality available on the PB8 and PB9 pins. Otherwise, disable the FSMC clock through the "RCC_AHBENR" register prior using the I2C1.
Since remapping doesn’t work, I think you can try their suggestion to disable the FSMC clock before calling the library functions and enable it after calling them?
Or a easier workaround, I believe, is to not use the I2C1 bus for your SHT20 and DS1307 devices. (If your microcontroller has more than one I2C interface.) I think the code is identical, except that you need to pass a different I2C handle to the Init functions, for example SHT2x_Init(&hi2c3);.
I’m sorry that I cannot provide a better solution to this issue as I’m not familiar with FSMC, but I hope this information can help you in some way?
I tried for solving problem but I could not. I used another board to read the snsor and send the data to main board. Thanks for your replying
Sorry that I couldn't help. Happy to hear that you got it working though.
Thank you very much. I share your github with my friends. they are useful libraries. Thank you
Thank you for the feedback! Hopefully your friends find them useful as well.