2023-12-28 06:50 AM
I am using STVD IDE to write code for STM8 the compiler I am using is Cosmic STM8 C compiler and the standard peripheral library provided for STM8. The problem I have is while evaluating and if statement for and input pin, if the condition is "== 0" or "== RESET" like the below code the code behaves properly, and the condition is evaluated successfully.
```
if(GPIO_ReadInputPin(GPIOC,GPIO_PIN_2) == RESET)
if(GPIO_ReadInputPin(GPIOC,GPIO_PIN_2) == 0)
```
But when evaluation the if statement with "== 1" or "== SET" as shown below the condition is never met despite measuring high (3.3V) on the actual input itself
```
if(GPIO_ReadInputPin(GPIOC,GPIO_PIN_2) == SET)
if(GPIO_ReadInputPin(GPIOC,GPIO_PIN_2) == 1)
```
I managed to make a workaround that by making the condition "!= 0" or "!= RESET" as shown below it is evaluated successfully and the code behaves properly, but I don't really understand why it works with "!= 0" or "!= and not with "== 0" or "== RESET", does it has anything to do with the compiler?
```
if(GPIO_ReadInputPin(GPIOC,GPIO_PIN_2) != RESET)
if(GPIO_ReadInputPin(GPIOC,GPIO_PIN_2) != 0)
```
Solved! Go to Solution.
2023-12-28 08:34 AM
Ok, but that's an arbitrary cast, and perhaps beyond the scope/implementation of a simple 8-bit compiler. Where false is zero, and true is everything else within the number space.
Quick search indicates this was reported more than a decade ago
https://community.st.com/t5/stm8-mcus/stm8-library-bug-gpio-readinputpin/td-p/490178
2023-12-28 08:05 AM
Are you sure the return is actually Boolean? It might just be an AND MASK using GPIO_PIN_2, in this case, so not zero/one, or absolute/specific values. Look at the library source.
Perhaps use
if(GPIO_ReadInputPin(GPIOC,GPIO_PIN_2)) // SET
if(!GPIO_ReadInputPin(GPIOC,GPIO_PIN_2)) // RESET
2023-12-28 08:21 AM
The return type of GPIO_ReadInputPin is an enum of type Bitset here is the implementation of them
BitStatus GPIO_ReadInputPin(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
{
return ((BitStatus)(GPIOx->IDR & (uint8_t)GPIO_Pin));
}
typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus, BitStatus, BitAction;
2023-12-28 08:34 AM
Ok, but that's an arbitrary cast, and perhaps beyond the scope/implementation of a simple 8-bit compiler. Where false is zero, and true is everything else within the number space.
Quick search indicates this was reported more than a decade ago
https://community.st.com/t5/stm8-mcus/stm8-library-bug-gpio-readinputpin/td-p/490178
2023-12-29 09:26 AM
Cross-post of this https://stackoverflow.com/questions/77727600/evaluation-if-conditional-statement-on-st-visual-developer-with-cosmic-for-stm8/77729742#77729742