//setup
RCC->AHBENR |= RCC_AHBENR_GPIOAEN; //enable the bus for port IOPA.
GPIOA->MODER |= 0x400; // MODER5 (PA5) LD2,
GPIOA->MODER |= 0x100000; // MODER10 (PA10) LED
GPIOC->PUPDR |= 0x2000000; // (PC13) B1
GPIOA->PUPDR |= 0x20; //(PA2) B2
//main
while (1)
{
if(GPIOA->IDR |= 0x4){
GPIOA->BSRR = 0x400;
HAL_Delay(1000);
GPIOA->BRR = 0x400;
HAL_Delay(1000);
}
if(GPIOC->IDR &= 0x2000){
GPIOA->BRR = 0x20;
}
else{
GPIOA->BSRR = 0x20;
}
}
So the first if statement, the led is toggling on its own and the button is not working.
the second if statement is correct but it needs to work like simultaneously like millis()
in Arduino IDE.
My questions are, is there anything I missed with configuring the button and how to make it work simultaneously?
You have made some very basic errors with C operators.
if(GPIOA->IDR |= 0x4)
means the same as:
temporary_value = (GPIOA->IDR | 0x4);
GPIOA->IDR = temporary_value;
if (temporary_value != 0)
This is wrong for two reasons: firstly, you aren't allowed to write to IDR, it is the input data register. Secondly the if
control expression will always be true because anything OR 4 is always not equal to zero.
Similarly further down &=
is also an assignment operator, you are trying to write to the input register again.
To test if a bit in a register is set, use:
if ((GPIOA->IDR & (1 << n)) != 0)
Where n
is the bit number you want to test. Don't calculate the bitmask in your head (eg: 0x400
) because you are more likely to make an error.