cancel
Showing results for 
Search instead for 
Did you mean: 

Evaluation if conditional statement on ST Visual Developer with Cosmic for STM8

Amgad
Associate

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)
```

1 ACCEPTED SOLUTION

Accepted Solutions

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

4 REPLIES 4

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

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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;

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..