0

I code in ic stm32f103vet for press button to turn on/off the led

#include "stm32f10x_rcc.h"
#include "stm32f10x.h"
#include "misc.h"

void GPIO_Config(void);

BitAction BitVal;

int main(){

    GPIO_Config();
    GPIO_SetBits(GPIOA, GPIO_Pin_6);
    
    while(1)
    {
        if(!GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_10)){
            while(!GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_10)){
            GPIO_WriteBit(GPIOA, GPIO_Pin_6, BitVal);
            BitVal =! BitVal; //error: assigning to 'BitAction' from incompatible type 'bool'
            }
        }
    }
}   
void GPIO_Config(void){
    GPIO_InitTypeDef  GPIO_InitStructure; 
    
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE, ENABLE);
    /*config Led PA6*/
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /*config Button E10*/
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOE, &GPIO_InitStructure);   //button U6
}

with definition of 'BitVal' is

/** 
  * @brief  Bit_SET and Bit_RESET enumeration  
  */
typedef enum
{ Bit_RESET = 0,
  Bit_SET
}BitAction;

i try to toggle bit value of BitVal with many way but it still not work how can i do that?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
tung vu
  • 1
  • 1
  • 2
    Since the enum values are only 0,1 you can use: `BitVal = !BitVal;` – wohlstad Oct 31 '22 at 13:50
  • i just try it but still not work with same error: assigning to 'BitAction' from incompatible type 'bool' – tung vu Oct 31 '22 at 13:52
  • In the general sense, if `BitVal` is of type `BitAction`, you can use `BitVal = ( BitVal == Bit_SET ) ? Bit_RESET : Bit_SET;`. That will work even if `Bit_SET` and `Bit_RESET` have other values than `1` and `0`. – Cheatah Oct 31 '22 at 13:53
  • You can try to cast: `BitVal = (BitAction)(!BitVal);` – wohlstad Oct 31 '22 at 13:53
  • What about `BitVal = 1 - BitVal`? – Marco Bonelli Oct 31 '22 at 13:55
  • 1
    Although you have narrowed this down to C, [the error message appears to be a message Clang reports when compiling for C++](https://godbolt.org/z/PY1MoTTsh). When compiling for C, it does not produce this message, even with elevated warning switches. So the first problem to solve is to ensure you are compiling as the programming language you want. If you want to use C, then change the switches (or the source file name extensions that may affect what language the compiler uses) to use C instead of C++, and this message will go away. – Eric Postpischil Oct 31 '22 at 14:28
  • If you want to use C++, then delete the C tag and put back the tag for C++. In that case, a cast to `(BitAction)` would silence the message, but there are other options in C++, and I do not know what C++ practitioners would generally recommend. – Eric Postpischil Oct 31 '22 at 14:30

1 Answers1

0

In first, you need to know that below code will toggle your LED from ON to OFF state until button is pressed:

        while(1) {            
            if(!GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_10)){
                while(!GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_10)){
                GPIO_WriteBit(GPIOA, GPIO_Pin_6, BitVal);
                BitVal =! BitVal; //error: assigning to 'BitAction' from incompatible type 'bool'
                }
            }
        }

It would be good to implement any flag informing that button was already pressed and some mechanism to handle button connector debounce effect.

If you want to learn how to toggle bit values in C (which is used many times a specially in embedded programming) try to get familiar with XOR (^) operations.

because your enum have only two values 0 or 1 and to have button state flag and also button debounce effect handled properly, you can use:

        int main() {
            GPIO_Config();
            GPIO_SetBits(GPIOA, GPIO_Pin_6);
             int debounceCnt = 0;
             _Bool btnPressed = false;
 
             while (1) {
                           
                while (!GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_10) && !btnPressed) {
                    if (80 < debounceCnt++) {
                        GPIO_WriteBit(GPIOA, GPIO_Pin_6, BitVal);
                        BitVal ^= Bit_SET; // toggle BitVal
                        btnPressed = true;
                        debounceCnt = 0;
                    }
                }
                
                if (btnPressed && GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_10)) {
                    if (80 < ++debounceCnt) {
                        btnPressed = false;
                        debounceCnt = 0;
                    }
                }
            }
        }
eRtuSa
  • 46
  • 5